Android Question DrawBitmap does not appear to work correctly

Gary Milne

Active Member
Licensed User
I'm doing a small preview pane on my app as shown in the picture below.

The image is copied from the white box where it says Baseline and shown in the preview pane at the top of the screen at 2x the scale. Problem is that the preview image is never correct. The red box shows approx what is appearing in the preview box but that is not what is being referenced. Any help appreciated as I've been banging my head on the wall for hours with this one. Code snippet below.

DrawBitmap.png


Here is the relevant code:
B4X:
Sub Preview_Image

    Clear_Panel("pnlPreview")
   
    Dim adjy As Int = -4%y
    Dim adjx As Int = 6%x
   
    'Setup the source Rectangle - It references a space 5% in height and 20% in width from the green Baseline
    Dim SrcRect As Rect
    SrcRect.Initialize(40%x, SP.Baseline - 2.5%y, 60%x, SP.Baseline + 2.5%y )
    cvsVR.DrawRectRotated(SrcRect, Colors.White, False, 2dip,0)       

    'Setup the destination rectangle
    Dim DstRect As Rect
    DstRect.Initialize(pnlPreview.Left, pnlPreview.Top, pnlPreview.Left + pnlPreview.Width, pnlPreview.top + pnlPreview.Height)
   
    'Draw the intended destination rectangle on the screen around the pnlPreview - In White
    cvsVR.DrawRect(DstRect, Colors.White, False, 2dip)   
   
    'Draw the approximate ACTUAL source rectangle on the screen - IN RED
    Dim SrcRect2 As Rect
    SrcRect2.Initialize(40%x + adjx, SP.Baseline - 2.5%y + adjy, 60%x + adjx, SP.Baseline + 2.5%y + adjy )
    cvsVR.DrawRect(SrcRect2, Colors.Red, False, 2dip)   
   
    'Because we are putting it into a different Panel we need a different rectangle using the coordinate base of the destination panel.
    Dim DstRect2 As Rect
    DstRect2.Initialize(0, 0, pnlPreview.Width, pnlPreview.Height) ' same as DstRect2.Initialize(0, 0, 40%x, 10%y)

    'Draw the image extracted from the source (main image) and place it into the preview pane at the top of the screen.
    cvsPreview.DrawBitmap(bmpRef, SrcRect, DstRect2)
   
    pnlPreview.Invalidate
   
End Sub
 

JordiCP

Well-Known Member
Licensed User
how do you declare bmpRef? Is it the same size as the View it is attached to? If not exactly and it just fills the view (Gravity.FILL), the offset may be due to the needed scaling for it to adapt to the view

In other words, the Rects refer to screen dimensions and it will only be valid if bitmap dimensions are the same as view dimensions
 
Last edited:

Gary Milne

Active Member
Licensed User
I get what you are saying. After looking at the size of the bmp it was 1080x1920 and my device is 1200x1662 so I think you are correct in that accounting for the movement. Now I just have to figure out how I generated a 1080x1920 bitmap in the first place.

Late addition:
The 1080x1920 is the camera resolution. All I had to do was resize it to the screen size using the code below and it worked perfectly.

Thanks so much for your help.

B4X:
    Sub CreateScaledBitmap(Original As Bitmap, NewWidth As Int, NewHeight As Int) As Bitmap
        Dim r As Reflector
        Dim b As Bitmap
      
        b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
            Array As Object(Original, NewWidth, NewHeight, True), _
            Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
        Return b
    End Sub
 
Last edited:
Top