Problem: Bitmap is cropped in ImageView no matter what

doncx

Active Member
Licensed User
Longtime User
After a user takes a picture with camera code, I create an ImageView and scale it to fit on a panel. Problem is that even when I scale it properly, no matter what gravity I select, the image is cropped and not all visible. This drives my users nuts, as they think their picture is smaller than they thought.

Can anyone help me to figure this out?

I see a thread from two years ago that seems to be the same problem (http://www.b4x.com/forum/basic4android-updates-questions/8601-imageview-images-fit-problem.html), but not an imageview solution.

Here's a snapshot of my code:

B4X:
Sub Globals
   Dim pnlImage As Panel
End Sub

Sub Activity_Resume

   pnlImage.RemoveAllViews

   If File.Exists(File.DirRootExternal, "lobster.jpg" ) = True Then
      Dim iv = CreateImageView( pnlImage, File.DirRootExternal, "lobster.jpg" ) As List
      pnlImage.AddView( iv.get(0), iv.get(1), iv.get(2), iv.get(3), iv.get(4) )
   End If

End Sub

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

Sub CreateImageView( p As Panel, d As String, f As String ) As List
   
   Dim scale As Float
   Dim FullSizeBitmap As Bitmap
   Dim ScaledBitmap As Bitmap
   Dim iv As ImageView
   Dim ReturnValue As List
   Dim left, top, width, height As Int

   FullSizeBitmap.initialize( d, f )
   scale = Min ( p.width / FullSizeBitmap.width, p.height / FullSizeBitmap.height )
   scale = Min( scale, 1 )
   ScaledBitmap = CreateScaledBitmap( FullSizeBitmap, Floor( FullSizeBitmap.width * scale ), Floor( FullSizeBitmap.height * scale ) )
   left = Floor( ( p.width - ScaledBitmap.width ) / 2 )
   top = Floor( ( p.height - ScaledBitmap.height ) / 2 )
   height = ScaledBitmap.height
   width = ScaledBitmap.width
   iv.Initialize("AddedImageViewEvents")
   iv.Gravity = Gravity.CENTER
   iv.Bitmap = ScaledBitmap
   ReturnValue.Initialize
   ReturnValue.Add( iv )
   ReturnValue.Add( left )
   ReturnValue.Add( top )
   ReturnValue.Add( width )
   ReturnValue.Add( height )
   
   Return ReturnValue

End Sub
 

doncx

Active Member
Licensed User
Longtime User
Thanks, Erel -

I just came up with a similar solution. Drawing the image on a canvas is the (obvious?) way to go for sure.

I'll take a close look at that link.

Meanwhile, for edification, here's the function I wrote:

B4X:
Sub DrawImageOnPanel( p As Panel, d As String, f As String )

   p.RemoveAllViews

   Dim Canvas1 As Canvas
   Dim Scale As Float
   Dim Bitmap1 As Bitmap
   Dim DestRect As Rect
   Dim x, y, xMargin, yMargin As Int

   Canvas1.Initialize( p )
   Bitmap1.Initialize( d, f )

   Scale   = Min( 1, Min ( p.width / Bitmap1.width, p.height / Bitmap1.height ) )
   x       = Floor( Bitmap1.width * Scale )
   y      = Floor( Bitmap1.height * Scale )
   xMargin   = Floor( ( p.width - x ) / 2 )
   yMargin   = Floor( ( p.height - y ) / 2 )

   DestRect.Initialize( xMargin, yMargin, xMargin + x, yMargin + y )
   Canvas1.DrawBitmap(Bitmap1, Null, DestRect)

End Sub

I'm only a month or so in to Basic4Android, so my code's getting more elegant by the minute
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…