I have a grayscaled image (filled shape with some gradient decoration) as an ImageResource in my ImageView. Is it possible to somehow apply color to that image? Maybe with the help of setColorFilter? I've read about PorterDuff.Mode, but could not figure out how to use it properly.
I'd be grateful for any help! Thanks in advance!
You can apply a single color easily to your ImageView with the following code:
PorterDuffColorFilter mColorFilter =
new PorterDuffColorFilter(0xffff0000, PorterDuff.Mode.DARKEN)
imageView.setColorFilter(mColorFilter);
You can change the PorterDuff mode to your requirements.
Related
I would like to change (map) white color to blue color. How can I do this on ImageView in Android? I tried setColorFilter by PorterDuff / LightingColorFilter / ColorMatrixColorFilter but I can't figure out how to set it up. There is a transparent background around the image.
Try to use Background Tint color function in xml or java code.
or
I would suggest another way. Take on frame layout and place view and then image view.
white portion in image should be transparent use PNG image.
and you can give color to base view color which ever you want.
Hi guys, i'm trying to make ActionBar the same as in the image above (with this "wave"), where the red color can be changed at run time. I tried to use GradientDrawable (code below), but the gradient effect was not cool and it's not what i need.
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
new int[] {cor, Color.WHITE});
gd.setCornerRadius(0f);
actionBar.setBackgroundDrawable(gd);
Any idea?
I'm not completly sure, but it can be done using custom drawable.xml something like this. Even, I think that you would have mix some drawables to get the image that you're posting.
An alternative is having the image in png or jpg that you want to set to actionbar and code this. Regards!
I created the red part of the image above at https://vectr.com (.svg).
in Android Studio, i added a new shape_action_bar.xml (vector asset), from the created image.
in the activity I added the following code:
Drawable drawable = getNavigationController().getResources().getDrawable(R.drawable.shape_action_bar);
ColorFilter color = new PorterDuffColorFilter(selected_color, PorterDuff.Mode.SRC_ATOP );
drawable.setColorFilter(color);
actionBar.setBackgroundDrawable(drawable);
The shape will change color according to the desired color. Thankss
I was going through Android : Converting color image to grayscale and other similar answers, when I doubted whether it's possible to grayscale a coloured "background image" i.e., image set using android:background="". Is it possible? If yes, how?
I have not got the answer even after several hours of extensive googling.
EDIT: I have found a hack for the problem, but it does not solve the problem completely. I have used
android:backgroundTint="#999999"
android:backgroundTintMode="multiply"
along with my code to set the background image.
Use an ImageView in your layout instead of trying to set the background to a drawable and then use this code to programmatically color the image to grayscale.
final ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
mImageView.setColorFilter(filter);
If you need help using an ImageView instead of background, please post your layout xml and java code for this scene.
The CircularImageView, https://github.com/Pkmmte/CircularImageView, works great when setting an image bitmap like so...
circularImageView.setImageBitmap(bitmap);
However, there are times where I just want to set a solid color instead of a bitmap. If I do something like this,
circularImageView.setBackgroundResource(R.color.blue);
The color of the view is set but the image is never made circular, so it fill the entire rectangular view. I'm assuming the getDrawable() is returning null so it can't actually manipulate the view. Anyone ran into this problem or any suggestions on what to do?
Edit:
I can do this but it seems a bit flimsy:
Bitmap image = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
image.eraseColor(android.graphics.Color.GREEN);
circularImageView.setImageBitmap(image);
You should call circularImageView.setImageResource(R.color.blue) instead.
The code you wrote sets the background of the view, not the content image. Looking at the code on github, this view will only clip the content image to the circle--it has no effect on the background at all.
Give a try for ColorDrawable;
int decode = Integer.decode("FF6666");
ColorDrawable colorDrawable = new ColorDrawable(decode);
I have an image in a resource file.
Drawable draw = getResources().getDrawable(R.drawable.my_icon);
The image has a transparent background.
Is there a way to programmatically set the background color to the Drawable before using the end product further in my code?
I think Drawing with PorterduffXferMode may help you in your case. This way you can merge two images (your image and a overlay completly in your color you want to replace the transparent pixels with) in many different ways.
Different porterduff modes explaned:
http://www.ibm.com/developerworks/java/library/j-mer0918/
Android example:
http://www.vogella.com/code/ApiDemos/src/com/example/android/apis/graphics/Xfermodes.html
This way you draw the result inside a new Bitmap. (SRC_OVER should work in your case if your image is the src and the background is used as the dst)
setColorFilter() with Porterduff SRC will break the transparent of drawable.
I used this in my code, and it work
disabledIcon = ContextCompat.getDrawable(getContext(), resId);
disabledIcon = DrawableCompat.wrap(disabledIcon);
disabledIcon.mutate(); // to not share its state with any other drawable
DrawableCompat.setTint(disabledIcon, ContextCompat.getColor(getContext(), R.color.button_text_disabled));