我以如此简单的方式旋转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图像,如果有任何区别的话)。
答案 0 :(得分:25)
您是否尝试过Compat库提供的animate方法?使用动画工具
时,我也遇到了同样的问题final OvershootInterpolator interpolator = new OvershootInterpolator();
ViewCompat.animate(fab).
rotation(135f).
withLayer().
setDuration(300).
setInterpolator(interpolator).
start();
答案 1 :(得分: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();
}
答案 2 :(得分:3)
There's an entirely another approach that works flawlessly for me (the one suggested in the accepted answer produces a clipped shadow on pre-L). Create an XML drawable and wrap your FAB icon into a <rotate>
tag like this:
<?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>
Set this drawable to your FAB, and animate either its level directly or the imageLevel
property of the FAB itself; it goes from 0 to 10000. If you'd like an OvershootInterpolator
, then set toDegrees
to 90 and animate the level up to the value of 5000 so it doesn't go beyond the bounds.
答案 3 :(得分:2)
最短的方法: 顺时针旋转:
fab.animate().rotationBy(135f) // 135f = 135 degree.
逆时针旋转(复位到初始位置):
fab.animate().rotationBy(-135f) // 135f = 135 degree.
答案 4 :(得分:0)
可以通过对象动画师实现相同的操作
moveRight.rotation = -180f