2013-05-08 238 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