Android 使用MotionEvent.ACTION\u MOVE使ViewFlipper像主屏幕一样
好的,我有一个Android 使用MotionEvent.ACTION\u MOVE使ViewFlipper像主屏幕一样,android,android-animation,viewflipper,Android,Android Animation,Viewflipper,好的,我有一个ViewFlipper,里面嵌套了三个LinearLayouts。它默认显示第一个。此代码: // Assumptions in my Activity class: // oldTouchValue is a float // vf is my view flipper @Override public boolean onTouchEvent(MotionEvent touchEvent) { switch (touchEvent.getAction()) { ca
ViewFlipper
,里面嵌套了三个LinearLayouts
。它默认显示第一个。此代码:
// 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;
}
}
//我的活动类中的假设:
//oldTouchValue是一个浮点值
//vf是我的视图翻转器
@凌驾
公共布尔onTouchEvent(MotionEvent touchEvent){
开关(touchEvent.getAction()){
case MotionEvent.ACTION\u DOWN:{
oldTouchValue=touchEvent.getX();
打破
}
case MotionEvent.ACTION\u UP:{
float currentX=touchEvent.getX();
如果(oldTouchValuecurrentX){
setinaimation(AnimationHelper.infomrightanimation());
setOutAnimation(AnimationHelper.outToLeftAnimation());
vf.showPrevious();
}
打破
}
case MotionEvent.ACTION\u移动:{
//TODO:创建ViewFlipper的一些代码
//就像主屏幕一样。
打破
}
}
返回false;
}
公共静态类AnimationHelper{
公共静态动画INFORMORightAnimation(){
动画信息右侧=新的TranslateAnimation(
Animation.RELATIVE_到_父级,+1.0f,
Animation.RELATIVE_到_父对象,0.0f,
Animation.RELATIVE_到_父对象,0.0f,
动画。相对于父对象,0.0f);
设置持续时间(350);
setInterpolator(新的AccelerateInterpolator());
返回版权;
}
公共静态动画outToLeftAnimation(){
动画outtoLeft=新的TranslateAnimation(
Animation.RELATIVE_到_父对象,0.0f,
Animation.RELATIVE_到_父级,-1.0f,
Animation.RELATIVE_到_父对象,0.0f,
动画。相对于父对象,0.0f);
outtoLeft.setDuration(350);
outtoLeft.setInterpolator(新的AccelerateInterpolator());
返回outtoLeft;
}
//下一个乐章
公共静态动画INFOROMLEFTANIMATION(){
动画信息左=新的TranslateAnimation(
Animation.RELATIVE_到_父级,-1.0f,
Animation.RELATIVE_到_父对象,0.0f,
Animation.RELATIVE_到_父对象,0.0f,
动画。相对于父对象,0.0f);
设置持续时间(350);
setInterpolator(新的AccelerateInterpolator());
返回左前;
}
公共静态动画outToRightAnimation(){
动画输出光=新的TranslateAnimation(
Animation.RELATIVE_到_父对象,0.0f,
Animation.RELATIVE_到_父级,+1.0f,
Animation.RELATIVE_到_父对象,0.0f,
动画。相对于父对象,0.0f);
outtoRight.setDuration(350);
setInterpolator(新的AccelerateInterpolator());
返回右侧;
}
}
。。。处理视图翻转,但动画非常“开/关”。我想知道是否有人能帮我完成最后一部分。假设我可以访问LinearLayouts,有没有一种方法可以根据deltaX和deltaY设置布局的位置
有人给了我,说我应该参考
applyTransformation
方法来获取如何执行此操作的提示,但我不知道如何重复相同的行为。在移动手指的同时移动视图很容易:
case MotionEvent.ACTION_MOVE:
final View currentView = vf.getCurrentView();
currentView.layout((int)(touchEvent.getX() - oldTouchValue),
currentView.getTop(), currentView.getRight(),
currentView.getBottom());
break;
现在,这只移动当前视图,而不移动相邻视图。我还没有对此进行测试,但这些可能可以通过getChildAt获得:
vf.getChildAt(vf.getDisplayedChild() - 1); // previous
vf.getChildAt(vf.getDisplayedChild() + 1); // next
希望对您有所帮助。如果我理解您的请求,现在应该使用 看起来像这样:
这是另一种方法忽略第二个链接,查看寻呼机是正确的方法