【发布时间】:2019-04-25 17:41:24
【问题描述】:
基本上我想做的是一张卡片转动的动画。 我有三个 ImageButton,我需要一个动画,当我按下它们时让它们翻转,然后在一两秒后让按下的那个消失。
【问题讨论】:
-
但是已经关闭了
-
它有一个公认的答案:stackoverflow.com/a/46111960/5314844
标签: android
基本上我想做的是一张卡片转动的动画。 我有三个 ImageButton,我需要一个动画,当我按下它们时让它们翻转,然后在一两秒后让按下的那个消失。
【问题讨论】:
标签: android
嘿,你可以使用 XML 制作这个动画
只需按照这些步骤进行
1) 在你的 res 目录下创建 anim 文件夹
2) 为 example flip_animation.xml
添加动画资源文件3) 将此代码添加到您的动画文件中
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1"
android:toXScale="-1"
android:fromYScale="1"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"/>
<scale
android:fromXScale="-1"
android:toXScale="1"
android:fromYScale="1"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="500"
android:duration="1000"/>
然后你可以在任何视图的编程部分调用它
在 Kotlin 中你可以做到
val animationUtils = AnimationUtils.loadAnimation(this, R.anim.flip_animation)
buttonAnimation.startAnimation(animationUtils)
在 Java 中你可以这样做
AnimationUtils animationUtils = AnimationUtils.loadAnimation(this,R.flip_animation);
Button buttonAnimation = findViewById(R.id.buttonAnimation);
buttonAnimation.startAnimation(animationUtils)
这是水平翻转视图的方法,如果你想要垂直视图,你也可以试试这个。
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1"
android:toXScale="1"
android:fromYScale="1"
android:toYScale="-1"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"/>
<scale
android:fromXScale="1"
android:toXScale="1"
android:fromYScale="-1"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="500"
android:duration="1000"/>
**如果你想要一些有趣的翻转,那么简单**
你可以试试这个
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1"
android:toXScale="-1"
android:fromYScale="-1"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"/>
<scale
android:fromXScale="-1"
android:toXScale="1"
android:fromYScale="1"
android:toYScale="-1"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="500"
android:duration="1000"/>
【讨论】: