【问题标题】:Get current RotationAngle while Animation is running on Android在 Android 上运行动画时获取当前的 RotationAngle
【发布时间】:2015-04-22 19:50:15
【问题描述】:

我正在使用 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 ,但它只返回已设置为图像对象的第一个值。

有没有办法通过使用XML 动画 来获得当前的旋转度数?如果没有,最好的方法应该是什么?

【问题讨论】:

  • 使用 ValueAnimator/ObjectAnimator

标签: android animation image-rotation


【解决方案1】:
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

【讨论】:

  • 我应该在哪里为这个 ObjectAnimator 定义我的 ImageView?
  • 我刚刚实现了 CustomImageView。应用程序正在运行,正在显示图像,但动画没有在我的 Fragment 中开始。 CustomImageView agulhaV = (CustomImageView) l.findViewById(R.id.agulha_volt); agulhaV.startAnimation(3000);
  • 它在我这边运行得很好。我将制作一个快速示例项目。
  • 我放了一个简单项目的链接,试试看。它工作得很好:)
  • 谢谢!我希望我可以投票两次。因此,一切都几乎工作正常,因为我一直在 Logcat 获得 Rotation: 0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多