且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Android 中旋转可绘制对象

更新时间:2023-02-01 21:06:05

需要使用Bitmap和Canvas Class函数来准备drawable:

You need to use Bitmap and Canvas Class functions to prepare drawable:

Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.image2);
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmResult); 
tempCanvas.rotate(90, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);

mImageView.setImageBitmap(bmResult);

在此代码示例中,图像中心发生了 90 度旋转.

In this code sample rotation for 90 degrees over image center occurs.