- How to change Bitmap image color in android?
- 8 Answers 8
- How to set tint for an image view programmatically in android?
- 23 Answers 23
- Change drawable color programmatically
- 19 Answers 19
- Change fill color on vector asset in Android Studio
- 15 Answers 15
- Update: AppCompat support
- Android — change send icon’s color on ImageButton
- 6 Answers 6
- Not the answer you’re looking for? Browse other questions tagged java android android-imagebutton or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
How to change Bitmap image color in android?
I am developing an android application in which I set an image to imageview. Now programmatic I want to change the bitmap image color. Suppose my image have red color initially and now I need to change it to orange color. How can I do that? Please help.
Here is my code. I managed to change the opacity but I do not know how to change the color.
8 Answers 8
I tried Josip’s answer but wouldn’t work for me, regardless of whether the offset parameter was 1 or 0 — the drawn bitmap just appeared in original colour.
However, this did work:
Update 1
Whilst the above works well and is useful in a lot of cases, if you just want to change the main colour of an ImageView drawable, which the op did, you can just use:
If you need more flexibility or this doesn’t give the desired effect, there’s an overload that allows you to change the PorterDuff Mode until you get what you’re after:
Update 2
Another good use case I’ve had for this lately is customizing the appearance of a Google map v2 marker icon. In order to use 2 graphics to allow (for example) small/large icons on a marker, but also a range of colours on those 2 graphics by changing the colour of them dynamically. In my case I was doing this inside a ClusterRenderer as the markers were also clustered, but this can be used with a regular map marker the same way:
Источник
How to set tint for an image view programmatically in android?
Need to set tint for an image view. I am using it the following way:
But it doesn’t change.
23 Answers 23
UPDATE:
@ADev has newer solution in his answer here, but his solution requires newer support library — 25.4.0 or above.
You can change the tint, quite easily in code via:
imageView.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint
If you want color tint then
For Vector Drawable
Most answers refer to using setColorFilter which is not what was originally asked.
The user @Tad has his answer in the right direction but it only works on API 21+.
To set the tint on all Android versions, use the ImageViewCompat :
Note that yourTint in this case must be a «color int». If you have a color resource like R.color.blue , you need to load the color int first:
This worked for me
@Hardik has it right. The other error in your code is when you reference your XML-defined color. You passed only the id to the setColorFilter method, when you should use the ID to locate the color resource, and pass the resource to the setColorFilter method. Rewriting your original code below.
If this line is within your activity:
Else, you need to reference your main activity:
Note that this is also true of the other types of resources, such as integers, bools, dimensions, etc. Except for string, for which you can directly use getString() in your Activity without the need to first call getResources() (don’t ask me why).
Otherwise, your code looks good. (Though I haven’t investigated the setColorFilter method too much. )
Источник
Change drawable color programmatically
I’m trying to change the color on a white marker image by code. I have read that the code below should change the color, but my marker remains white.
Did I miss something? Is there any other way to change colors on my drawables located in my res folder?
19 Answers 19
Using DrawableCompat is important because it provides backwards compatibility and bug fixes on API 22 devices and earlier.
You can try this for svg vector drawable
You may need to call mutate() on the drawable or else all icons are affected.
You can try this for ImageView . using setColorFilter() .
Another way to do this on Lollipop, android 5.+ is setting a tint on a bitmap drawable like such:
This will work for you if you have a limited number of colors you want to use on your drawables. Check out my blog post for more information.
I have wrote a generic function in which you can pass context, icon is id drawable/mipmap image icon and new color which you need for that icon.
This function returns a drawable.
You could try a ColorMatrixColorFilter, since your key color is white:
This worked for me. Make sure to put «ff» between 0x and color code. Like this 0xff2196F3
Same as the accepted answer but a simpler convenience method:
Note, the code here is Kotlin.
You may want to try Mode.LIGHTEN or Mode.DARKEN . The Android Javadocs are horrible at explaining what the PorterDuff Modes do. You can take a look at them here: PorterDuff | Android
I suggest looking around at Compositing on Mozilla’s site here. (They don’t have all the modes that android does but they have a lot of them)
Use this: For java
you can use PorterDuff.Mode.SRC_ATOP, if your background has rounded corners etc.
For those who use Kotlin a simple extension function:
Источник
Change fill color on vector asset in Android Studio
Android Studio now supports vector assets on 21+ and will generate pngs for lower versions at compile time. I have a vector asset (from the Material Icons) that I want to change the fill color. This works on 21+, but the generated pngs do not change color. Is there a way to do this?
15 Answers 15
Don’t edit the vector assets directly. If you’re using a vector drawable in an ImageButton, just choose your color in android:tint .
BUT you cannot use @color references for colors (..lame), otherwise it will work only for L+
As said in other answers, don’t edit the vector drawable directly, instead you can tint in java code, like that:
And for the sake of simplicity, I have created a helper class:
To use just do the following:
To change vector image color you can directly use android:tint=»@color/colorAccent»
To change color programatically
Currently the working soloution is android:fillColor=»#FFFFFF»
Nothing worked for me except hard coding in the vector
However, fillcolor and tint might work soon. Please see this discussion for more information:
Also the colors mighr stick in the cache so deleting app for all users might help.
Update: AppCompat support
Other answers suspecting if android:tint will work on only 21+ devices only, AppCompat(v23.2.0 and above) now provides a backward compatible handling of tint attribute.
So, the course of action would be to use AppCompatImageView and app:srcCompat (in AppCompat namespace) instead of android:src (Android namespace).
Here is an example(AndroidX: This is androidx.appcompat.widget.AppCompatImageView ;)):
And don’t forget to enable vector drawable support in gradle:
Android studio now supports vectors pre-lollipop. No PNG conversion. You can still change your fill color and it will work.
In you ImageView, use
In your gradle file,
If the vectors are not showing individually set colors using fillColor then they may be being set to a default widget parameter.
Try adding app:itemIconTint=»@color/lime» to activity_main.xml to set a default color type for the widget icons.
Add this library to the Gradle to enable color vector drawable in old android Devices.
and re sync gradle. I think it will solve the problem.
if you look to support old version pre lolipop
use the same xml code with some changes
instead of normal ImageView —> AppCompatImageView
instead of android:src —> app:srcCompat
here is example
dont forget update your gradle as @ Sayooj Valsan mention
Notice To any one use vector dont ever ever never give your vector reference to color like this one android:fillColor=»@color/primary» give its hex value .
Источник
Android — change send icon’s color on ImageButton
How to change the default color of send icon on this ImageButton ?
I want to use gray instead of current white color.
6 Answers 6
Add tint attribute and you can set any color you want. Also you can set android:tintMode attribute (Which says how the color should apply).
You can use colorFilter on image view and can give any color runtime.
Add android:tint attribute to set the icon colour.
put this image in your drawable folder and then
save it in drawable as an image
download icon put inside drawable folder
you can put your costomize image in you drawable then you can use it as image src
Not the answer you’re looking for? Browse other questions tagged java android android-imagebutton or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник