0

我的屏幕上有一个 ImageView,我想让它摇晃(向左旋转然后向右旋转)。我知道如何为 ImageView 设置动画,这是我的代码:

new RotateAnimation(20f, 50f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

// Start animating the image
final ImageView splash = (ImageView) findViewById(R.id.mobileshake);
splash.startAnimation(anim);

问题是,现在 Imageview 正在循环一个动画,但我想要循环 2 个动画(向左旋转然后向右旋转)。

我怎样才能做到这一点?

对不起,我的英语不好..

4

2 回答 2

0

您可以使用AnimationSet.

API Demos中有一个使用TranslateAnimationxml 定义的“摇晃”动画的示例。您可以通过遵循类似的方法来实现您正在寻找的结果。

于 2012-05-20T11:02:27.240 回答
0

我通过执行以下操作弄清楚了,并且工作非常顺利:)

final RotateAnimation anim1 = new RotateAnimation(20f, 50f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim1.setInterpolator(new LinearInterpolator());
        //anim1.setRepeatCount(Animation.INFINITE);
        anim1.setDuration(300);

        final RotateAnimation anim2 = new RotateAnimation(50f, 20f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim2.setInterpolator(new LinearInterpolator());
        //anim2.setRepeatCount(Animation.INFINITE);
        anim2.setDuration(300);

        final ImageView splash = (ImageView) findViewById(R.id.mobileshake);
        anim1.setAnimationListener(new AnimationListener(){

            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
                splash.startAnimation(anim2);
            }

            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }});
        anim2.setAnimationListener(new AnimationListener(){

            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
                splash.startAnimation(anim1);
            }

            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }});

        splash.startAnimation(anim1);
于 2012-05-20T11:09:07.577 回答