【问题标题】:How to make a flip animation using XML如何使用 XML 制作翻转动画
【发布时间】:2019-04-25 17:41:24
【问题描述】:

基本上我想做的是一张卡片转动的动画。 我有三个 ImageButton,我需要一个动画,当我按下它们时让它们翻转,然后在一两秒后让按下的那个消失。

【问题讨论】:

标签: android


【解决方案1】:

嘿,你可以使用 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"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-23
    • 2012-01-24
    • 2012-04-28
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多