一个需求是旋转ImageView中的src,而不旋转background。使用的是
RotateDrawable,仅修改xml的src的drawable即可
image_rotate.xml 设置
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true" // 是否可见
android:pivotX="50%" // 圆心
android:pivotY="50%" // 圆心
android:fromDegrees="0" // 起始
android:toDegrees="360" // 结束
android:drawable="@drawable/ic_launcher_round"
>
</rotate>
代码部分
// level的MAX_LEVEL为10000.(参照RotateDrawable源码)
ValueAnimator anim = ValueAnimator.ofInt(0, 10000);
private void startAnim() {
anim.setRepeatMode(ValueAnimator.RESTART);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setDuration(1500);
anim.setInterpolator(new LinearInterpolator());
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int curr = (int)animation.getAnimatedValue();
Log.d(TAG, "onAnimationUpdate: curr " + curr);
mImageView.setImageLevel(curr);
}
});
anim.start();
}
private void stopAnim() {
if (anim != null && anim.isRunning()) {
anim.end();
}
}
网友评论