if you want to set color for paint, you have two methods to use:
Method1: paint.setColor(Color.YELLOW);
Method2: paint.setARGB(255, 100, 100, 0);
Use the above methods, draw a yellow cycle in android. You will find that 'paint.setARGB() is different from paint.setColor() ',
use Method1 you will get a yellow cycle. (hopeful result)
use Method2 you will get a dim_yellow cycle, it's very strange,
Please give me some good suggestions if you have met this issue.
Well, these are two different colors:
Color.YELLOW
Hex: FFFFFF00
ARGB: 255, 255, 255, 0
Your color
Hex: FF646400
ARGB: 255, 100, 100, 0
Change paint.setARGB(255, 100, 100, 0); to paint.setARGB(255, 255, 255, 0); if you want the identical color.
Yellow -> Red:255 Green:255 Blue:0 Alpha:100
Related
I have an ImageView which shows a Drawable of my Resource Folder. Second i have a seekbar below the ImageView. The color of each pixel should be changed depending on the value of the seekbar. The colors of each pixel should be changed in the way, that when the seekbar has max value, the 'g' and 'b' value of each pixel should be 0, so that only the red is shown. If you lower the seekbar it should be converted back.
I think this is done by converting the drawable to a bitmap, iterating over the pixel and change their color values depending on the current seekbar progress and their current color values.
But this is just an idea. So far i was not able to implement this in a correct way. Maybe some of you can give a code example, or even have a much better solution for this. Would be glad if someone could help.
EDIT: With the help of #pskink (see comment below) i was able to implement the thing:
float colorValue = 1 - (float) seekBar.getProgress() / 100; // SeekBar has 100 as maxVal
float[] colorMatrix = {
1, 0, 0, 0, 0, //red
0, colorValue, 0, 0, 0, //green
0, 0, colorValue, 0, 0, //blue
0, 0, 0, 1, 0 //alpha
};
ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
backgroungImageView.setColorFilter(colorFilter);
The problem now is (obviously) that not only the original red parts stand out. Everything is red and this is also not what i want. The goal i am trying to achieve is that only the red parts are shown when the seekbar reaches max level. Would be cool if this is also possible with a ColorMatrix but i am not sure. Any suggestions? Could playing around with the Alpha Channel help?
If anyone is interested, here is how i solved the problem with pskinks help:
(SeekBar still has maxValue 100)
float pr = seekBar.getProgress() / (float) 50;
float[] colorMatrix = {
1, 0, 0, 0, 0, //red
0, 1, 0, 0, 0, //green
0, 0, 1, 0, 0, //blue
pr / 2, -pr, -pr, 1, 0 //alpha
};
ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
backgroungImageView.setColorFilter(colorFilter);
Just play around with the values to get some other results. So far these are the values with which i got the best results.
I have image with one color(different shades) and alpha channel. Image has reflections and shadows. I want create RGB color and paint this image. I tried set ColorMatrixColorFilter, but this filter is applied to the the original color and creates a different color than was chosen.
filter = new ColorMatrixColorFilter(new float[]
{
1, 0, 0, 0, colors[0],
0, 1, 0, 0, colors[1],
0, 0, 1, 0, colors[2],
0, 0, 0, 1, 0
});
"colors" array has successively red, green, blue colors.
I also tried using this for images with grayscale, but this also not give me colour which i want.
When I set some value at 255, received color is unnatural without reflections and shadows.
I am trying to make a custom brush. I took an image and trying it draw it on canvas when i touch and move the finger. I have a star shape png image with only white color. I planned to change the color according to color selection by user. To change the color I am using color matrix as follows :
float[] colorTransform = { //For red color as of now
1f,0 , 0, 0,0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 1, 0};
ColorMatrix colorMatrix = new ColorMatrix();
// colorMatrix.setSaturation(0f); //Remove Colour
colorMatrix.set(colorTransform); //Apply the Red
ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
Paint paint = new Paint();
paint.setColorFilter(colorFilter);
paint.setAntiAlias(true);
for (Point pos: positions)
canvas.drawBitmap(mBitmapBrush, pos.x, pos.y, paint);
Everything seems to work fine with one problem:
"While drawing Bitmap I am getting a thin black color border around my star shape". Am I using colormatrix in wrong way ? Any suggestions?
Thanks in advance.
If you want to use the color matrix to replace the original color of the bitmap, change the right-most column.
float[] colorTransform = {
0, 0, 0, 0, 255, // Set red component of each output pixel to max
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 1, 0}; // Copy over the alpha channel from the input
The last column is an offset term. It is added to the result, no matter how the input looked like. In above example, 255 is added to the red channel.
Note that the top-left 3x4 submatrix is empty. This essentially discards all color information in the input image. The 5th column adds color again: the color that the user picked.
Here is the code I use:
Shader mShader0 = new LinearGradient(10 , 0, 66 ,0,
new int[] { Color.RED, Color.GREEN, Color.BLUE },
null, Shader.TileMode.REPEAT); // CLAMP MIRROR REPEAT
pincel1.setShader(mShader0);
canvasG.drawRect( 0, 0, 30, 200, pincel1);
canvasG.drawRect(1250, 0, 1280, 200, pincel1);
canvasG.drawRect(1000, 0, 1030, 200, pincel1);
canvasG.drawRect( 200, 0, 230, 200, pincel1);
canvasG.drawRect( 250, 0, 280, 200, pincel1);
pincel1.setShader(null);
The result is that all the columns are different.
Why are not all the same? What is wrong?
The reason is because the LinearGradient is defined to start at x=10, end at x=66 and repeat after that. When you assign the gradient to the paint, and draw different rectangles, the co-ordinates of the rectangles are used to determine which "part" of the gradient to draw. For example: a rectangle from x=0 and width=56 will contain your complete gradient. Any rectangle after that will repeat the pattern.
For more understanding, if you draw rectangles at x={a, a+56, a+2*56, a+3*56 ..} and width<56 they will contain the same gradient pattern. I hope this explains why the above observations are correct.
I'm simply trying to paint/draw to the Canvas in Android. However, when I set the color using hex values or using the setARGB method, it doesn't work. But when I use Color.x (eg, Color.GREEN) it works. Here's the code:
Bitmap image = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
Paint paintBackground = new Paint();
int green = Color.argb(0, 0, 255, 0); // 65280 (Won't work)
green = 0x0000ff00; // 65280 (Won't work)
paintBackground.setARGB(0, 0, 255, 0);
green = paintBackground.getColor(); // 65280 (Won't work)
green = Color.GREEN; // -16711936 (Works!)
paintBackground.setColor(green);
green = paintBackground.getColor(); // -16711936
paintBackground.setStyle(Paint.Style.FILL);
canvas.drawRect(0, 0, bitmapWidth, bitmapHeight, paintBackground);
So basically Color.GREEN returns -16711936 - and this WORKS.
However, the hex value is 65280 - and this DOES NOT WORK. That is, it doesn't paint the green rectangle.
I need to use hex values because I need to set the color to 0x00ffff00 here and then later to a different hex value.
Does Android Color (setColor in Paint) Need a Negative Integer?
The problem is that 0x0000ff00 is not green, but fully transparent green. Fully opaque green would be 0xff00ff00 which is, as you have already noticed, -16711936. Similarly, when using setARGB you need to specify 255 for alpha for the color to be fully opaque.
Color holds 4 fields, alpha, red, green and blue. Whenever anything is mostly opaque it is negative. 50.2% transparent green is positive (0x7F00FF00/2,130,771,712) and 49.8% transparent green is negative (0x8000FF00/-2,147,418,368)
You can also call Color.rgb(0, 255, 0). With rgb() alpha is by default 255, fully opaque.