【问题标题】:Programmatically rotate drawable or view以编程方式旋转可绘制对象或视图
【发布时间】:2013-02-26 07:39:23
【问题描述】:
<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" />

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

我该怎么做?

这是我的回调

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

        return true;
    }
}

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

【问题讨论】:

标签: java android rotation


【解决方案1】:

以下代码围绕其中心旋转 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);

【讨论】:

  • 这很适合修复,但是当我检测到旋转手势时我想要的是动态的
  • final RotateAnimation animRotate = new RotateAnimation(0.0f, 360.0f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
【解决方案2】:

这是为 imageView 放置旋转可绘制对象的一个​​很好的解决方案:

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

【讨论】:

  • 非常不推荐的解决方案!!因为您正在重新实现“绘制”功能,所以每次需要更新可绘制对象时,它都会被旋转。
  • @Corbella 有道理,那么这里有什么问题?
【解决方案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 设备(Honeycomb 之前的设备)上支持此代码,您可能需要使用 NineOldAndroid 库。

【讨论】:

    猜你喜欢
    • 2014-03-26
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 2011-10-19
    • 2021-11-06
    • 1970-01-01
    相关资源
    最近更新 更多