I need a little help with the thing that I want to achieve. I'm using BitmapShader in my application to draw on a canvas. I'm setting a custom png file as shader to my paint variable and I want to change the color of shader.
Here is an example code which I'm using :
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.particle_point);
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
mPaint.setShader(shader);
ColorFilter filter = new LightingColorFilter(0xFFFFFFFF , 0x000000FF );
mPaint.setColorFilter(filter);
I find that I can change it's color by using :
ColorFilter filter = new LightingColorFilter(0xFFFFFFFF , 0x000000FF );
, but I need to be able to change it's color by using a custom color picker,which returns color code similar to this : -234423123.
So is there any way that I can use this color code and set it as color to my paint variable.
Thanks in advance!
The color you are getting converted to hex is: FFFFFFFFF206FCAD. So youst need to get rid of the 8 leading Fs:
int color = -234423123;//0xFFFFFFFFF206FCAD
int myColor = 0x00000000FFFFFFFF & color;
myColor should be ok.
Just to add a little bit more detailed to the Moss's answer. As he suggest you can use myColor as the value you want and to set the right value to your shader you have to add myColor to your LightingColorFilter like this :
ColorFilter filter = new LightingColorFilter(myColor , myColor );
And it should work.
To get the hex String:
"#"+Integer.toHexString(n));
But your color picker just returns the color's int value which should be enough to work with!
ColorFilter filter = new LightingColorFilter(0xFFFFFFFF , 0x000000FF );
Just change the value which represents the color to the int your colorpicker returns... (without 0x in front of it ofcourse)...
If I ain't wrong this should work just as fine!
Related
I want to set background with gradient. This's my code:
val startColor = "0xFFAC235E"
val endColor = "0xFF640C35"
val gradient = GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
intArrayOf(
startColor.toInt(),
endColor.toInt()
)
)
view.background = gradient
and it through an exception:
java.lang.NumberFormatException: For input string: "0xFFAC235E"
If I replace startColor = 0xFFAC235E, the code above work fine. But that's not what I want.
I need put color as param String. Is there anyway to convert it?
Try replacing 0x with #.
For ex:
startColor.replace("0x", "#")
Generally we define colors with hex color codes. So, I think this will work for you.
Edit
You have to parse the color string to convert it into integer.
Color.parseColor(startColor.replace("0x", "#"))
I'm trying to set my color for the outline of my button, but I don't get it to work
I'm using material button and when I use
button.setStrokeColorResource(Color.parseColor(#e4dcd4))
is not working and tells me this
Expected a color resource id (R.color.) but received an RGB integer
I tried almost everything I could found about in stack, but I can't get it to set this strokeColor programmatically
Edit
Almost all setColors use #ColorInt , but this strokeColor uses #ColorRes, which is not working for me, also there is setStrokeColor
public void setStrokeColor(#Nullable ColorStateList strokeColor) {
if (isUsingOriginalBackground()) {
materialButtonHelper.setStrokeColor(strokeColor);
}
}
But I can't get it to work either.
It worked like this
val colorInt = Color.parseColor("#e4dcd4")
val csl = ColorStateList.valueOf(colorInt)
my_button.strokeColor = csl
You might try this
button.setStrokeColor(ContextCompat.getColor(this, R.color.your_color_xml));
Other way you can do is
ShapeDrawable gradientDrawable = (ShapeDrawable)button.getBackground();
gradientDrawable.setStroke(2, your_color);
Also as #Gabriele said you can get an int as a color as :
//From RGB
int colorRGB = Color.rgb(255,0,0);
//From HEX String
int colorHEX = Color.parseColor("#FF11AA");
You have to set the width of the stroke because the default value is 0.
<Button
app:strokeWidth="2dp"
../>
button.strokeColor = ColorStateList.valueOf(Color.parseColor("#e4dcd4"))
or
// if color define in color.xml
button.strokeColor = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.yourColorCOde))
// if you have different state and you want to set programmatically then do as :-
var states = arrayOf(
intArrayOf(R.attr.state_enabled),
intArrayOf(-R.attr.state_enabled),
intArrayOf(-R.attr.state_checked),
intArrayOf(R.attr.state_pressed)
)
// Color list define respect of state
var colors = intArrayOf(
Color.BLACK,
Color.RED,
Color.GREEN,
Color.BLUE
)
// Set stroke color
button.strokeColor = ColorStateList(states, colors)
Colour Code : e32b0f, d03559, I want to design its background drawable please help me
Try,
int colors[] = { 0xff255779, 0xfcc6c0cd };
GradientDrawable gradientDrawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
view.setBackgroundDrawable(gradientDrawable);
1. choose color and add color hex code in colors array.
2. GradientDrawable using and set orientation properties.[ex. GradientDrawable.Orientation.TOP_BOTTOM]
3. set view background [ex. Linearlayout.setBackgroundDrawable(gradientDrawable);]
I want to extract the RGB value from color
My color is declared in xml as
<color name="color_primary">#009688</color>
Now I want the RGB value of this in my Activity at runtime.
How to convert color_primary in Color object ?
Any help? Thanks in advance.
Your color is an int. You can use
int color = getResources().getColor(R.color.color_primary);
in onCreate of your Activity. If you need the RGB components of your color, you can use the Color class:
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
I have a ImageView where I put a Bitmap (left), sometimes I wanna see bitmap with a semitransparent blue layer (right). I try with a ColorFilter (LightingColorFilter and PorterDuffColorFilter) but I get a dark blue, how can I do this with ColorFilter or anything else?
Thanks.
EDIT (I tried this and other variants)
//ColorFilter filter = new PorterDuffColorFilter(color.wather, PorterDuff.Mode.DST_OVER);
ColorFilter filter = new LightingColorFilter(color.mul, color.wather);
// mul = 0xFFFFFFFF and wather = 0x7000FFFF
BitmapScaler scaler = new BitmapScaler();
imagen.setImageBitmap(scaler.getScaled());
imagen.setColorFilter(filter);
I tried diferents mul, add values and always get this:
I've already finds the mul, add values, I had a problem with color.xml because I used ID number instead the color RGB number, big mistake. Transparence (Alpha) are ignore but I can get the effect downing intensity of add value.
int mul = 0xFFFFFF;
int add = 0x005050;
filter = new LightingColorFilter(mul, add);
Thanks Elior for your help.