button with a gradient and bitmap

D

Deleted member 103

Guest
how can I create a button with a gradient and bitmap?
 

Attachments

  • ButtonWithBitmap.jpg
    ButtonWithBitmap.jpg
    3.6 KB · Views: 964

Erel

B4X founder
Staff member
Licensed User
Longtime User
The attached program creates a gradient button with an image. The gradient is set in the designer.
Then a Canvas is used to draw the image to the button.

20101211_1.png


Note that when you initialize the canvas it changes the button drawable to a BitmapDrawable. Which means that the background will not change when you press on it.
The following code is a modified version that sets the background to a StateListDrawable:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    Dim canvas1 As Canvas
    canvas1.Initialize(Button1)
    Dim r As Rect
    Dim btnRect As Rect
    btnRect.Initialize(0, 0, Button1.Width, Button1.Height)
    r.Initialize(btnRect.CenterX - 30dip, btnRect.CenterY - 30dip, _
        btnRect.CenterX + 30dip, btnRect.CenterY + 30dip)
    canvas1.DrawBitmap(LoadBitmap(File.DirAssets, "smiley.gif"), Null, r) 
    
    'additional code
    Dim sld As StateListDrawable
    sld.Initialize
    Dim cd As ColorDrawable
    cd.Initialize(Colors.Green, 5dip)
    sld.AddState(sld.State_Pressed, cd)
    sld.AddCatchAllState(button1.Background)
    button1.Background = sld
End Sub
 

Attachments

  • GradientAndImage.zip
    8 KB · Views: 507
Upvote 0
D

Deleted member 103

Guest
Thank you for the example but I would like to create a button with the Library fgGradientButtons. Isn't that possible with Basic4Android?
 
Upvote 0
D

Deleted member 103

Guest
I think you've misunderstood me.

Here again the question is translated correctly.
Thank you for the example but I would like to create a button with the Library fgGradientButtons. Isn't that possible with Basic4Android?
Thank you for the example but I would like to create a button like the Library fgGradientButtons. Isn't that possible with Basic4Android?
 
Upvote 0
D

Deleted member 103

Guest
I've tried it now, but I am not quite satisfied.
B4X:
Sub Process_Globals
   
End Sub

Sub Globals
   Dim Button1 As Button
   Dim r As Rect
   Dim btnRect As Rect
   Dim Gradient1 As GradientDrawable
   Dim canvas1 As Canvas
   Dim Clrs(2) As Int
   Dim time As Timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   canvas1.Initialize(Button1)
   btnRect.Initialize(0, 0, Button1.Width, Button1.Height)
   r.Initialize(btnRect.CenterX - 30dip, btnRect.CenterY - 30dip, _
      btnRect.CenterX + 30dip, btnRect.CenterY + 30dip)
   
   DrawButton(Colors.Green,Colors.Blue)

   time.Initialize("time",100)
End Sub

Sub Button1_Click
   DrawButton(Colors.Blue,Colors.Green)
   time.Enabled=True
End Sub

Sub time_Tick
   DrawButton(Colors.Green, Colors.Blue)
   time.Enabled=False
End Sub

Sub DrawButton(col1 As Int, col2 As Int)
   Clrs(1) = col1
   Clrs(0) = col2
   Gradient1.Initialize("TOP_BOTTOM", Clrs)
   Canvas1.DrawDrawable(Gradient1, btnRect)
   canvas1.DrawBitmap(LoadBitmap(File.DirAssets, "smiley.gif"), Null, r)
   Activity.Invalidate
End Sub
 
Upvote 0
Top