我正在使用自定义的浮动操作菜单。我需要在显示/隐藏菜单按钮时实现缩放动画,就像这里所示floating action button behaviour.
有没有什么方法可以做到这一点?
我正在使用自定义的浮动操作菜单。我需要在显示/隐藏菜单按钮时实现缩放动画,就像这里所示floating action button behaviour.
有没有什么方法可以做到这一点?
你可以在浮动操作按钮上使用这些缩放动画,它会产生与设计库浮动操作按钮相同的效果。
scale_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="100"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="85%"
android:pivotY="85%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
scale_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="100"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="85%"
android:pivotY="85%"
android:toXScale="0"
android:toYScale="0" />
</set>
FloatingActionButton fab = this.findViewById( ... );
Animation myAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_up);
fab.startAnimation(myAnim);
意思是:创建一个浮动操作按钮(FloatingActionButton)对象,并为它加载一个名为scale_up
的动画资源文件。然后,通过调用startAnimation()
方法,让该按钮执行这个动画。 - adrianoBP请在自定义视图上加载这些动画。不要忘记将中心点设置为50%并将插值器设置为线性插值器。
scale_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="100"
android:fromXScale="0"
android:fromYScale="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
scale_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="100"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0"
android:toYScale="0" />
</set>