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

时间:2015-04-22 19:50:15

标签: android animation image-rotation

我使用 XML 文件定义的ImageView来转动Animation

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

虽然ImageView具有getRotation() method,但它只返回已设置为图像对象的第一个值。

无论如何使用 XML动画获取当前的旋转度数?如果没有,最好的方法是什么?

1 个答案:

答案 0 :(得分:2)

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