Android Question CreateRoundRectBitmap and ImageDownloader

MarcoRome

Expert
Licensed User
Longtime User
Hi all i have this code:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
    
    
    
    Dim fotogrande() As String = Array As String("esempio-fotoa70.jpg","esempiofotoiphone6.jpg","fotosamsunga10.jpg","fotosamsung5.jpg")
    Starter.mappa_foto.Initialize
    For i = 0 To 3
        clv_grande.Add(CreateListItemGrande( "TEST GRANDE", "https://www.xxx.com//test/"&fotogrande(i),clv_grande.AsView.Width, clv_grande.AsView.Height - 10dip, i, ""),"")
    #if B4i 
        Downloader.Download(foto)
    #else if B4A
       CallSubDelayed2(ImageDownloader, "Download", Starter.mappa_foto)
    #End If
End Sub

Sub CreateListItemGrande(Text As String, immagine As String, Width As Int, Height As Int, riga As Int, passa_id As String ) As Panel
    Dim p As Panel
    p.Initialize("")
    #if B4A
    p.SetLayout(0, 0, Width, Height)
    #else if B4i
        p.SetLayoutAnimated(0, 1, 0, 0, Width, Height) 'set the size before the layout is loaded
    #End If
    
    p.LoadLayout("lay_clv_interno")


    'Immagina Carico dal Sito B4A / B4i
    #if B4i 
        foto.Put(img_foto, Starter.variabile1&name_image)
    #else if B4A
        Starter.mappa_foto.Put(ImageView1, immagine)
    #End If

    Return p

End Sub

and work without problem.

I would like to use the CreateRoundRectBitmap ( https://www.b4x.com/android/forum/pages/results/?query=CreateRoundRectBitmap ), if I upload files locally from the device everything works.
B4X:
Sub CreateListItemGrande(Text As String, immagine As String, Width As Int, Height As Int, riga As Int, passa_id As String ) As Panel
    Dim p As Panel
    p.Initialize("")
    #if B4A
    p.SetLayout(0, 0, Width, Height)
    #else if B4i
        p.SetLayoutAnimated(0, 1, 0, 0, Width, Height) 'set the size before the layout is loaded
    #End If
    
    p.LoadLayout("lay_clv_interno")

    Dim img As B4XBitmap = xui.LoadBitmap(File.DirAssets, immagine)
    ImageView1.SetBitmap(CreateRoundBitmap(img, ImageView1.Width))

    Return p

End Sub


But how to use the same to upload images via the internet ??
Any idea ?
Thank you
Marco
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You don't need to write such code:
B4X:
Dim p As Panel
    p.Initialize("")
    #if B4A
    p.SetLayout(0, 0, Width, Height)
#else if B4i
p.SetLayoutAnimated(0, 1, 0, 0, Width, Height) 'set the size before the layout is loaded
    #End If

Correct:
B4X:
Dim p As B4XView = XUI.CreatePanel("")
p.SetLayoutAnimated(0, 0, 0, Width, Height)

You should modify ImageDownloader and create the round bitmap right after the image is downloaded.
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Right Erel ( as usual ).


You should modify ImageDownloader and create the round bitmap right after the image is downloaded.
Code ImageDownloaderRound
B4X:
.....
Sub JobDone(Job As HttpJob)
    ongoingTasks.Remove(Job.JobName)
    If Job.Success Then
        Dim bmp As Bitmap = Job.GetBitmap
        cache.Put(Job.JobName, bmp)
        If tasks.IsInitialized Then
            For i = 0 To tasks.Size - 1
                Dim link As String = tasks.GetValueAt(i)
                If link = Job.JobName Then
                    Dim xui As XUI
                    Dim bmp1 As B4XBitmap = bmp
                    If bmp1.Width <> bmp1.Height Then
                        'if the image is not square then we crop it to be a square.
                        Dim l As Int = Min(bmp1.Width, bmp1.Height)
                        bmp1 = bmp1.Crop(bmp1.Width / 2 - l / 2, bmp1.Height / 2 - l / 2, l, l)
                    End If
                    Dim iv As ImageView = tasks.GetKeyAt(i)
                    Dim c As B4XCanvas
                    iv.Color = Colors.Black 'sfondo
                    Dim xview As B4XView =  iv
                    xview.SetLayoutAnimated(0, 0, 0, iv.Width, iv.Width)
                    c.Initialize(xview)
                    Dim path As B4XPath
                    path.InitializeOval(c.TargetRect)
                    c.ClipPath(path)
                    c.DrawBitmap(bmp1.Resize(iv.Width, iv.Width, False), c.TargetRect)
                    c.RemoveClip
                    c.DrawCircle(c.TargetRect.CenterX, c.TargetRect.CenterY, c.TargetRect.Width / 2 - 2dip, xui.Color_White, False, 2dip) 'comment this line to remove the border
                    c.Invalidate
                    Dim res As B4XBitmap = c.CreateBitmap
                    c.Release
                    iv.SetBackgroundImage(res)
                    'iv.SetBackgroundImage(bmp)
                End If
            Next
        End If
    Else
        Log("Error downloading image: " & Job.JobName & CRLF & Job.ErrorMessage)
    End If
    Job.Release
End Sub
.....
 
Upvote 0
Top