janki_rajput janki_rajput - 3 months ago 17
Android Question

Rotate custom view in android

I am trying to rotate an image. Rotation is perfectly working but when I get that rotated image back getting some blank space. What should I do for remove that blank space?

I have tried this code:

public static Bitmap RotateBitmap(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.setRotate(90, 0, 0);
matrix.postTranslate(source.getHeight(), 0);
// matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

Utility.tempBitmap = RotateBitmap(Utility.tempBitmap, -90);

Answer

I solved my problem by changing in view set bitmap some changes as per my requirement. using this code :

the code i used before (Gives me white space)

 public static Bitmap RotateBitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.setRotate(90, 0, 0);
    matrix.postTranslate(source.getHeight(), 0);
    // matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

Utility.tempBitmap = RotateBitmap(Utility.tempBitmap, -90);

Solution code is here:

public int setBitmap(final Bitmap bitmap) 
{
changeBitmapContrastBrightness(1, 50);
gapRect = new RectF();
dest = new Rect();
setBackgroundColor(Color.GRAY);
if (bitmap.getWidth() > bitmap.getHeight()) {
this.mbitmap = bitmap;
invalidate();
return 0;
}
this.mbitmap = bitmap;
 invalidate();
 return 1;
Comments