android - Applying color filter in StateListDrawable not working -
i'm writing application in have on-the-fly image-mutations.
what have put drawable on screen somewhere, give fancy color can changed on fly , make clickable (with statelistdrawable).
for color-change on fly thinking use porterduffcolorfilter apply on drawable. adding drawables statelistdrawable seems bad idea color filters removed. came solution found somewhere on so:
bitmapfactory.options options = new bitmapfactory.options(); options.inpreferredconfig = bitmap.config.argb_8888; bitmap 1 = bitmapfactory.decoderesource(context.getresources(), r.drawable.my_drawable, options); bitmap onecopy = bitmap.createbitmap(one.getwidth(), one.getheight(), bitmap.config.argb_8888); canvas c = new canvas(onecopy); paint p = new paint(); p.setcolorfilter(new porterduffcolorfilter(ontheflycolorresid, porterduff.mode.src_atop)); c.drawbitmap(one, 0, 0, p); ... sld.addstate(new int[]{-statefocused}, new bitmapdrawable(context.getresources(), onecopy));
this kind of works, there's 1 tricky thing. image below result while r.drawable.my_drawable png file entirly black 3px transparant border around it.
if add original drawable full black image transparant pixels on side below:
sld.addstate(new int[]{-statefocused}, context.getresources().getdrawable(r.drawable.my_drawable));
and result:
so thinking might wrong onecopy bitmat or drawing on canvas, change code this:
bitmapfactory.options options = new bitmapfactory.options(); options.inpreferredconfig = bitmap.config.argb_8888; bitmap 1 = bitmapfactory.decoderesource(context.getresources(), r.drawable.my_drawable, options); ... sld.addstate(new int[]{-statefocused}, new bitmapdrawable(context.getresources(), one));
so no more transformations of drawable, reading bitmap , transforming drawable again results in strange result:
and want second image custom color applied.
does has idea why strange fade-effect left , right of image?
to apply color filter , change color of drawable find using base drawable grayscale/white best while used in combination mode.multiply porterduff filter.
if want in code fine, best combination, in opinion, have textview defined in xml said white-grayscale background in code can change background color:
<textview android:id="@+id/mybox" . . . android:background="@drawable/box"
the box drawable can anything, png or xml defining rectangular shape filled solid color white.
int ontheflycolor = 0xaarrggbb; or getresources().getcolor(r.color.red); textview tv = findviewbyid(r.id.mybox); tv.getbackground().setcolorfilter(ontheflycolor, mode.multiply);
Comments
Post a Comment