Android - using an image resource multiple times but changing alpha on only 1 instance
I'm pretty sure I saw a specific command for this, but I can't remember what it's called, which means I can't Goog开发者_StackOverflow中文版le it!
If I have an image resource, e.g. R.drawable.myimage , which gets used in a Linearview more than once and I want to change the alpha on just a single occurrence of that image, (normally, changing the alpha changes all the occurrences of that image), what command do I use to 'unlink' the changes of that image.
Take a look at Drawable.mutate()
.
Here is a code example that I've used. I think it is self-descriptive.
Drawable icon = context.getResources().getDrawable(R.drawable.actions_icon);
iconView.setImageDrawable(icon);
if (action.isNew()) {
icon.setAlpha(50);
}
else {
icon.setAlpha(255);
}
You can also read an article about Drawable mutations
Hope this helps!
This will work also in this code...
Paint gpaint = new Paint(Paint.ANTI_ALIAS_FLAG);
gpaint.setAlpha(whatever number you want alpha);
canvas.drawBitmap(MainMenu.dressgirl, drx,dry,gpaint);
all others could be
canvas.drawBitmap(MainMenu.dressgirl, drx,dry,null);
精彩评论