2013-05-08 79 views
0

我只能旋轉圖像一次,當我再次點擊按鈕時,圖像凍結,不旋轉。請幫幫我。matrix.postRotate(90)在圖像旋轉

try{ 
    //Bitmap bMap; 

    //Get ImageView from layout xml file 
    img = (ImageView) findViewById(R.id.imageView01); 

    //Decode Image using Bitmap factory. 
    Bitmap bMap = BitmapFactory.decodeFile(selectedImagePath); 

    //Create object of new Matrix. 
    Matrix matrix = new Matrix(); 

    //set image rotation value to 90 degrees in matrix. 
    matrix.postRotate(90); 

    //Create bitmap with new values. 
    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), matrix, true); 

    //put rotated image in ImageView. 
    img.setImageBitmap(bMapRotate); 

    Context context = getApplicationContext(); 
    CharSequence text = "Image Rotated" ; 
    int duration = Toast.LENGTH_SHORT; 

    Toast toast = Toast.makeText(context, text, duration); 

    toast.show(); 

}catch (Exception e) {  
    e.printStackTrace(); 
    displayExceptionMessage(e.getMessage()); 
} 
+0

在這裏,可以旋轉的下一個時間已旋轉的圖像,而不是原來的圖像,它不會在旋轉的原始圖像的任何影響試。 – Hardik4560 2013-05-08 09:23:41

+0

我必須從imageView中獲取圖像並再次旋轉它嗎?但如何..你可以請讓我看看,因爲我是Android新手。謝謝 – chai 2013-05-08 09:25:30

+0

這取決於你到底想要達到什麼目標,從imageView獲取圖像將是一個更好的選擇。 – Hardik4560 2013-05-08 09:29:21

回答

2

試試這個

try { 
    //Bitmap bMap; 

    //Get ImageView from layout xml file 
    img = (ImageView) findViewById(R.id.imageView01); 

    //Decode Image using Bitmap factory. 
    //Bitmap bMap = BitmapFactory.decodeFile(selectedImagePath); 
    Bitmap bMap = Bitmap.createBitmap(img.getDrawingCache()); 

    //Create object of new Matrix. 
    Matrix matrix = new Matrix(); 

    //set image rotation value to 90 degrees in matrix. 
    matrix.postRotate(90); 
    matrix.postScale(0.5f, 0.5f); 

    int newWidth = bMap.getWidth()/2; 
    int newHeight = bMap.getHeight()/2; 

    //Create bitmap with new values. 
    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, newWidth, newHeight, matrix, true); 

    //put rotated image in ImageView. 
    img.setImageBitmap(bMapRotate); 

    Context context = getApplicationContext(); 
    CharSequence text = "Image Rotated" ; 
    int duration = Toast.LENGTH_SHORT; 

    Toast toast = Toast.makeText(context, text, duration); 

    toast.show(); 

} catch (Exception e) {  
    e.printStackTrace(); 
    displayExceptionMessage(e.getMessage()); 
} 

西隧我在我們的代碼所做的是我已經創建了從ImageView的旋轉的位圖。

希望這有助於..

+0

哇..這工作..謝謝你這麼多..我真的很感激這個幫助..順便說一句你可以告訴我如何執行圖像加載到imageView作物行動?這是改進OCR的功能之一,因爲用戶只會選擇文本區域而忽略不包含任何文本的其他部分。非常感謝你 – chai 2013-05-08 09:39:37

+0

你的意思是你想裁剪或縮放圖像,然後將其插入到ImageView中?旋轉後, – bakriOnFire 2013-05-08 09:47:45

+0

裁剪圖像。 – chai 2013-05-08 09:49:00