Making a ViewFlipper like the Home Screen using MotionEvent.ACTION_MOVE

血红的双手。 提交于 2019-12-03 04:35:18

问题


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 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.


回答1:


Moving the view while you're moving your finger is easily done:

case MotionEvent.ACTION_MOVE:
    final View currentView = vf.getCurrentView();
    currentView.layout((int)(touchEvent.getX() - oldTouchValue), 
            currentView.getTop(), currentView.getRight(), 
            currentView.getBottom());
break;

Now this only moves the current view, not the neighbors. I didn't test that yet, but those can probably be obtained by getChildAt:

vf.getChildAt(vf.getDisplayedChild() - 1); // previous
vf.getChildAt(vf.getDisplayedChild() + 1); // next

Hope that helps.




回答2:


If I understand your request, this effect should now be implemented using a View Pager

Looks like this:



来源:https://stackoverflow.com/questions/2600916/making-a-viewflipper-like-the-home-screen-using-motionevent-action-move

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!