Bak to this...
I'm using a bunch of buttons, and some will have background images...
My images are larger than my buttons, but that is not a problem...
However, the Buttons Gets Squared, and the small default offset in it's margins is lost too, so it appears to be a litle bigger that all others with no image...Any work around??
Thanks Klaus...Can I ask of you a simpler example?
Just ho to add an image to a button, png with alpha, and still keep the corners, examining your code I got lost in all the diming...
You can add the image in the Designer.
You must add the image files with Add Images.
For Enabled Drawable set Bitmap Drawable and then in Image file choose the image file.
If you want rounded corners, the image must have the corners with 'transparent' color (like the images in the test program in my previous post).
For the buttons you can set different bitmaps for the different states (Enabled Drawable, Disabled Drawable, Pressed Drawable).
It's MUCH easier to set the views in the designer !
You can directly set all parameters or leave their default values, I wouldn't add the views by code.
The code in the TestButtons program sets different bitmaps or colors to buttons by code and changes them acording to a pressed or not pressed state.
Been pkaying around with some examples of the canvas...
still no luck...
B4X:
Dim Canvas1 As Canvas
Dim Bitmap1 As Bitmap
Bitmap1.Initialize(File.DirAssets, "Conts.png")
Dim DestRect As Rect
DestRect.Initialize(2, 2, 20,20)
Canvas1.Initialize(BtnB)
Canvas1.DrawBitmap(Bitmap1, Null, DestRect)
This compiles ok, but gives me a null exception on my device
Thanks Klaus, but that does not what I want..
The effect acomplished by your code I can get using 3 code lines:
Dim Bitmap1 As Bitmap
Bitmap1.Initialize(File.DirAssets, "conts.png")
BtnB.SetBackgroundImage(Bitmap1)
The png is transparent, and the button does not show...only the image
PS: From what I can see, I need to resize the image, using canvas...and then aply the resulting bitmap to the button view...but I can get ir to work...
Yes, but if you set in my code Birmap2 a different bitmap you will see a change when the button is pressed !
With your code you don't see any reaction when the button is pressed.
What exactly do you want to do ?
If your png image is totaly transparent and setting it to the button you won't see anything.
My png is nor completly transparent..
it has a figure in it but it's background color is transparent...
I want to Overlay it in a button view...in a way that it should just blend with it...
Attached you find a test program, I think it shows what you want to do.
More complicated than doing it in the Designer.
It seems that it is not possible to access directly the button's bitmaps with a Canvas view.
I am using b4a v1.1 that came out last week and still have this problem. What do I need to do to maintain the rounded corners and "3D" look while changing the button color?
I am using b4a v1.1 that came out last week and still have this problem. What do I need to do to maintain the rounded corners and "3D" look while changing the button color?
The 3d effect is achieved using images. You will not be able to change the color and keep this effect. You will need to create your own images.
If you want round corners you will need to initialize a ColorDrawable and assign it to the Button. The round corners are only kept with views that have "simple" drawable and not StateListDrawable like the button.