以编程方式旋转可绘制对象或视图

2022-01-16 00:00:00 rotation android java
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/your_drawable"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />

我想以编程方式旋转可绘制对象.

I want programmatically rotate the drawable.

我该怎么做?

这是我的回调

private class RotateListener implements RotateGestureDetector.OnRotateGestureListener{
    @Override
    public boolean onRotate(MotionEvent event1, MotionEvent event2,
            double deltaAngle) {

        return true;
    }
}

deltaangle不超过0.1,我不确定提取值是多少.

The deltaangle not more than 0.1, I not sure what is the extract value.

推荐答案

以下代码围绕 ImageView 的中心旋转:

The following code rotates an ImageView around its center:

ImageView myImageView = (ImageView)findViewById(R.id.my_imageview);

AnimationSet animSet = new AnimationSet(true);
animSet.setInterpolator(new DecelerateInterpolator());
animSet.setFillAfter(true);
animSet.setFillEnabled(true);

final RotateAnimation animRotate = new RotateAnimation(0.0f, -90.0f,
    RotateAnimation.RELATIVE_TO_SELF, 0.5f, 
    RotateAnimation.RELATIVE_TO_SELF, 0.5f);

animRotate.setDuration(1500);
animRotate.setFillAfter(true);
animSet.addAnimation(animRotate);

myImageView.startAnimation(animSet);

相关文章