Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
Dim src As B4XBitmap = xui.LoadBitmapResize(File.DirAssets, "white-black-horse-14593326.jpg", 400, 400, False)
Dim mask As B4XBitmap = xui.LoadBitmapResize(File.DirAssets, "1200px-Heart_corazn.svg.png", 400, 400, False)
Dim n As Long = DateTime.Now
For i = 1 To 10
Activity.SetBackgroundImage(CropImageBaseOnMaskWithBitmapCreator(src, mask))
Next
Log($"BitmapCreator: time per iteration: $1.1{(DateTime.Now - n) / 10}"$)
Dim n As Long = DateTime.Now
For i = 1 To 10
Activity.SetBackgroundImage(CropImageBaseOnMaskWithCanvas(src, mask))
Next
Log($"Canvas: time per iteration: $1.1{(DateTime.Now - n) / 10}"$)
End Sub
Sub CropImageBaseOnMaskWithBitmapCreator (src As B4XBitmap, Mask As B4XBitmap) As B4XBitmap
Dim target As BitmapCreator
target.Initialize(src.Width, src.Height)
target.CopyPixelsFromBitmap(Mask)
Dim srcbc As BitmapCreator
srcbc.Initialize(src.Width, src.Height)
srcbc.CopyPixelsFromBitmap(src)
For x = 0 To srcbc.mWidth - 1
For y = 0 To srcbc.mHeight - 1
If target.IsTransparent(x, y) = False Then
target.CopyPixel(srcbc, x, y, x, y)
End If
Next
Next
Return target.Bitmap
End Sub
Sub CropImageBaseOnMaskWithCanvas(src As Bitmap, mask As Bitmap) As Bitmap
Dim B1,B2 As Bitmap
Dim cv As Canvas
B1.InitializeMutable(src.Width, src.Height)
cv.Initialize2(B1)
Dim rt As Rect
rt.Initialize(0, 0, src.Width, src.Height)
cv.DrawBitmap(src, Null, rt)
B2.InitializeMutable(src.Width, src.Height)
cv.Initialize2(B2)
cv.DrawBitmap(mask, Null, rt)
For r=0 To B2.Height-1
For c=0 To B2.Width-1
If B2.GetPixel(c,r)<>Colors.Transparent Then
cv.DrawPoint(c,r,B1.GetPixel(c,r))
End If
Next
Next
Return B2