2013-06-05 47 views
1

我正在使用postRotate(float度,float px,float py)旋轉ImageView,將px和py設置爲幾個不同的值,包括(0,0)和(imgView.getHeight(),imgView.getWidth ()),但它拒絕圍繞中心以外的任何其他點旋轉。我想知道這是否與我的引力位於LinearLayout的中心有關?圍繞指定點旋轉圖像不起作用! (Android)

我的佈局:

<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我注意到有一些類似的問題,但他們都沒有合適的答案。

回答

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,你可能會發現有幫助。

+0

謝謝,我現在就放棄這一點。 –

+0

沒有。仍然拒絕圍繞中心以外的任何其他點旋轉。 –

+0

哦,不!有用!我只需要調用setAngleRotation(顯然)。唯一的問題是,它會在原始邊界周圍剪切圖像。任何想法如何阻止它這樣做? –

1

一個輕微的「黑客」,你可能會調整圖像大小使用像Paint.net或Gimp,取決於您的操作系統。這將使圖像看起來在另一點上旋轉。如果您計劃使用大量圖像,則此解決方案將毫無意義。 This是一個使用opengl的旋轉魔方教程。

+0

它可能會給你一些線索,不能評論你的帖子呢,大聲笑 – Lunchbox

+0

是的,這是行得通的,但對我的情況並不理想,因爲我不能在我的中心擁有那個可能形象的中心。佈局。 –

+0

調整線性佈局位置會改變圖像旋轉的位置,但它不會改變圖像旋轉的點。 – Lunchbox