Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim xui As XUI
Dim img As B4XBitmap
Dim xIV As B4XView
Private ImageViewCircularImage As ImageView
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Activity.LoadLayout("main")
DownloadAndSaveFile("https://b4x-4c17.kxcdn.com/images/Logo_on-dark.png")
Dim img As B4XBitmap = xui.LoadBitmap(File.DirInternal, "temp.png")
Dim xIV As B4XView = ImageViewCircularImage
xIV.SetBitmap(CreateRoundBitmap(img, xIV .Width))
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
'xui is a global XUI variable.
Sub CreateRoundBitmap (Input As B4XBitmap, Size As Int) As B4XBitmap
If Input.Width <> Input.Height Then
'if the image is not square then we crop it to be a square.
Dim l As Int = Min(Input.Width, Input.Height)
Input = Input.Crop(Input.Width / 2 - l / 2, Input.Height / 2 - l / 2, l, l)
End If
Dim c As B4XCanvas
Dim xview As B4XView = xui.CreatePanel("")
xview.SetLayoutAnimated(0, 0, 0, Size, Size)
c.Initialize(xview)
Dim path As B4XPath
path.InitializeOval(c.TargetRect)
c.ClipPath(path)
c.DrawBitmap(Input.Resize(Size, Size, False), c.TargetRect)
c.RemoveClip
c.DrawCircle(c.TargetRect.CenterX, c.TargetRect.CenterY, c.TargetRect.Width / 0 - 2dip, xui.Color_Black, False, 2dip) 'comment this line to remove the border
c.Invalidate
Dim res As B4XBitmap = c.CreateBitmap
c.Release
Return res
End Sub
Sub DownloadImage(Link As String, iv As ImageView)
Dim j As HttpJob
j.Initialize("", Me)
j.Download(Link)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
iv.Bitmap = j.GetBitmap
End If
j.Release
End Sub
Sub DownloadAndSaveFile (Link As String)
Dim j As HttpJob
j.Initialize("", Me)
j.Download(Link)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Dim out As OutputStream = File.OpenOutput(File.DirInternal, "temp.png", False)
File.Copy2(j.GetInputStream, out)
out.Close '<------ very important
End If
j.Release
End Sub