How to rotate an ImageView

Toon

New Member
Licensed User
What is the easiest way to rotate an Imageview using code (without using any non-default libraries) ? :) Thanks for reading!
 

Djembefola

Active Member
Licensed User
What is the easiest way to rotate an Imageview using code (without using any non-default libraries) ? :)

B4X:
Sub RotateView(v As View, bmp As Bitmap, degrees As Float)
    Dim c As Canvas
   Dim r As Rect
   c.Initialize(v)
   r.Initialize(0,0,v.Width,v.Height)
   c.DrawRect(r,Colors.transparent,True,0)
   c.DrawBitmapRotated(bmp,Null,r,degrees)
   v.Invalidate
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Interesting piece of code

but

I end up with a 45 degrees rotated image where the corners are cut off since they end up outside the imageview's boundaries ?
(the image size grows due to the rotation)

I guess it needs to be rotated, scaled, recentered and then drawn to the canvas ?
 
Last edited:
Upvote 0

Mark Read

Well-Known Member
Licensed User
Easier is with the RSImageProcessing Library

B4X:
Sub ShowImage(bmp As Bitmap)
If bmp.width>bmp.height Then            'Image is landscape, rotate
      Dim rbmp As RSImageProcessing
      mBmp=rbmp.rotateBitmap(bmp, -90)

end if

.....rest of code
 
Upvote 0

sorex

Expert
Licensed User
I sorted it out.

my view was square and the loaded image rectangular.

having the source image smaller than the destination view helps ;)

I just added 1 line to the sub code to clean the mess when doing several rotations in a row. (it left trails as it didn't erase the destination bitmap/canvas)
 
Upvote 0

DarkAngel

Member
Licensed User
Hello,

How can i rotate a BitmapDrawable?
Or converte from bitmapdrawable do bitmap to make the rotation and convert back do bitmapdrawable?
Is it possible?
 
Upvote 0
Top