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

时间:2013-02-26 07:39:24

标签: java android rotation

<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" />

我想以编程方式旋转drawable。

我该怎么做?

这是我的回调

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

        return true;
    }
}

deltaangle不超过0.1,我不确定什么是提取值。

3 个答案:

答案 0 :(得分:34)

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

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);

答案 1 :(得分:13)

这是一个很好的解决方案,可以为imageView放置一个旋转的drawable:

Drawable getRotateDrawable(final Bitmap b, final float angle) {
    final BitmapDrawable drawable = new BitmapDrawable(getResources(), b) {
        @Override
        public void draw(final Canvas canvas) {
            canvas.save();
            canvas.rotate(angle, b.getWidth() / 2, b.getHeight() / 2);
            super.draw(canvas);
            canvas.restore();
        }
    };
    return drawable;
}

用法:

Bitmap b=...
float angle=...
final Drawable rotatedDrawable = getRotateDrawable(b,angle);
root.setImageDrawable(rotatedDrawable);

另一种选择:

private Drawable getRotateDrawable(final Drawable d, final float angle) {
    final Drawable[] arD = { d };
    return new LayerDrawable(arD) {
        @Override
        public void draw(final Canvas canvas) {
            canvas.save();
            canvas.rotate(angle, d.getBounds().width() / 2, d.getBounds().height() / 2);
            super.draw(canvas);
            canvas.restore();
        }
    };
}

另外,如果你想旋转位图,但是害怕OOM,你可以使用我做过的NDK解决方案here

答案 2 :(得分:3)

由于您尝试使用Almero的Android手势探测器,我决定采用相同的方法来寻找合适的解决方案:

public class MainActivity extends Activity {

    private RotateGestureDetector mRotateDetector;
    private float mRotationDegrees = 0.f;
    private static final float ROTATION_RATIO = 2;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRotateDetector = new RotateGestureDetector(getApplicationContext(), new RotateListener());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mRotateDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

    private class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
        @Override
        public boolean onRotate(RotateGestureDetector detector) {
            mRotationDegrees -= detector.getRotationDegreesDelta();
            ImageView v = (ImageView) findViewById(R.id.imageView);
            // For NineOldAndroids library only!
            ViewHelper.setRotation(v, mRotationDegrees * ROTATION_RATIO);
            // For HONEYCOMB and later only!
            v.setRotation(mRotationDegrees * ROTATION_RATIO);
            return true;
        }
    }
}

它适用于我(我能够用双指旋转手势旋转ImageView。 注意:不要忘记选择合适的旋转方法调用。我评论了他们两个以引起你的注意。

ROTATION_RATIO只是一个倍增器,用于加速手指运动的旋转响应。 您可以为视图使用任何旋转轴(setRotation(),setRotationX()和setRotationY())方法。

要在API级别低于11(预蜂窝设备)的Android设备上启用此代码支持,您可能需要使用NineOldAndroid库。