Android Question rotate a non square bitmap with transparent parts

Toni

Member
Licensed User
Longtime User
Hello,
I tried out everything I found in the forum but no code example works for me.
I flipped the bitmap of an non square and partly transparent imageview and it works fine. I've tried to translate this to
a rotating sub to do in the same manor, but find no working solution. I tried also out a DestRec with dip values thatare
explicit larger than a 45° rotated bitmap of my imageview, but at the end even when I change width and height for the
imageview the dimension stay as initial values. For rotating square bitmaps the code works fine. I know that I have to
recalculate left and top for non square bitmaps but that should be a peace of cake, even for me :rolleyes:
Any hints please? Thanks in advance!

rotating a bitmap:
Sub Gesture_onPointerDown(ptrIndex As Int, PID As Int, MotionEvent As Object)
    ' flipp image works fine
    Dim i As ImageView
    i=Sender
    Dim j As Int=i.tag  

    Dim vert As Boolean, hor As Boolean
    Dim bmp As Bitmap=i.bitmap
    Dim bmpflip As Bitmap
    bmpflip.InitializeMutable(bmp.width, bmp.height)
    Dim cv As Canvas
    cv.Initialize2(bmpflip)  
    cv.DrawColor(Colors.ARGB(0, 0, 0, 0))                                   ' 0 means fully transparent
    If bmp.Width>bmp.Height Then
        vert=True
        hor=False
    Else
        vert=False
        hor=True
    End If
    Dim DestRect As Rect
    DestRect.Initialize(0dip, 0dip, bmp.Width, bmp.height)
    cv.DrawBitmapFlipped(bmp, Null, DestRect, vert, hor) 
    i.Bitmap=bmpflip
    i.Invalidate
End Sub

Sub Gesture_onDoubleTap(X As Float, Y As Float, MotionEvent As Object)
' rotating  the bitmap works not
    Dim i As ImageView
    i=Sender
    Dim j As Int=i.tag   
    Dim bmp As Bitmap=i.bitmap
    Dim bmprot As Bitmap
    bmprot.InitializeMutable(bmp.width, bmp.height)
    Dim cv As Canvas
    cv.Initialize2(bmprot)
    cv.DrawColor(Colors.ARGB(0, 0, 0, 0))                           ' 0 means fully transparent
    Dim DestRect As Rect
    DestRect.Initialize(0dip, 0dip, bmp.Width, bmp.height)            ' also tried changing width and height, 210dip x 210dip or added a multiplyer to  width and height
    cv.DrawBitmaprotated(bmp, Null, DestRect, 90)
    i.Bitmap=bmprot                                           ' also tried setLayoutAnimated(0, i.left, i.top, new width, new height)
    i.Invalidate
end sub
 

klaus

Expert
Licensed User
Longtime User
You can make an animated rotation with an additional ImageView for the rotation.

B4X:
' --- Coherent rotation ---
Sub Gesture_onDoubleTap(X As Float, Y As Float, MotionEvent As Object)
    Dim imv As ImageView = Sender
    Dim j As Int = imv.Tag

    Dim bmp As B4XBitmap = imv.Bitmap
    Dim rotated As B4XBitmap = RotateBitmapJO(bmp, 90)
   
    Private imvd As ImageView    
    imvd.Initialize("")
    imvd.Gravity = Gravity.FILL
    imvd.Bitmap = bmp
    imv.Parent.As(B4XView).AddView(imvd, imv.Left, imv.Top, imv.Width, imv.Height)
    
    imv.Visible = False
    imv.Bitmap = rotated
    
    For a = 0 To 90 Step 5
        imvd.As(B4XView).Rotation = a
        Sleep(20)
    Next
    
    imv.Visible = True
    imvd.Visible = False
    imvd.RemoveView
    
    Dim tmp As Int = BaseW(j)
    BaseW(j) = BaseH(j)
    BaseH(j) = tmp

    ' applies the current scale
    Dim w As Int = Round(BaseW(j) * Scale(j))
    Dim h As Int = Round(BaseH(j) * Scale(j))
    Dim cx As Float = imv.Left + imv.Width / 2
    Dim cy As Float = imv.Top + imv.Height / 2
    Dim newLeft As Int = Round(cx - w / 2)
    Dim newTop As Int = Round(cy - h / 2)

    imv.SetLayoutAnimated(0, newLeft, newTop, w, h)
End Sub
 

Attachments

  • ProjectRotate4.zip
    38.2 KB · Views: 8
  • Like
Reactions: zed
Upvote 0
Top