2013-07-18 33 views
0

我想在RemoteViews中旋轉位圖。但是,當我使用Matrix.setRotate方法或任一種Matrix.postRotate方法時,Bitmap會變得更加粗糙。這是我用來完成任務的代碼。android Matrix.setRotate縮放我的位圖

Bitmap bMap = BitmapFactory.decodeResource(context.getResources(), R.drawable.arrow); 
Matrix m = new Matrix(); 
m.setRotate((float) 0, bMap.getWidth()/2, bMap.getHeight()/2); 
bMap = Bitmap.createBitmap(bMap,0,0, bMap.getWidth(),bMap.getHeight(),m, true); 

RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.speedometer); 
remoteView.setImageViewBitmap(R.id.speedoNeedle, bMap); 

這裏是原始佈局文件中的XML:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="136px" 
android:layout_height="136px" 
> 

<ImageView 
     android:layout_width="136px" 
     android:layout_height="136px" 
     android:src="@drawable/dial" 
     /> 
<ImageView 
     android:id="@+id/speedoNeedle" 
     android:layout_width="9px" 
     android:layout_height="78px" 
     android:layout_marginLeft="63px" 
     android:layout_marginTop="27px" 
     android:rotation="-138" 
     android:src="@drawable/arrow" /> 
</RelativeLayout> 

如果我的旋轉值設置爲零或註釋掉m.setRotate((浮點)0,bMap.getWidth()/ 2,bMap.getHeight()/ 2)位圖顯示正確。

enter image description here

如果我設置旋轉值138,我得到這樣的: 你可以勉強看到針頭。

enter image description here

這裏是在184價值的截圖:

enter image description here

針是不是在276

最大值我在做什麼,甚至可見錯誤?我該如何解決它?

在此先感謝。

回答

1

那麼,從第一個角度來看,你已經將speedoNeedle ImageView的寬度設置爲9px,這對於箭頭位圖的整個角度範圍來說似乎都不夠...不是這樣問題?據我所知,你正在旋轉整個箭頭位圖的中心,因此旋轉位圖的尺寸會有所不同...首先,我會嘗試將speedoNeedle ImageView的寬度和高度設置爲相同的數字。

+0

這是有道理的,我會嘗試一下,讓你知道,如果這樣的作品。謝謝。 – dherrin79

+0

是的,這是正確的想法。我試過了,但效果還是不錯,但仍然存在一些縮放問題,我不得不使針的尺寸適當,以便在位圖旋轉45度時對角線長度小於刻度盤的總寬度。我認爲這將起作用。感謝您的輸入。 – dherrin79

+0

我上面的評論並不完美,但我仍然相信接受的答案是正確的想法。如果任何人有任何想法,將不勝感激。 – dherrin79