Android图像移动

时间:2014-08-04 02:55:32

标签: android eclipse

我的图像在我的布局中移动动画,我想要发生的是当我点击图像时,动画将重新启动,它将返回到起点。这是我的新代码,请帮忙

公共类游戏扩展活动{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    final ImageView chips = (ImageView) findViewById(R.id.chips);

      final TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 500.0f);
      animation.setDuration(9000);
      animation.setRepeatCount(Animation.INFINITE);
      animation.setRepeatMode(Animation.INFINITE);
      animation.setFillAfter(true);
      chips.startAnimation(animation);

      chips.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (animation != null) {
                animation.reset();
               chips.setAnimation(null);
            }

            return true;
        }
    });

1 个答案:

答案 0 :(得分:0)

重置动画状态并将其从ImageView中删除: 触摸屏:

@Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        animation.reset();
        bula1.setAnimation(null);
        return super.onTouchEvent(event);
    }

或触摸图片视图:

bula1.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (animation != null) {
                    animation.reset();
                    bula1.setAnimation(null);
                }

                return true;
            }

        });
@Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return super.onTouchEvent(event);
    }