【问题标题】:Rotating an image around a specified point doesn't work! (Android)围绕指定点旋转图像不起作用! (安卓)
【发布时间】:2013-06-05 11:35:31
【问题描述】:

我正在使用 postRotate(float degree, float px, float py) 旋转 ImageView,将 px 和 py 设置为几个不同的值,包括 (0,0) 和 (imgView.getHeight(),imgView.getWidth() ) 但它拒绝围绕中心以外的任何其他点旋转。我想知道这是否与我的重心位于 LinearLayout 中心这一事实有关?

我的布局:

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

我如何旋转图像:

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

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

【问题讨论】:

    标签: android matrix rotation image-rotation


    【解决方案1】:

    我正在使用自定义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();
        }
    }
    

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

    【讨论】:

    • 谢谢,我现在就试试看。
    • 不。仍然拒绝围绕中心以外的任何其他点旋转。
    • 哦不!有用!我只需要调用 setAngleRotation (显然)。唯一的问题是,它会在原始边界周围裁剪图像。任何想法如何阻止它这样做?
    • 我猜你需要把这个图像视图放在它当前的顶部。即使你覆盖了 onDraw,它的 onLayout 仍然是一样的,所以它的尺寸和视口和以前一样,这就是剪报的原因。我在一个跨越更大区域的 RelativeLayout 中使用了这个 imageView,旋转 ImageView 不是问题,因为它有足够的空间来旋转......我希望它是有意义的。 :)
    【解决方案2】:

    轻微的“hack”,您可以使用 Paint.net 或 Gimp 等工具调整图像大小,具体取决于您的操作系统。这将使图像看起来在另一个点上旋转。但是,如果您打算使用大量图像,则此解决方案将毫无意义。 This是一个使用opengl的旋转立方体教程。

    【讨论】:

    • 它可能会给你一些线索,还不能评论你的帖子,lol
    • 是的,这确实有效,但它不适合我的情况,因为我不能将那个可能的图像的中心放在布局的中心。
    • 调整线性布局位置会改变图像旋转的位置,但不会改变图像旋转的点。
    • 是的,但如果你旋转大约 0,0 然后在视图中重新定位它,它看起来就像是围绕中心旋转,不是吗?
    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    相关资源
    最近更新 更多