lazybios

使用RotateAnimation实现图片按钮旋转

定义ImageButton
<ImageButton
    android:id="@+id/btn"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:background="@null"
    android:scaleType="fitXY"
  android:src="@drawable/icon"/>
ImageButton imageBtn = (ImageButton) findViewById(R.id.btn);
/res/anim文件夹下定义旋转动画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="359"  
        android:duration="300"  
        android:repeatCount="-1"  
        android:pivotX="50%"  
        android:pivotY="50%"
        android:interpolator="@android:anim/linear_interpolator"
        />  
</set> 

设置补间动画变化率

Animation btnAnim = AnimationUtils.loadAnimation(this, R.anim.rotate);  
LinearInterpolator linear = new LinearInterpolator();  
btnAnim.setInterpolator(linear);  

interpolator_list

根据参数:上面的动画即表示以按钮的中心位置由0的位置开始,顺时针进行359度的旋转。这里的旋转可以使用下面公式求得:

旋转速度 = android:duration/(android:toDegrees-android:fromDegrees)
加载动画与清除动画
# 开始旋转
if (btnAnim != null) {
  imageBtn.startAnimation(btnAnim); 
}

# 停止旋转
imageBtn.clearAnimation();

-完-

参考引用

微信关注「日拱一卒」公众号