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

问题描述 投票:10回答:4

我以这么简单的方式旋转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图像,如果有任何差异)。

android rotation android-animation floating-action-button
4个回答
25
投票

您是否尝试过使用Compat库提供的animate方法?使用Animation utils时我也遇到了同样的问题

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

13
投票
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();
}

4
投票

有一种完全另一种方法可以完美地为我工作(在接受的答案中建议的那个在前L上产生一个剪裁的阴影)。创建一个XML drawable并将您的FAB图标包装成<rotate>标记,如下所示:

<?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>

将此drawable设置为您的FAB,并直接设置其级别或FAB本身的imageLevel属性;它从0到10000.如果你想要一个OvershootInterpolator,那么将toDegrees设置为90并将该级别设置为5000的值,这样它就不会超出界限。


0
投票

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

  moveRight.rotation = -180f
© www.soinside.com 2019 - 2024. All rights reserved.