使喜欢使用MotionEvent.ACTION_MOVE主屏幕中ViewFlipper [英] Making a ViewFlipper like the Home Screen using MotionEvent.ACTION_MOVE
问题描述
好吧,我有一个 ViewFlipper
三 LinearLayouts
嵌套在它里面。它默认显示的第一个。这code:
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;
}
}
...处理视图翻转但动画是非常开/关。我不知道是否有人能帮助我的最后一部分。假设我可以访问LinearLayouts,是有办法,我可以设置基于DELTAX和移动deltaY布局的位置?
... 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?
有人给了我此链接:<一href="https://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/view/animation/TranslateAnimation.java;h=d2ff754ac327dda0fe35e610752abb78904fbb66;hb=HEAD" rel="nofollow">https://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/view/animation/TranslateAnimation.java;h=d2ff754ac327dda0fe35e610752abb78904fbb66;hb=HEAD并说我应该指的是 applyTransformation
方法如何做到这一点的提示,但我不知道如何重复同样的行为。
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.
推荐答案
移动视图你移动,而你的手指是很容易做到:
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;
现在这只是移动目前看来,没有邻居。我没有测试还没有,但那些或许可以getChildAt获得:
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.
这篇关于使喜欢使用MotionEvent.ACTION_MOVE主屏幕中ViewFlipper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!