【问题标题】:Android: Rotate animation and move animation combinationAndroid:旋转动画和移动动画组合
【发布时间】:2012-08-10 08:40:08
【问题描述】:

我有一个 ImageView 图像。我需要将此图像向右旋转 90 度,然后将其从左向右移动。我设法做到了。我使用 AnimationListener 并在旋转完成后启动 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"
/>

旋转动画()

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

moveAnimation()

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

【问题讨论】:

  • 您能否提供用于旋转和移动图像的示例代码。
  • 好的,但是我给你我的意见.. 尝试使用矩阵进行旋转和类似的矩阵进行平移,这样你的矩阵将被集中并且图像视图不会被重置。在执行任何矩阵运算之前,不要忘记将 imageview 的属性 scaletype 设置为矩阵。

标签: android animation image-rotation


【解决方案1】:

您需要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 );
    }   
} 

【讨论】:

  • 确定我做到了!那就是问题所在!我的不好不是我没有说出来。
  • 我稍后会添加一些代码,但总之我使用 xml 动画文件进行旋转和没有 xml 文件的简单移动动画。
  • 它应该可以工作,我已经测试了代码。正如我从您的要求中了解的那样,它应该旋转 90 度(不重置)然后向右平移(不重置)。也许我错了。
猜你喜欢
  • 2011-05-20
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 2015-05-25
  • 2021-09-26
  • 1970-01-01
相关资源
最近更新 更多