Android 弹出动画
Android pop in animation
我在我的应用程序中制作了一个浮动操作按钮,我想将其弹出,在那里它不可见并开始增长到 60dp 这样的大小,然后调整回 56dp 的正常大小。那怎么办?我知道如何做一个正常的淡入淡出动画,但不知道如何弹出。
我会在 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>
然后在你的 activity:
Animation expandIn = AnimationUtils.loadAnimation(this, R.anim.expand_in);
actionButton.startAnimation(expandIn);
当我将它切换为使用 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>
这是一个显示插值器示例的视频:https://www.youtube.com/watch?v=OMOJxBe9KGg
这里是 Android 文档中提到的地方:https://developer.android.com/guide/topics/resources/animation-resource.html#View
我采用了原始答案,但添加了额外的动画层以提供更好的流行效果。
enter code here
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="100"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.2"
android:toYScale="1.2" />
<scale
android:duration="100"
android:fromXScale="1.2"
android:fromYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100"
android:toXScale="0.8"
android:toYScale="0.8" />
<scale
android:duration="100"
android:fromXScale="0.8"
android:fromYScale="0.8"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
我在我的应用程序中制作了一个浮动操作按钮,我想将其弹出,在那里它不可见并开始增长到 60dp 这样的大小,然后调整回 56dp 的正常大小。那怎么办?我知道如何做一个正常的淡入淡出动画,但不知道如何弹出。
我会在 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>
然后在你的 activity:
Animation expandIn = AnimationUtils.loadAnimation(this, R.anim.expand_in);
actionButton.startAnimation(expandIn);
当我将它切换为使用 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>
这是一个显示插值器示例的视频:https://www.youtube.com/watch?v=OMOJxBe9KGg
这里是 Android 文档中提到的地方:https://developer.android.com/guide/topics/resources/animation-resource.html#View
我采用了原始答案,但添加了额外的动画层以提供更好的流行效果。
enter code here
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="100"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.2"
android:toYScale="1.2" />
<scale
android:duration="100"
android:fromXScale="1.2"
android:fromYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100"
android:toXScale="0.8"
android:toYScale="0.8" />
<scale
android:duration="100"
android:fromXScale="0.8"
android:fromYScale="0.8"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>