0

我有一个 ImageView 图像。我需要将此图像向右旋转 90 度,然后将其从左向右移动。我设法做到了。我使用了 AnimationListener并在旋转完成后开始moveAnimation()。但是在运动图像恢复到原来的样子之前(旋转之前)。

用于旋转rotation.xml的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"
/>

旋转动画()

    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();
        }
    });

移动动画()

     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);
}
4

1 回答 1

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 );
    }   
} 
于 2012-08-10T09:20:09.187 回答