Making a ViewFlipper like the Home Screen using MotionEvent.ACTION_MOVE

Posted by DJTripleThreat on Stack Overflow See other posts from Stack Overflow or by DJTripleThreat
Published on 2010-04-08T14:53:09Z Indexed on 2010/04/10 18:53 UTC
Read the original article Hit count: 1347

Filed under:
|
|

Ok I have a ViewFlipper with three LinearLayouts nested inside it. It defaults to showing the first one. This code:

// Assumptions in my Activity class:
// oldTouchValue is a float
// vf is my view flipper
@Override
public boolean onTouchEvent(MotionEvent touchEvent) {
  switch (touchEvent.getAction()) {
    case MotionEvent.ACTION_DOWN: {
      oldTouchValue = touchEvent.getX();
      break;
    }
    case MotionEvent.ACTION_UP: {
      float currentX = touchEvent.getX();
      if (oldTouchValue < currentX) {
        vf.setInAnimation(AnimationHelper.inFromLeftAnimation());
        vf.setOutAnimation(AnimationHelper.outToRightAnimation());
        vf.showNext();
      }
      if (oldTouchValue > currentX) {
        vf.setInAnimation(AnimationHelper.inFromRightAnimation());
        vf.setOutAnimation(AnimationHelper.outToLeftAnimation());
        vf.showPrevious();
      }
      break;
    }
    case MotionEvent.ACTION_MOVE: {
      // TODO: Some code to make the ViewFlipper
      // act like the home screen.
      break;
    }
  }
  return false;
}

public static class AnimationHelper {
  public static Animation inFromRightAnimation() {
    Animation inFromRight = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT, +1.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f);
    inFromRight.setDuration(350);
    inFromRight.setInterpolator(new AccelerateInterpolator());
    return inFromRight;
  }

  public static Animation outToLeftAnimation() {
    Animation outtoLeft = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, -1.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f);
    outtoLeft.setDuration(350);
    outtoLeft.setInterpolator(new AccelerateInterpolator());
    return outtoLeft;
  }

  // for the next movement
  public static Animation inFromLeftAnimation() {
    Animation inFromLeft = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT, -1.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f);
    inFromLeft.setDuration(350);
    inFromLeft.setInterpolator(new AccelerateInterpolator());
    return inFromLeft;
  }

  public static Animation outToRightAnimation() {
    Animation outtoRight = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, +1.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f);
    outtoRight.setDuration(350);
    outtoRight.setInterpolator(new AccelerateInterpolator());
    return outtoRight;
  }
}

... handles the view flipping but the animations are very "on/off". I'm wondering if someone can help me out with the last part. Assuming that I can access the LinearLayouts, is there a way where I can set the position of the layouts based on deltaX and deltaY?

Someone gave me this link: https://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/view/animation/TranslateAnimation.java;h=d2ff754ac327dda0fe35e610752abb78904fbb66;hb=HEAD and said I should refer to the applyTransformation method for hints on how to do this but I don't know how to repeat this same behavior.

© Stack Overflow or respective owner

Related posts about android

Related posts about viewflipper