Android Question B4XTable1 - Gravity.FILL

T201016

Active Member
Licensed User
Longtime User
Hi,
Here is my test code:
The problem is that after refreshing B4XTable1
Gravity.FILL does not function properly as intended.
Only after restarting the Gravity.FILL application does the pictures work as it should.

What could be the reason?

To determine the size of the image, I use:
Buffer = rm.ImageToBytes(rm.ResizeBitmapMaxByte(bmp ,MaxByte))

Does it matter ??

B4X:
Public Sub [B]ResizeBitmapMaxByte[/B](Original As Bitmap, MaxByte As Long) As Bitmap
    Dim ByteSize As Long
    Dim ObjectBitmap As JavaObject = Original
    Dim API_Level As JavaObject

    API_Level.InitializeStatic("android.os.Build.VERSION")
 
    If API_Level.GetField("SDK_INT") >= 19 Then 'kitkat
        ByteSize = ObjectBitmap.RunMethod("getAllocationByteCount", Null)
    else if API_Level.GetField("SDK_INT") >= 12 Then 'Android 3.1
        ByteSize = ObjectBitmap.RunMethod("getByteCount", Null)
    Else 'Android versions
        ByteSize = ObjectBitmap.RunMethod("getRowBytes", Null) * Original.Height
    End If

    Dim Ratio As Float = Sqrt(MaxByte/ByteSize)

    If Ratio < 1 Then
        Width  = (Original.Width  * Ratio)
        Height = (Original.Height * Ratio)

        Return Original.Resize(Width, Height, True)
    Else
        Return Original
    End If
End Sub

B4X:
Private Sub B4XTable1_DataUpdated
    Try
        Dim sf As Object = SQL1.ExecQueryAsync("SQL","SELECT Blob FROM customers", Null)
        Wait for (sf) SQL_QueryComplete(Success As Boolean, rs As ResultSet)
     
        If Success Then
            For i = 0 To B4XTable1.VisibleRowIds.Size - 1 'row(s)
             
                Dim RowId As Long = B4XTable1.VisibleRowIds.Get(i)

                If RowId > 0 Then 'View the content of the fields

                    ...
                 
                    Dim p As B4XView = BlobColumn.CellsLayouts.Get(i + 1) '+1 because the first cell is the header
                    Dim v As ImageView
                    v.Initialize("")
                    v.Gravity = Gravity.FILL ------------------------------- ???
                    p.AddView(v, 1dip, 1dip, BlobColumn.Width - 2dip, B4XTable1.RowHeight - 1dip)
 
                    rs.Position = RowId -1 'The first valid position is 0.
                    Dim p As B4XView = BlobColumn.CellsLayouts.Get(i + 1) '+1 because the first cell is the header
                    Dim b As B4XView = p.GetView(1)
                 
                    Dim Buffer() As Byte = rs.GetBlob("Blob")
         
                    If Buffer <> Null Then
                        Dim InputStream1 As InputStream
                        InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
                        Dim bmp As Bitmap
                        bmp.Initialize2(InputStream1)
                        InputStream1.Close
                        b.SetBitmap(bmp)
                       v.Gravity = Gravity.FILL  ------------------------------------ ???
                    Else
                        b.SetBitmap(Null)
                    End If
                Else
                    Exit 'The End View the content of the fields
                End If
            Next
        End If
        rs.Close
     
        ...
     
    Catch
        Log(LastException.Message)
    End Try
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Dim sf As Object = SQL1.ExecQueryAsync("SQL","SELECT Blob FROM customers", Null)
Wait for (sf) SQL_QueryComplete(Success As Boolean, rs As ResultSet)
This is a mistake. Don't use async method here as the DataUpdated can be called multiple times. You can use async methods with the "index pattern".

Buffer = rm.ImageToBytes(rm.ResizeBitmapMaxByte(bmp ,MaxByte))
Why do you need this strange code?

Are you removing the old ImageViews? Looks like you are adding more and more ImageView to the same cells.
 
Upvote 0

T201016

Active Member
Licensed User
Longtime User
Hello Erel

Yes, I actually deleted the wrong code (p.AddView (...))
and used regular View instead of B4XView it works for now. B4XView seems to center the images by default, but I'm not sure.

As for your second question. I am importing photos after resizing them with this code (ResizeBitmapMaxByte), because I don't want to create temporary files on the disk for importing photos> 2MB.

B4X:
Dim p As B4XView = BlobColumn.CellsLayouts.Get(i + 1) '+1 because the first cell is the header
Dim b As View = p.GetView(1)
   
rs.Position = RowId -1 'The first valid position is 0.
                   
Dim Buffer() As Byte = rs.GetBlob("Blob")
           
If Buffer <> Null Then
    Dim InputStream1 As InputStream
    InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
    Dim bmp As Bitmap
    bmp.Initialize2(InputStream1)
    InputStream1.Close
    b.SetBackgroundImage(bmp) '------------------------------>>>>> Replace with b.SetBitmap(bmp)
Else
    b.SetBackgroundImage(Null)
End If
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
and used regular View instead of B4XView it works for now. B4XView seems to center the images by default, but I'm not sure.
B4XView.SetImage does set the gravity to center in B4A. In many cases I use such code:
B4X:
ImageView1.SetImage(...)
#if B4A
Dim iv As ImageView = ImageView1
iv.Gravity = Gravity.Fill
#end if
 
Upvote 0
Top