android 如何旋转浮动动作按钮而不旋转阴影?

dm7nw8vv  于 2023-03-27  发布在  Android
关注(0)|答案(6)|浏览(163)

我以这样一种简单的方式旋转FAB:

fab.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate));
  • rotate.xml*:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"/>
</set>

这是可行的,但是它的阴影和FAB一起旋转。但是我只需要旋转FAB(或者甚至是它的src图像,如果有任何不同的话)。

webghufk

webghufk1#

你试过使用Compat库提供的animate方法吗?我在使用Animation utils时也遇到了同样的问题

final OvershootInterpolator interpolator = new OvershootInterpolator();
ViewCompat.animate(fab).
           rotation(135f).
           withLayer().
           setDuration(300).
           setInterpolator(interpolator).
           start();
gcmastyq

gcmastyq2#

public void rotateFabForward() {
    ViewCompat.animate(fab)
            .rotation(135.0F)
            .withLayer()
            .setDuration(300L)
            .setInterpolator(new OvershootInterpolator(10.0F))
            .start();
}

public void rotateFabBackward() {
    ViewCompat.animate(fab)
            .rotation(0.0F)
            .withLayer()
            .setDuration(300L)
            .setInterpolator(new OvershootInterpolator(10.0F))
            .start();
}
p5cysglq

p5cysglq3#

最短路径:对于顺时针旋转:

fab.animate().rotationBy(135f) // 135f = 135 degree.

对于逆时针旋转(复位到初始位置):

fab.animate().rotationBy(-135f) // 135f = 135 degree.
ifmq2ha2

ifmq2ha24#

还有一种完全不同的方法对我来说是完美的(在接受的答案中建议的方法会在pre-L上产生一个剪切的阴影)。

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="45">
    <bitmap android:src="@drawable/ic_add_white_24dp"/>
</rotate>

将这个可绘制对象设置为您的FAB,并直接设置其级别或FAB本身的imageLevel属性的动画;它从0到10000。如果你想要一个OvershootInterpolator,然后设置toDegrees为90,并设置动画水平到5000的值,这样它就不会超出边界。

gwo2fgha

gwo2fgha5#

你可以只旋转45度来获得在晶圆厂按钮的点击上的取消按钮效果,然后再旋转-45度来获得正常效果。

binding?.fabAddPostMenu?.animate()?.rotationBy(45f)
h43kikqp

h43kikqp6#

同样可以通过对象动画器实现:

moveRight.rotation = -180f

相关问题