Friday 27 July 2012

Android - how to create a sliding View

Creating a View which slides back and forth between an original position and a displacement position is easy post- Ice Cream Sandwich (by means of the new ObjectAnimator class and setTranslationX(float translationX) etc methods). Doing this pre- Ice Cream Sandwich is not so easy. Below is a class I created which you can use as a basis for creating a View which toggles (slides) between an original position and a displacement position. The way it works is to extend FrameLayout (could be a different View depending on your need), to start the translate right/left animations when the toggleSlide() method is called, and to listen for the end of the animations in order to relocate itself right/left as appropriate, as follows:

public class SlidingFrameLayout extends FrameLayout
{
  private final int durationMilliseconds = 1000;
  private final int displacementPixels = 200;

  private boolean isInOriginalPosition = true;
  private boolean isSliding = false;

  public SlidingFrameLayout(Context context)
  {
    super(context);
  }

  public SlidingFrameLayout(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs);
  }

  public SlidingFrameLayout(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);
  }

  @Override
  protected void onAnimationEnd()
  {
    super.onAnimationEnd();

    if (isInOriginalPosition)
      offsetLeftAndRight(displacementPixels);
    else
      offsetLeftAndRight(-displacementPixels);

    isSliding = false;
    isInOriginalPosition = !isInOriginalPosition;
  }

  @Override
  protected void onLayout(
      boolean changed,
      int left,
      int top,
      int right,
      int bottom)
  {
    super.onLayout(changed, left, top, right, bottom);

    // need this since otherwise this View jumps back to its original position
    // ignoring its displacement
    // when (re-)doing layout, e.g. when a fragment transaction is committed
    if (changed && !isInOriginalPosition)
      offsetLeftAndRight(displacementPixels);
  }

  public void toggleSlide()
  {
    // check whether frame layout is already sliding
    if (isSliding)
      return; // ignore request to slide

    if (isInOriginalPosition)
      startAnimation(new SlideRightAnimation());
    else
      startAnimation(new SlideLeftAnimation());

    isSliding = true;
  }

  private class SlideRightAnimation extends TranslateAnimation
  {
    public SlideRightAnimation()
    {
      super(
        Animation.ABSOLUTE, 0,
        Animation.ABSOLUTE, displacementPixels,
        Animation.ABSOLUTE, 0,
        Animation.ABSOLUTE, 0);

      setDuration(durationMilliseconds);
      setFillAfter(false);
    }
  }

  private class SlideLeftAnimation extends TranslateAnimation
  {
    public SlideLeftAnimation()
    {
      super(
        Animation.ABSOLUTE, 0,
        Animation.ABSOLUTE, -displacementPixels,
        Animation.ABSOLUTE, 0,
        Animation.ABSOLUTE, 0);

      setDuration(durationMilliseconds);
      setFillAfter(false);
    }
  }
}

Tuesday 24 July 2012

Android: Posting a Facebook question with options

Posting a question which has no associated options is easy to figure out from the Facebook documentation. Posting a question which does have associated options I struggled to find a single example for and figured out by pure luck, a shot in the dark! Solution below:

Bundle params = new Bundle();
params.putString("questions", "This is a question.");
params.putString("options", "[\"Option 1\", \"Option 2\"]");

new AsyncFacebookRunner(myFacebookObject)
    .request("me/questions", params, "POST" myRequestListener);

Tuesday 17 July 2012

How to Bulk-Crop whitespace from images

Found a good tool today - XnView by Pierre e Gougelet - for bulk-, auto- cropping of whitespace from a set of images. To do this with XnView is as simple as the following steps:

  • Select the files to be cropped;
  • Go to Tools > Batch Processing;
  • On the first tab of the Batch Processing window (the General tab) set the destination directory (if you don't want to have your files overwritten) and the output format.
  • On the second tab (the Transformations tab) select the Auto Crop feature from the list of available Transformations and click Add to add it to the list of the active transformations. On the Parameters panel at the bottom, choose your desired background colour.
  • Press the Go button and check your destination folder see that everything went okay.
Steps above adapted from John Thomas' answer found in the following thread: