Android流行动画

qwe*_*rtz 11 android button android-animation android-activity

我在我的应用程序中设置了一个浮动操作按钮,我希望它可以弹出,在那里它是不可见的,并开始增长到60dp的大小,然后调整回到正常大小56dp.怎么办?我知道如何做一个正常的动画淡入淡出,但不是流行音乐.

Ric*_*ick 27

我会在res/anim中创建一个动画文件并使用顺序缩放动画,如下所示:

expand_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale 
        android:fromXScale="0.0" 
        android:fromYScale="0.0"
        android:toXScale="1.1" <!--set the scale value here-->
        android:toYScale="1.1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="400"/> <!--length of first animation-->


    <scale 
        android:fromXScale="1.1" <!--From whatever scale you set in the first animation-->
        android:fromYScale="1.1"
        android:toXScale="1.0" <!--Back to normal size-->
        android:toYScale="1.0"
        android:pivotX="50%" 
        android:pivotY="50%"
        android:startOffset="400" <!--start 2nd animation when first one ends-->
        android:duration="400"/>
</set>
Run Code Online (Sandbox Code Playgroud)

然后在你的活动中:

Animation expandIn = AnimationUtils.loadAnimation(this, R.anim.expand_in);
actionButton.startAnimation(expandIn);
Run Code Online (Sandbox Code Playgroud)

  • 当我将"缩小"部分更改为.9而不是1.0时,我发现这个动画更加平滑.我不知道为什么会这样.添加@AhmedW的建议没有帮助. (3认同)

bey*_*eal 11

当我将其切换为使用Overshoot Interpolator时,我发现我的动画比当前接受的答案更平滑.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/overshoot_interpolator" >

<scale
    android:duration="700"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0" />

</set>
Run Code Online (Sandbox Code Playgroud)

这是一段显示内插器示例的视频:https://www.youtube.com/watch?v = OMOJxBe9KGg

以下是Android文档中的内容:https://developer.android.com/guide/topics/resources/animation-resource.html#View