在同一活动中动画图像

时间:2014-01-03 08:18:17

标签: android animation

我想在同一个活动中一个接一个地动画三个图像。在动画开始时,第一个图像出现一段时间,然后是第二个,然后是第三个。我正在使用帧动画,但我的问题是我每次触摸屏幕时都想要动画。代码会清晰显示

final AnimationDrawable animation = new AnimationDrawable();

    animation.addFrame(getResources().getDrawable(R.drawable.hammer_one), 200);
    animation.addFrame(getResources().getDrawable(R.drawable.hammer_two), 200);
    animation.addFrame(getResources().getDrawable(R.drawable.hammer_three), 200);

    animation.setOneShot(true);

    img[0] = (ImageView)findViewById(R.id.ImageViewPreview);
    img[1] = (ImageView)findViewById(R.id.ImageViewPreview1);
    img[2] = (ImageView)findViewById(R.id.ImageViewPreview2);
    img[3] = (ImageView)findViewById(R.id.ImageViewPreview3);
    img[4] = (ImageView)findViewById(R.id.ImageViewPreview4);

以上是我布局中的图像视图。

 for(i = 0 ; i<5 ; i++){

        img[i].setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent event) {

                if(event.getAction() == MotionEvent.ACTION_DOWN) {

                    int num = event.getPointerCount();

                     for (int a = 0; a < num; a++) {
                          int x = (int) event.getX(event.getPointerId(a));
                          int y = (int) event.getY(event.getPointerId(a));
                          Log.e("x",x + " ");
                          Log.e("y",y + " ");
                        }

                    arg0.setBackgroundDrawable(animation);
                    animation.start();

                    return true;
                }else if(event.getAction() == MotionEvent.ACTION_UP) {

                    int num = event.getPointerCount();

                     for (int a = 0; a < num; a++) {
                          int x = (int) event.getX(event.getPointerId(a));
                          int y = (int) event.getY(event.getPointerId(a));
                          Log.e("x",x + " ");
                          Log.e("y",y + " ");
                        }

                    arg0.setBackgroundDrawable(animation);
                    animation.start();

                    return true;
                }

                return false;
            }
        });
    }

The above is the code for onTouch i.e when the layout is touched.

现在上面的代码就第一次触摸而言工作正常。但是当我第二次触摸布局时,只有图像出现但动画没有出现。请帮助

1 个答案:

答案 0 :(得分:0)

问题可能与animation.setOneShot(true);有关吗?也许,把它设置为假。或者,在每个onTouch事件中,创建一个新的动画对象,而不是重复使用已调用setOneShot(true);的相同动画对象。

相关问题