android - 为布局设置动画旋转重复返回

标签 android xml animation

我想在从 0 到 -30 旋转之前和从 -30 到 0 之后创建动画旋转,所以我创建了两个文件动画 xoay.xml

<rotate  xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="-30"
    android:pivotX="50%"
    android:duration="6000"
    android:repeatCount="infinite"
    android:fillAfter="true" />

xoay2.xml

<rotate  xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="-30"
    android:toDegrees="0"
    android:pivotX="50%"
    android:duration="6000"
    android:repeatCount="infinite"
    android:fillAfter="true" />

在 Activity 中我用代码开始动画

final RotateAnimation rotate= (RotateAnimation) AnimationUtils.loadAnimation(this,R.anim.xoay);
final RotateAnimation rotate2= (RotateAnimation) AnimationUtils.loadAnimation(this,R.anim.xoay_2);

当我设置

myView.startAnimation(rotate); or myView.startAnimation(rotate2);
它运行。现在我想当 myView 完成动画 xoay.xml 时它将运行 xoay2.xml 并且当 xoay2.xml 完成时它运行 xoay.xml ... 我试过代码:

AnimationSet s = new AnimationSet(false);
s.addAnimation(rotate);
s.addAnimation(rotate2);
myView.startAnimation(s)

但它没有运行。 我该怎么做?非常感谢!

最佳答案

您可以使用 RotateAnimation 实现这一点

Animation an = new RotateAnimation(0.0f, 30.0f);

// Set the animation's parameters
an.setDuration(6000);               // duration in ms
an.setRepeatCount(-1);                // -1 = infinite repeated
an.setRepeatMode(Animation.REVERSE); // reverses each repeat
an.setFillAfter(true);               // keep rotation after animation

// Aply animation to image view
myView.setAnimation(an);

关于android - 为布局设置动画旋转重复返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42687007/

相关文章:

android - 什么时候应该在 Flash 中使用 CPU 或 GPU 编译器选项?

java - "Internal error: Unexpected failure when preparing tensor allocations"- Firebase MLKIT - Android - 本地模型加载失败

java - XML 文件提取 - 相对路径不起作用

c# - 简单数据结构的最轻序列化方法

visual-c++ - 如何在控制台程序中向上一行(C++)

java - 找不到类 Android

android - 如何在 corona sdk mapView 中设置缩放

来自 xml 和 SEO 的 jquery append 元素

javascript - 显示动画不是动画

android - fillAfter 和 fillEnabled 在 Android View 动画 XML 中不起作用