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.
Related
In my app I am trying to change the color of my marker image. But it doesn't work exactly how I want it to work.
Drawable drawable = getDrawable(R.drawable.ic_marker);
drawable.setColorFilter(item.getColor()), PorterDuff.Mode.ADD);
The drawable looks like this (below), when I add the ColorFilter the marker gets the correct color and the white stays white, but also the transparent part of the image gets the color. I only want the black to change and the white and transparent part have to stay that way.
This is what I use for experiments with setColorFilter, ColorMatrix and ColorMatrixColorFilter:
drawable.setColorFilter(item.getColorMatrix());
OR:
drawable.setColorFilter(new ColorMatrixColorFilter(getColorMatrix5()));//custom 5
//custom 5
private ColorMatrix getColorMatrix5()
{
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);//make it greyscale
ColorMatrix blueMatrix = new ColorMatrix(new float[] {
0, 0, 0, 0, 0, // red
0, 0, 0, 0, 0, // green
1, 1, 1, 1, 1, // blue
1, 1, 1, 1, 1 // alpha
});
// Convert, then scale and clamp
colorMatrix.postConcat(blueMatrix);
return colorMatrix;
}//getColorMatrix5
See PorterDuff, PorterDuff.Mode,PorterDuffXfermode, ColorFilter,ColorMatrix, ColorMatrixColorFilter, PorterDuffColorFilter, Canvas, Color.
I know I can use this code to draw a line on the rectangle:
paint.setColor(Color.RED);
canvas.drawRect(100, 100, 400, 400, paint);
paint.setColor(Color.GREEN);
canvas.drawLine(0, 0, 500, 500, paint);
And the canvas looks like this :
But now I want to draw the line under the rectangle without exchange the order of their drawing, and it should like :
How can I do that ?
Can the canvas undo what has just been painted on it?
or
Does canvas have layers in it, which I can specify to draw on?
You can use CustomViews to draw the shape and existing shape or on Image
You can do it simply by drawing two lines:
paint.setColor(Color.RED);
canvas.drawRect(100, 100, 400, 400, paint);
paint.setColor(Color.GREEN);
canvas.drawLine(0, 0, 100, 100, paint);
canvas.drawLine(400, 400, 500, 500, paint);
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.
I am working in some game idea with Android and AndEngine, but I can't find a good tiling approach.
Some part of the game will consist on a rectangular grid. Three "styles" are possible, for each square side of the grid or the inner square. For simplicity we can think about gray, blue and red.
The problem is, when I think about making the sprite sheet, I'm not sure how to do it.
This is a quick (and bad drawing) of my first thoughts, being black the grid and green the cuts. Problem with this one is I would need to have up to 512 versions of the line crossing.
Is there a better approach? Can I do that without sprite sheets, just drawing lines and filling rectangles?
Sorry, I can't follow your thoughts completely. But, I understand you are handling a lot of squares and lines in different styles. And that's where you are right, you don't need any Sprites for that, AndEngine has some classes to draw simple things and it is way faster than Sprites.
basic example that reproduces your graphic with lines
// first the green lines (they are under the black ones)
Line[] greenLines = new Line[8];
// from (x0 ,y0) to (x1,y1) with lineWidth=5px
// the outer square
greenLines[0] = new Line(0, 0, 100, 0, 5, vertexBufferObjectManager); // top line
greenLines[1] = new Line(100, 0, 100, 100, 5, vertexBufferObjectManager); // right line
greenLines[2] = new Line(100, 100, 0, 100, 5, vertexBufferObjectManager); // bottom line
greenLines[3] = new Line(0, 100, 0, 0, 5, vertexBufferObjectManager); // left line
// inner horizontal lines
greenLines[4] = new Line(0, 33, 100, 33, 5, vertexBufferObjectManager);
greenLines[5] = new Line(0, 66, 100, 66, 5, vertexBufferObjectManager);
// inner vertical lines
greenLines[6] = new Line(33, 0, 33, 100, 5, vertexBufferObjectManager);
greenLines[7] = new Line(66, 0, 66, 100, 5, vertexBufferObjectManager);
// now the black lines
Line[] blackLines = new Line[4];
blackLines[0] = new Line(0, 15, 100, 15, 5, vertexBufferObjectManager);
blackLines[1] = new Line(0, 81, 100, 81, 5, vertexBufferObjectManager);
blackLines[2] = new Line(15, 0, 15, 100, 5, vertexBufferObjectManager);
blackLines[3] = new Line(81, 0, 81, 100, 5, vertexBufferObjectManager);
// now set the color and attach the lines to the scene (green)
for(Line line: greenLines){
line.setColor(0f,1f,0f);
myScene.attachChild(line);
}
// now set the color and attach the lines to the scene (black)
for(Line line: blackLines){
line.setColor(0f,0f,0f);
myScene.attachChild(line);
}
this above example should actually work. Now you only have to change it and adjust it to your needs. If you want to change a line you could call myLine.setPosition(fromX, fromY, toX, toY); oh and a rectangle is quite simple as well: Rectangle rectangle = new Rectangle(50,50,100,100, 5, vertexBufferObjectManager); for a rectangle that starts at (50,50) and is 100 pixels wide and 100 pixels high. And has a line width of 5 pixels. You can set the color of the Rectangle as you can for the lines. The only problem is, that the rectangle is always filled. If you want an empty rectangle you have to draw it with lines.
public Line buildGrid(int pWidth, int pHeight, float pRed, float pGreen, float pBlue){
Line grid = new Line(0, 0, 0, pHeight);
grid.setColor(0.5f, 0.5f, 0.5f);
int cont = 0;
while(cont < pWidth){
cont += 10;
grid.attachChild(new Line(cont, 0, cont, pHeight));
grid.getLastChild().setColor(pRed, pGreen, pBlue);
}
cont = 0;
while (cont < pHeight){
cont += 10;
grid.attachChild(new Line(0, cont, pWidth, cont));
grid.getLastChild().setColor(pRed, pGreen, pBlue);
}
return grid;
}
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