3

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

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 .

If any body can give me clue or solve the problem following is my Imageview xml code

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

</ImageView>

Thanks

Community
  • 1
  • 1
Nitin
  • 1,966
  • 4
  • 22
  • 53

3 Answers3

1

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.

user2669161
  • 41
  • 1
  • 4
0

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
  • 1,509
  • 1
  • 16
  • 21
0

Thanks @Alan for the answer I have got the solution that I just draw the image after rotating on (10,10) x,y on canvas like.

`tempCanvas.drawBitmap(bmpOriginal, 10, 10, null);`
Nitin
  • 1,966
  • 4
  • 22
  • 53