2012-06-13 56 views
0

我仍然在一段代碼的工作,我想對圖像進行順時針和逆時針使用按鈕旋轉,我想下面的代碼:旋轉圖像的onclick問題

case (R.id.clock): 
      matrix.postRotate(90); 
      matrix.postRotate(getDegreesFromRadians(angle), mid.x, mid.y); 

     break; 

     case (R.id.anticlock): 
      float degrees = 0; 
      degrees = degrees-10; 
      matrix.postRotate(degrees); 
      break; 

它是工作,但不正確的方法,首先我要按下按鈕,然後點擊imageview旋轉圖像。任何幫助?

我問的問題在這裏也Android Rotate image ontouch 但現在我使用按鈕

+0

這個代碼是不完整的。 'imageview.setOnclickListener()'發生了什麼? – ariefbayu

+0

@ariefbayu你可以找到我更新的答案,我已經提到了鏈接, – Numair

+0

如果是這樣的話,你叫'view.setImageMatrix(矩陣);'在按鈕點擊? – ariefbayu

回答

2

這就是我通常做它想要的。我創建靜態的Util類用這種方法:

public static Bitmap rotate(Bitmap bitmap, int degree) { 
    int w = bitmap.getWidth(); 
    int h = bitmap.getHeight(); 

    Matrix mtx = new Matrix(); 
    mtx.postRotate(degree); 

    return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true); 
} 

,然後,我用這樣的:

case 21: // ROTATE LEFT 
    ImageProcessActivity.processedBitmap = ImageUtil.rotate(ImageProcessActivity.processedBitmap, -90); 
    d = new BitmapDrawable(ImageProcessActivity.processedBitmap); 
    ImageProcessActivity.selectedImage.setImageDrawable(d); 

break; 
case 22: // ROTATE RIGHT 
    ImageProcessActivity.processedBitmap = ImageUtil.rotate(ImageProcessActivity.processedBitmap, 90); 
    d = new BitmapDrawable(ImageProcessActivity.processedBitmap); 
    ImageProcessActivity.selectedImage.setImageDrawable(d); 
break; 

你看,我的代碼,我正在顯示與ImageView的分離圖像。這樣,我可以輕鬆地操縱圖像,而不需要ImageView的哈希。正如名稱所解釋的,ImageView僅用於查看圖像。不是操縱的來源。

+0

它終於奏效了,謝謝 – Numair

0

您必須通知您的視圖已更改,以便android呈現它。意見沒有渲染的幀率,所以做這樣的改變後的矩陣:

yourView.invalidate(); 

應工作