简体   繁体   English

在Android中旋转画布时图像被剪裁?

[英]Image get clipped while rotating canvas in android?

I am developing an application in which I am rotating image using following code. 我正在开发一个应用程序,我在其中使用以下代码旋转图像。

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(-10, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2); 
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);

mImageView.setImageBitmap(bmResult);

Taken from Rotating a drawable in Android 取自Android中的旋转绘图

but the image get clipped from edges something similar to this 但是图像会从边缘剪裁出类似于此的东西

I have searched the internet but I cannot resolve it. 我搜索过互联网,但无法解决。 I know that i have to increase the size of bmResult.I also tried doing that but still some edges got clipped . 我知道我必须增加bmResult的大小。我也试过这样做,但仍有一些边缘被修剪。

If any body can give me clue or solve the problem following is my Imageview xml code 如果有任何身体可以给我提供线索或解决问题,我的Imageview xml代码

<ImageView
            android:id="@+id/img"
            android:layout_width="45dp"
            android:layout_height="45dp" >

</ImageView>

Thanks 谢谢

One alternative that worked for me was to set the margins to a negative number: 一个对我有用的替代方案是将边距设置为负数:

    params.setMargins(-350, -350, -350, -350);
    final ImageView img1 = new ImageView(this);
    img1.setScaleType(ScaleType.FIT_CENTER);
    linearLayoutBackground.addView(img1, params);

Probably not a best practice, but it did the trick in a pinch. 可能不是最佳做法,但它在紧要关头做了诀窍。

I was having the same problem, this code worked out for me. 我遇到了同样的问题,这段代码对我有用。 Try it. 试试吧。

Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.image2); 
        Matrix mat = new Matrix();
        mat.postRotate(-10);
        Bitmap bMapRotate = Bitmap.createBitmap(bmpOriginal, 0, 0, bmpOriginal.getWidth(), bmpOriginal.getHeight(), mat, true);
        mImageView.setImageBitmap(bMapRotate);

谢谢@Alan的答案我得到了解决方案,我只是在画布上旋转(10,10)x,y后绘制图像。

`tempCanvas.drawBitmap(bmpOriginal, 10, 10, null);`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM