0

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

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

1 回答 1

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 创建了一个位图以进行旋转。

希望这可以帮助..

于 2013-05-08T09:32:55.937 回答