如何将位图旋转90度

mur*_*rli 108 android

在android中有一个声明 canvas.drawBitmap(visiblePage, 0, 0, paint);

当我添加时canvas.rotate(90),没有任何效果.但如果我写

canvas.rotate(90)
canvas.drawBitmap(visiblePage, 0, 0, paint);
Run Code Online (Sandbox Code Playgroud)

我没有得到位图.那么我做得对不对?

小智 237

你也可以试试这个

Matrix matrix = new Matrix();

matrix.postRotate(90);

Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg, width, height, true);

Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用旋转的图像在imageview中设置

imageView.setImageBitmap(rotatedBitmap);
Run Code Online (Sandbox Code Playgroud)

  • 导入android.graphics (6认同)
  • 这使用了大量的内存.对于大位图,由于内存中位图的多个副本,它可能会产生问题. (4认同)
  • 我认为对于你想要的scaledBitmap(bitmapOrg,宽度,高度,true) (2认同)
  • 哪个矩阵导入?android.graphics还是android.opengl? (2认同)

Arv*_*vis 160

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
Run Code Online (Sandbox Code Playgroud)

要从资源中获取位图:

Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img);
Run Code Online (Sandbox Code Playgroud)

  • @ShishirGupta未经过测试但由android docs测试:`如果源位图是不可变的并且请求的子集与源位图本身相同,则返回源位图并且不创建新的位图. (4认同)

com*_*m1x 26

Kotlin短暂延期

fun Bitmap.rotate(degrees: Float): Bitmap {
    val matrix = Matrix().apply { postRotate(degrees) }
    return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}
Run Code Online (Sandbox Code Playgroud)

用法:

val rotatedBitmap = bitmap.rotate(90F) // value must be float
Run Code Online (Sandbox Code Playgroud)


Ars*_*war 12

下面是在android中旋转或重新调整图像大小的代码

public class bitmaptest extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        LinearLayout linLayout = new LinearLayout(this);

        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.android);

        int width = bitmapOrg.width();
        int height = bitmapOrg.height();
        int newWidth = 200;
        int newHeight = 200;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                          width, height, matrix, true);

        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        ImageView imageView = new ImageView(this);

        // set the Drawable on the ImageView
        imageView.setImageDrawable(bmd);

        // center the Image
        imageView.setScaleType(ScaleType.CENTER);

        // add ImageView to the Layout
        linLayout.addView(imageView,
                new LinearLayout.LayoutParams(
                      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
                )
        );

        // set LinearLayout as ContentView
        setContentView(linLayout);
    }
}
Run Code Online (Sandbox Code Playgroud)

您还可以查看此链接以获取详细信息:http://www.anddev.org/resize_and_rotate_image_-_example-t621.html


Goo*_*ian 8

使用JavacreateBitmap()方法可以通过学位。

Bitmap bInput /*your input bitmap*/, bOutput;
float degrees = 45; //rotation degree
Matrix matrix = new Matrix();
matrix.setRotate(degrees);
bOutput = Bitmap.createBitmap(bInput, 0, 0, bInput.getWidth(), bInput.getHeight(), matrix, true);
Run Code Online (Sandbox Code Playgroud)


小智 6

默认情况下,旋转点是Canvas的(0,0)点,我的猜测是您可能希望围绕中心旋转它.我这样做了:

protected void renderImage(Canvas canvas)
{
    Rect dest,drawRect ;

    drawRect = new Rect(0,0, mImage.getWidth(), mImage.getHeight());
    dest = new Rect((int) (canvas.getWidth() / 2 - mImage.getWidth() * mImageResize / 2), // left
                    (int) (canvas.getHeight()/ 2 - mImage.getHeight()* mImageResize / 2), // top
                    (int) (canvas.getWidth() / 2 + mImage.getWidth() * mImageResize / 2), //right
                    (int) (canvas.getWidth() / 2 + mImage.getHeight()* mImageResize / 2));// bottom

    if(!mRotate) {
        canvas.drawBitmap(mImage, drawRect, dest, null);
    } else {
        canvas.save(Canvas.MATRIX_SAVE_FLAG); //Saving the canvas and later restoring it so only this image will be rotated.
        canvas.rotate(90,canvas.getWidth() / 2, canvas.getHeight()/ 2);
        canvas.drawBitmap(mImage, drawRect, dest, null);
        canvas.restore();
    }
}
Run Code Online (Sandbox Code Playgroud)


Gnz*_*zlt 6

我会进一步简化comm1xKotlin 扩展函数

fun Bitmap.rotate(degrees: Float) =
    Bitmap.createBitmap(this, 0, 0, width, height, Matrix().apply { postRotate(degrees) }, true)
Run Code Online (Sandbox Code Playgroud)