【发布时间】:2015-11-22 19:18:36
【问题描述】:
我想在动画视图上设置触摸事件,然后在触摸视图上停止动画。
这是我的活动代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
touch_me = (TextView) findViewById(R.id.touch_me);
touch_me.setOnTouchListener(new OnTouchMeListener());
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int fromX = size.x;
int fromY = size.y;
int toX = (-1) * view.getWidth();
TranslateAnimation animation = new TranslateAnimation(fromX, -100, 0, 0);
animation.setDuration(6000);
animation.setRepeatCount(5);
animation.setFillAfter(true);
touch_me.startAnimation(animation);
}
这是我的班级听众:
public class OnTouchMeListener implements View.OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
touch_me.clearAnimation();
return true;
}
}
问题是仅在视图的初始位置(在 XML 文件中)检测到 onTouch 谁能帮帮我?
【问题讨论】:
-
什么是
OnTouchMeListener?这是嵌套类吗?您是否尝试将touch_me.clearAnimation();更改为v.clearAnimation();? -
@DamianKozlak OnTouchMeListener 是我自己在触摸事件上调用的类。让我试试这个我会告诉你的
-
嗨 @DamianKozlak 我试过 v.clearAnimation 但它不起作用
-
v.getAnimation().cancel();怎么样?你试过了吗? -
是的,但它也不起作用。