ImageView中的循环动画

时间:2012-05-20 10:49:06

标签: android animation imageview

我的屏幕上有一个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个动画循环(向左旋转然后向右旋转)。

我该怎么做?

抱歉我的英语不好..

2 个答案:

答案 0 :(得分:0)

您可以使用AnimationSet组合两个(或更多)动画。

API Demos中使用xml中定义的TranslateAnimation有一个“摇动”动画示例。您可以按照类似的方法获得所需的结果。

答案 1 :(得分: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);