Android:旋转动画并移动动画组合

时间:2012-08-10 08:40:09

标签: android animation image-rotation

我有一个ImageView图像。我需要将此图像向右旋转90度,然后将图像从左向右移动。我设法如何做到这一点。我使用 AnnimationListener ,轮换完成后,我开始 moveAnimation() 。但在移动图像之前返回原始外观(旋转前)。

用于轮换的xml代码 rotation.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromDegrees="0"
  android:interpolator="@android:anim/linear_interpolator"
  android:toDegrees="90"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="1000"
  android:startOffset="0"
/>

rotateAnimation()

    private void rotateAnimation(){

        Animation rotation = AnimationUtils.loadAnimation(getContext(), R.anim.rotate);
        rotation.setRepeatCount(0);
        rotation.setFillAfter(true);

        rotation.setAnimationListener(new AnimationListener() {


        public void onAnimationEnd(Animation animation) {
            moveAnnimation();
        }
    });

moveAnnimation()

     private void moveAnnimation(){

    TranslateAnimation moveLefttoRight = new TranslateAnimation(0, 2000, 0, 0);
        moveLefttoRight.setDuration(1000);
        moveLefttoRight.setFillAfter(true);

        moveLefttoRight.setAnimationListener(new AnimationListener() {

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

        }

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

        }

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

        }
    });


    image.startAnimation(moveLefttoRight);
}

1 个答案:

答案 0 :(得分:0)

旋转和翻译setFillAfter(true)对象需要Animation

Animation.html#setFillAfter(boolean)

If fillAfter is true, the transformation that this animation performed will persist when it is finished. Defaults to false if not set. Note that this applies to individual animations and when using an AnimationSet to chain animations.

以下是我的代码,你需要AnimationSet来实现链接动画效果。

public class MainActivity extends Activity {

    ImageView image = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        image = (ImageView)findViewById(R.id.imageview);

        animateImageView();
    }

    private void animateImageView() {
        AnimationSet set = new AnimationSet(true);
        set.setFillAfter(true);

        Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
        TranslateAnimation moveLefttoRight = new TranslateAnimation(0, 200, 0, 0);
        moveLefttoRight.setDuration(1000);
        moveLefttoRight.setStartOffset(1000);

        set.addAnimation(rotation);
        set.addAnimation(moveLefttoRight);

        image.startAnimation( set );
    }   
}