I tried to set colors programically for Paint library. And when I set colors, everything looks normal, like here:
But when I draw on the canvas with these set colors, every color except black becomes kinda purple, like this:
Everything looks normal, and causes no crashes. How to solve this? Any help appreciated. Thanks!
You can try to use this function ContextCompat.getColor() to retrieve the color :
java-static fun getColor(context: Context, id: #ColorRes Int): #ColorInt Int
It will return:
A single color value in the form 0xAARRGGBB.
So for example, if you would like to set color to brown, instead of having just the resource ID R.color.brown, use the following:
ContextCompat.getColor(context, R.color.brown)
Related
I have a svg icon imported to android studio.
The svg is basically a heart with a hard coded color.
I wish to set the color of this heart dynamically to any RGB value.
But it looks like ImageButton only has a "setBackgroundTint" method that takes a ColorStateList which only takes color resource.
Is there anyway for me to use Color.rgb() to set the color for my image?
The ColorStateList class has the static method valueOf(int color) that you can use with Color.rgb() to get a ColorStateList of a single color. For example:
ColorStateList tint = ColorStateList.valueOf(Color.rgb(255, 0, 255));
imageView.setBackgroundTintList(tint);
I am able to set my shape's stroke color by doing this:
GradientDrawable shape = (GradientDrawable) myview.getBackground();
shape.setStroke(strokeWidth, color);
Q: How do I get the current color of my shape's stroke programmatically?
Note that I am using API level 19 (so I cannot use shape.getColor() which anyway would return the filling color of the shape I believe, and not it's stroke's color).
As #pskink mentioned, Checking android sources shows that there is no such API for getting the current stroke color (which is odd and a pity).
Eventually what I did to overcome my problem, is keeping my own variable in code that indicates the current stroke color.
Android SDK provides the following icons.
Is there a way to set a color to those .. and if possible, how to do so?
<ImageView
android:id="#+id/share_button"
style="#style/IconNav"
android:src="#android:drawable/ic_menu_share"/>
<ImageView
android:id="#+id/bookmark_button"
style="#style/IconNav"
android:src="#android:drawable/ic_input_get"/>
UPDATE
After doing a complete refresh of the project, it turns out the tint attribute in Xml did the trick.
For the short answer
.. this is the solution that worked for me - adding the property to the ImageView xml:
android:tint="#color/grass_dark"
The answer from #goldenb is a thorough run through of the different ways to solve for this, so am marking that one as the answer.
You can indeed use a tint as a way of changing an ImageView's colour, BUT you should be reminded that the android:tint will always be applied on top of the original colour.
as stated by blogger danlew
ImageView's tint mixes the tint color with the original asset. What you want is for the tint color to take over entirely; instead it
applies the tint on top of the existing color. So, for example, if the
source asset is black, and you want it to be #77FFFFFF (a translucent
shade of white), you'll actually end up getting that shade of white
with a black background beneath it.
android:tint is limited to ImageView. You want to be able to tint any Drawable in any View.
One possible alternative would be for you to use android ColorFilter
According to the official documentation:
A color filter can be used with a Paint to modify the color of each pixel drawn with that paint. This is an abstract class that should never be used directly.
There are lots of more or less complex things you can do with ColorFilter but how can you apply this then?
One simple example from another so question is:
//White tint
imageView.setColorFilter(Color.argb(255, 255, 255, 255));
or
imageView.setColorFilter(ContextCompat.getColor(context,R.color.COLOR_YOUR_COLOR))
Or a more complete answer here in SO from here
ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);
// we can create the color values in different ways:
redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );
You should check these links if you want to learn more
SO - What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?
setColorFilter()
Fast Android asset theming with ColorFilter
SO-Modifying the color of an android drawable
You can use a bitmap with a tint. Add this to your drawables folder.
ic_input_get_colored.xml :
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#android:drawable/ic_input_get"
android:tint="#color/yourDesiredColor"/>
Let's say I have an ShapeDrawable in Android. Not a bitmap. I would like to replace single color (stroke color) in it by another color dynamically. The new desired color is not known at design time and can't be put into drawable resource.
For example, go from picture 1 (where black color represents the color I want to replace, and checkered background is the background not covered by the shape outline):
to image 2, where red is the color I want:
I can build the shape using alpha masks if necessary - i.e. have the white or black colors transparent, if necessary, or make the outline green, for example. The white fill color must remain white in the final result.
Is it possible to achieve that with standard color filters - ColorMatrixColorFilter, or PorterDuffColorFilter ?
If so, I'm having hard time figuring out specific filter.
I assume custom ColorFilter is not possible.
In the specific case of having a black border that you want to set to an arbitrary color, and a white interior, you can use a PorterDuff filter in ADD mode. Since it's saturating it won't have any effect on the white area, and since black is zero adding the color will effectively set all black areas to that color.
e.g. to set it to red:
drawable.setColorFilter(0x00ff0000, PorterDuff.Mode.ADD);
This tutorial may be helpful. According to this one, you can replace the white color portion of an image with desired color.
Changing color of drawable icon programmatically http://takeoffandroid.com/uncategorized/changing-color-of-drawable-icon-programmatically/
So i'm having a problem with using decimal colors in android.
I'm getting color codes from an external database.
For Example:
16777215 is white
16711680 is red
Now I want to use this colors to create a GradientDrawable.
int color = myDbReader.getColor(); //returns the decimal color code
new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[] {color, 0 });
The code example produces always a completley white gradient object.
I tried to google how to convert the decimal colors in the right way.. But i didn't find anything.
Can anyone give me a hint how to use the decimal colors in the right way?
You should read about android colours.
Android colors are 32bit unsigned integers, not signed ones!
Also Android colors are using Alpha bit, please read the article and the solution will be clear.
Even if SO is extremely good source of information you should google it before asking, it was the first link on Google when I entered "colour android".
BTW, I'm not an android developer.
Android colors are ARGB units, so your color should actually consist of 4 bytes with 1 of them for the alpha value. In order to convert you can have something like this
0xFF000000 & yourColor
which will set the opacity of your color to 100%(alpha byte is set). Another option is to use Color.parseColor after converting your integer to a hexadecimal string. Also do not refer to colors by some magic constants. The static fields of the Color class are designed so that you don't have to do that.
So based on your suggestions I spent last night with reading about the different types of Color codes and finally found the solution:
The given numbers are access color codes so i only have to:
int color = myDbReader.getColor(); //returns the decimal color code
gradient = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[] {Color.rgb((value/256/256)%256, (value/256)%256, value%256), 0 });
Thanks four your help!