matrix.postRotate(90)在图像旋转中

时间:2013-05-08 09:18:42

标签: android rotation imageview

我只能旋转图像一次,当我再次点击按钮时,图像会冻结并且不会旋转。请帮我。

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());
}

1 个答案:

答案 0 :(得分: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创建了一个位图用于旋转。

希望这会有所帮助..