动画在 Android 上 运行 时获取当前 RotationAngle

Get current RotationAngle while Animation is running on Android

我正在使用 XML 文件定义的 Animation 旋转 ImageView

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <rotate
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="90"
        android:fillAfter="true" />    
</set>

尽管 ImageViewgetRotation() method ,它只是 returns 已设置到图像对象的第一个值。

有没有办法通过XML动画获取当前的旋转度数?如果没有,最好的方法是什么?

public class CustomImageView extends ImageView {

    private ObjectAnimator rotationAnimator;

    public CustomImageView(Context context) {
        super(context);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setRotation(float rotation) {
        super.setRotation(rotation);
        // Do something
    }

    public synchronized void startAnimation(int animationDuration) {

        if (rotationAnimator == null || !rotationAnimator.isRunning()) {
            Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
            Keyframe kf2 = Keyframe.ofFloat(0.5f, 180f);
            Keyframe kf1 = Keyframe.ofFloat(1f, 360f);

            PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);
            rotationAnimator = ObjectAnimator.ofPropertyValuesHolder(this, pvhRotation);
            rotationAnimator.setRepeatCount(ObjectAnimator.INFINITE);
            rotationAnimator.setInterpolator(new LinearInterpolator());
            rotationAnimator.setDuration(animationDuration);
            rotationAnimator.start();
        }
        else {
            // Already running
        }
    }

版本 2,因为您遇到问题

public class CustomImageView extends ImageView {

    private ObjectAnimator rotationAnimator;
    private float myRotation;

    public CustomImageView(Context context) {
        super(context);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setMyRotation(float rotation) {
        this.myRotation = rotation;
        Log.d("CustomImage", "Rotation: " + rotation);
        invalidate();
    }

    public synchronized void startAnimation(int animationDuration) {

        if (rotationAnimator == null || !rotationAnimator.isRunning()) {
            Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
            Keyframe kf2 = Keyframe.ofFloat(0.5f, 180f);
            Keyframe kf1 = Keyframe.ofFloat(1f, 360f);

            PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("myRotation", kf0, kf1, kf2);
            rotationAnimator = ObjectAnimator.ofPropertyValuesHolder(this, pvhRotation);
            rotationAnimator.setRepeatCount(ObjectAnimator.INFINITE);
            rotationAnimator.setInterpolator(new LinearInterpolator());
            rotationAnimator.setDuration(animationDuration);
            rotationAnimator.start();
        }
        else {
            // Already running
        }
    }

    public synchronized void stopAnimation() {
        if (rotationAnimator != null) {
            rotationAnimator.cancel();
            rotationAnimator = null;
        }
    }

    public synchronized boolean getAnimationRunning() {
        return rotationAnimator != null && rotationAnimator.isRunning();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.rotate(myRotation, getWidth() / 2.0f, getHeight() / 2.0f);

        super.onDraw(canvas);
    }
}

示例 logcat 输出:

04-22 22:48:55.475  16341-16341/animation.example.com.animationdemo D/CustomImage﹕ Rotation: 358.44
04-22 22:48:55.490  16341-16341/animation.example.com.animationdemo D/CustomImage﹕ Rotation: 0.48000813
04-22 22:48:55.505  16341-16341/animation.example.com.animationdemo D/CustomImage﹕ Rotation: 2.4
04-22 22:48:55.525  16341-16341/animation.example.com.animationdemo D/CustomImage﹕ Rotation: 4.44

我已经将代码打包成一个简单的项目:LINK