Android 围绕指定点旋转图像不会';不行!(安卓)

Android 围绕指定点旋转图像不会';不行!(安卓),android,matrix,rotation,image-rotation,Android,Matrix,Rotation,Image Rotation,我使用postRotate(float degrees,float px,float py)旋转图像视图,将px和py设置为几个不同的值,包括(0,0)和(imgView.getHeight(),imgView.getWidth()),但它拒绝围绕中心以外的任何其他点旋转。我想知道这是否和我的重心在直线上的事实有关 我的布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:o

我使用postRotate(float degrees,float px,float py)旋转图像视图,将px和py设置为几个不同的值,包括(0,0)和(imgView.getHeight(),imgView.getWidth()),但它拒绝围绕中心以外的任何其他点旋转。我想知道这是否和我的重心在直线上的事实有关

我的布局:

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

PS我注意到有一些类似的问题,但没有一个有合适的答案

我正在使用一个自定义的
图像视图
来设置角度旋转

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

如果您使用剪贴簿,您可能会发现这很有帮助。

我使用的是一个自定义的
ImageView
来设置角度旋转

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

如果你使用剪贴簿,你可能会发现这很有帮助。

一个小小的“黑客”,你可能会根据你的操作系统,使用类似Paint.net或Gimp的东西来调整图像大小。这会使图像在另一个点上旋转。但是,如果您计划使用大量图像,那么此解决方案将毫无意义。是一个使用opengl的旋转立方体教程。

一个小小的“黑客”,您可能会使用类似Paint.net或Gimp的东西来调整图像大小,具体取决于您的操作系统。这会使图像在另一个点上旋转。但是,如果您计划使用大量图像,那么此解决方案将毫无意义。是一个使用opengl的旋转立方体教程。

它可能会给你一些线索,现在还不能对你的帖子发表评论,是的,这确实有效,但对我的情况来说并不理想,因为我无法将可能的图像的中心放在布局的中心。调整线性布局位置会改变图像旋转的位置,虽然它不会改变图像旋转的点。是的,但如果你旋转的点在0,0左右,然后在视图中重新居中,它看起来就像是在围绕中心旋转,不是吗?它可能会给你一些线索,还不能评论你的帖子,是的,这确实有效,但对我的情况并不理想,因为我无法将该图像的中心置于布局的中心。调整线性布局位置会改变图像旋转的位置,虽然它不会改变图像旋转的点。是的,但如果你旋转的点在0,0左右,然后在视图中重新居中,它看起来就像是围绕中心旋转,不是吗?谢谢,我现在就试试。不。仍然拒绝围绕中心以外的任何点旋转。哦,不!它起作用了!我不得不打电话给setAngleRotation(显然)。唯一的问题是,它会围绕原始边界剪裁图像。有没有办法阻止它这样做?我想你需要把这个图像视图放在它当前的上面。尽管覆盖了onDraw,但它的唯一布局保持不变,因此它的尺寸和视口与以前相同,这就是剪报的原因。我在一个相对较大的区域使用了这个imageView,旋转imageView没有问题,因为它有足够的空间旋转。。。我希望这是有道理的谢谢,我现在就来试试。不。仍然拒绝围绕中心以外的任何点旋转。哦,不!它起作用了!我不得不打电话给setAngleRotation(显然)。唯一的问题是,它会围绕原始边界剪裁图像。有没有办法阻止它这样做?我想你需要把这个图像视图放在它当前的上面。尽管覆盖了onDraw,但它的唯一布局保持不变,因此它的尺寸和视口与以前相同,这就是剪报的原因。我在一个相对较大的区域使用了这个imageView,旋转imageView没有问题,因为它有足够的空间旋转。。。我希望这是有道理的