2013-05-13 93 views

回答

0

嘗試:

Matrix matrix=new Matrix(); 
imageView.setScaleType(ScaleType.MATRIX); 
matrix.postRotate((float) angle, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2); 
imageView.setImageMatrix(matrix); 

它從你給的鏈接相同的答案來。

+0

是的,我看到這個,但它不會從中心點旋轉。我應該添加什麼東西?感謝您的快速回復。 – IndieBoy 2013-05-13 17:06:36

+0

@JASX發佈了一個解釋,如果你好奇... – newbyca 2013-10-15 07:26:59

3

這個怎麼樣(比goodm的回答略有不同):

public Bitmap rotateImage(int angle, Bitmap bitmapSrc) { 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 
    return Bitmap.createBitmap(bitmapSrc, 0, 0, 
      bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true); 
} 
+0

Yahhhh。它的工作。謝謝。 – IndieBoy 2013-05-13 17:47:58

+0

只是偶然發現了這個,因爲我有類似的問題!如果你能幫助我,我會在哪裏加載png圖片到這個代碼中? – 2017-06-02 17:40:35

7

的問題@ goodm的解決方案是,ImageView的可能尚未奠定了沒有導致imageView.getDrawable()的getBounds()。 width()和.height()返回0.這就是爲什麼你仍然在0,0左右旋轉的原因。解決這個問題的一個方法就是確保在使用類似這樣的佈局之後創建並應用矩陣:How to set fixed aspect ratio for a layout in Android

@ Voicu的解決方案沒問題,但它需要您直接處理可能效率低下的位圖。更好的做法可能是直接查詢圖像資源的大小,但實際上並未將其加載到內存中。我用一個實用的方法來做到這一點,它看起來像這樣:

public static android.graphics.BitmapFactory.Options getSize(Context c, int resId){ 
    android.graphics.BitmapFactory.Options o = new android.graphics.BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(c.getResources(), resId, o); 
    return o; 
} 

這將返回一個Options對象將包含實際的寬度和高度。從活動中你可以使用這樣的:

ImageView img = (ImageView)findViewById(R.id.yourImageViewId); 
Options o = getSize(this, R.drawable.yourImage); 
Matrix m = new Matrix(); 
m.postRotate(angle, o.outWidth/2, o.outHeight/2); 
img.setScaleType(ScaleType.MATRIX); 
img.setImageMatrix(m); 
2

,這是爲我工作:

RotationAnimatin anim= new RotationAnimation(0f,350f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 

//然後設置插,持續時間和RepeatCount

yourImageView.startAnimation(anim);

+0

它是RotateAnimation而不是RotationAnimation – 2017-02-09 13:57:45

0

嘗試此

gestureCenterPoint.x =(int)(imageView.getPaddingLeft()+ matrix瓦爾斯[Matrix.MTRANS_X]); gestureCenterPoint.x + =(origonalWidth/2)*(matrixVals [Matrix.MSKEW_X]); gestureCenterPoint.x + =(origonalWidth/2)*(matrixVals [Matrix.MSCALE_X]);

gestureCenterPoint.y =(int)(imageView.getPaddingTop()+ matrixVals [Matrix.MTRANS_Y]); gestureCenterPoint.y + =(origonalHeight/2)*(matrixVals [Matrix.MSKEW_Y]); gestureCenterPoint.y + =(origonalHeight/2)*(matrixVals [Matrix.MSCALE_Y]);