Android Code Snippet Draw round bitmap

Sub Name: DrawRoundBitmap

Description: This code uses Path object to draw a round bitmap at the center of a transparent mutable bitmap.

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim cvs As Canvas = CreateBitmap
   DrawRoundBitmap(cvs, LoadBitmap(File.DirAssets, "1.jpg"))
   Activity.SetBackgroundImage(cvs.Bitmap)
End Sub

Sub CreateBitmap As Canvas
   Dim bmp As Bitmap
   bmp.InitializeMutable(200dip, 200dip)
   Dim cvs As Canvas
   cvs.Initialize2(bmp)
   Dim r As Rect
   r.Initialize(0, 0, bmp.Width, bmp.Height)
   cvs.DrawRect(r, Colors.Transparent, True, 0)
   Dim p As Path
   p.Initialize(0, 0)
   Dim jo As JavaObject = p
   Dim x = 100dip, y = 100dip, radius = 100dip As Float
   jo.RunMethod("addCircle", Array As Object(x, y, radius, "CW"))
   cvs.ClipPath(p)
   Return cvs
End Sub

Sub DrawRoundBitmap (cvs As Canvas, bmp As Bitmap)
   Dim r As Rect
   r.Initialize(0, 0, cvs.Bitmap.Width, cvs.Bitmap.Height)
   cvs.DrawBitmap(bmp, Null, r)
End Sub

upload_2014-5-21_9-10-30-png.25169


Dependencies: JavaObject

Tags: round bitmap, ClipPath
 
Last edited:

anOparator

Active Member
Licensed User
Longtime User
I thought the round/oval button was a great option , this eclipses that :cool:
 

deyvitavara

New Member
Licensed User
Longtime User
Sub Name: DrawRoundBitmap

Description: This code uses Path object to draw a round bitmap at the center of a transparent mutable bitmap.

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim cvs As Canvas = CreateBitmap
   DrawRoundBitmap(cvs, LoadBitmap(File.DirAssets, "1.jpg"))
   Activity.SetBackgroundImage(cvs.Bitmap)
End Sub

Sub CreateBitmap As Canvas
   Dim bmp As Bitmap
   bmp.InitializeMutable(200dip, 200dip)
   Dim cvs As Canvas
   cvs.Initialize2(bmp)
   Dim r As Rect
   r.Initialize(0, 0, bmp.Width, bmp.Height)
   cvs.DrawRect(r, Colors.Transparent, True, 0)
   Dim p As Path
   p.Initialize(0, 0)
   Dim jo As JavaObject = p
   Dim x = 100dip, y = 100dip, radius = 100dip As Float
   jo.RunMethod("addCircle", Array As Object(x, y, radius, "CW"))
   cvs.ClipPath(p)
   Return cvs
End Sub

Sub DrawRoundBitmap (cvs As Canvas, bmp As Bitmap)
   Dim r As Rect
   r.Initialize(0, 0, cvs.Bitmap.Width, cvs.Bitmap.Height)
   cvs.DrawBitmap(bmp, Null, r)
End Sub

upload_2014-5-21_9-10-30-png.25169


Dependencies: JavaObject

Tags: round bitmap, ClipPath

Erel,

How could I do if I want to add color and size to the edge?

Thank you!
 

deyvitavara

New Member
Licensed User
Longtime User
Top