Android Question How to replace color of drawable

kostefar

Active Member
Licensed User
Longtime User
Would something like this work via inline Java (assuming you make it "B4A aware," which is beyond my skill set)?

https://stackoverflow.com/questions/11376516/change-drawable-color-programmatically

I think it´d be a really nice and elegant solution, but I´m not much of a java head myself either so I think the way for me to go is by first converting the drawable to a bitmap, then do the color replacement, and then convert back again.

Thanks alot though!
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
I did it!

B4X:
Dim jo As JavaObject = lblText.Background
jo.RunMethod("setColorFilter", Array As Object(bgcolor, "SRC_IN"))

There´s just one problem with this: bgcolor is set to a 24bit RGB value (x,x,x) and when using this, the pixels that are are disappearing, they get fully transparent.
But if I set the color to for instance Color.Red, the before mentioned pixels do what they should. See below:



Any idea how to resolve this?
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Nice! I don't know enough about how Android graphics work internally, but maybe it's the mode you're using?

See: http://ssp.impulsetrain.com/porterduff.html

Thanks Jeffrey, and also for pointing me in the right direction in the first place - that´s where I picked up the method of doing it, in that stackoverflow link.
Unfortunately, I don´t think there´s anything on that impulsetrain site which explains how it can be that there´s a difference in using Colors.RGB vs Colors.Red here.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
See this:
B4X:
    Dim lblText As Label
    Dim Ca As Canvas
    Dim bgcolor As int = 0x0000FF
  
    lblText.Initialize("")
    lblText.Text="Test"
    lblText.TextColor=Colors.White
    Activity.AddView(lblText,0,0,200dip,40dip)
  
    Ca.Initialize(lblText)
    Ca.DrawColor(Colors.Red)
  
  
    Dim jo As JavaObject = lblText.Background
    jo.RunMethod("setColorFilter", Array As Object(Bit.Or(bgcolor,0xFF000000), "SRC_IN"))

I use Bit.Or for transofr 24bit color to 32bit color
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User


Thanks Star-Dust. It actually makes no difference if I´m using RGB(x,x,x) or Bit.Or(0xXXXXXX). But what I found out is that there are simply certain colors where the low-opacity bits will be more visible than with others. If it´s a bright color, they probably get so bright that they just disappear. It´s afterall not about 24 or 32 bit, but something needs to be done either in Photoshop by setting the opacity higher, or by simply choosing a different color in b4a.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…