Rotating an image around a specified point doesn&#

2019-02-20 14:41发布

I'm rotating an ImageView with postRotate(float degrees, float px, float py), setting px and py to a few differnt values including (0,0) and (imgView.getHeight(),imgView.getWidth()) but it refuses to rotate around any other point than the center. I'm wondering whether it's got anything to do with the fact that my gravity is center in the LinearLayout?

My layout:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">

    <ImageView
        android:id="@+id/imageTraj"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/impactangle" />

How I'm rotating the image:

matrix.postRotate(degrees,imageView.getHeight(),imageView.getWidth());
imageView.setImageBitmap(Bitmap.createBitmap(imageScaled, 0, 0,
                imageScaled.getWidth(), imageScaled.getHeight(), matrix, true));

PS I've noticed there are a few similar questions, but none of them have suitable answers.

2条回答
Root(大扎)
2楼-- · 2019-02-20 15:11

I am using a custom ImageView where I set the angle rotation.

public class CompassImage extends ImageView {
    private float angleRotation;

    public CompassImage(Context context) {
        super(context);
    }

    public CompassImage(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CompassImage(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setAngleRotation(float angleRotation) {
        this.angleRotation = angleRotation;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Rect clipBounds = canvas.getClipBounds();
        canvas.save();
        canvas.rotate(angleRotation, clipBounds.exactCenterX(), clipBounds.exactCenterY());
        super.onDraw(canvas);
        canvas.restore();
    }
}

If you play around with clipBounds you may find that helpful.

查看更多
家丑人穷心不美
3楼-- · 2019-02-20 15:21

A slight "hack", you could probably adjust the image size using something like Paint.net or Gimp, depending on your OS. That would make the image appear to spin on another point. This solution would be pointless if you are planning on using a lot of images though. This is a spinning cube tutorial using opengl.

查看更多
登录 后发表回答