1

我正在使用 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我注意到有一些类似的问题,但没有一个有合适的答案。

4

2 回答 2

3

我正在使用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,您可能会发现这很有帮助。

于 2013-06-05T11:46:31.050 回答
1

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

于 2013-06-05T11:46:44.680 回答