簡體   English   中英

圍繞指定點旋轉圖像不起作用! (機器人)

[英]Rotating an image around a specified point doesn't work! (Android)

我正在使用postRotate(浮點數,浮點數px,浮點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我注意到有一些類似的問題,但沒有一個有合適的答案。

我正在使用自定義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,你會發現它很有幫助。

輕微的“黑客”,您可以使用Paint.net或Gimp之類的東西調整圖像大小,具體取決於您的操作系統。 這將使圖像看起來在另一點旋轉。 如果您計划使用大量圖像,此解決方案將毫無意義。 是一個使用opengl的旋轉立方體教程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM