Android canvas, how to draw something under an existing shape? - android

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);

Related

How to draw multiple bitmap using paint?

I am trying to draw multiple bitmap using this code.
Paint paint = new Paint();
Path path = new Path(); // path of crop
for (int i = 0; i < Crop.points.size(); i++) {
path.lineTo(Crop.points.get(i).x, Crop.points.get(i).y);
canvas.drawPath(path, paint);
bitmap1= highlightImage(bitmap1); //bitmap1 drawn completely
canvas.drawBitmap(bitmap1, 0, 0, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// select inside part of crop
canvas.drawBitmap(bitmap2, 0, 0, paint);// draw crop part
highlightImage is a method for blurring a bitmap. This canvas first draw bitmap1 that is blurred and then draw bitmap2.
The problem is, when I use only this code:
bitmap1= highlightImage(bitmap1);
canvas.drawBitmap(bitmap1, 0, 0, paint);
the bitmap1 is blurred and drawn correctly, but when I use this
bitmap1= highlightImage(bitmap1);
canvas.drawBitmap(bitmap1, 0, 0, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap2, 0, 0, paint);
the bitmap1 is drawn but without the blur effect. What's the problem?
the first image is the image of blur bitmap
the second one is for the time that two bit map is drawn. you can see that is transparent without blur effect.
[first ][1]
[second ][2]
[what I want][3]

How to draw a graphics in view on android platform

I want to draw a view like the picture above,but I'm stuck in how to draw the outline, I can only draw a circle, I cannot add two ears ,so someone can help me, I also want to draw a progress.
Draw line it just
#Override
public void onDraw(Canvas canvas) {
//canvas.drawLine(sx, sy, fx, fy, paint);
canvas.drawLine(20, 0, 0, 20, paint);
}
If you are asking how to draw an arc, then you really need to use the Path.
or this code:
canvas.drawColor(Color.CYAN);
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.RED);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(5);
RectF rectF = new RectF(50, 20, 100, 80);
canvas.drawOval(rectF, p);
p.setColor(Color.BLACK);
canvas.drawArc (rectF, 90, 45, true, p);

Vertical LinearGradient with Android in Java

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.

StrokeJoin with drawLine? or Gradient on path?

I try to draw a comet, by drawLine() with alpha Gradient, but the lines won't connect perfectly like in this image:
http://s14.postimg.org/y5w6pgl6p/Screenshot_2013_07_09_16_14_21.png
the path could be like S shape or C or (keep moving)
using this code :
paint.setStrokeJoin(Paint.Join.BEVEL);
paint.setStrokeWidth(20);
paint.setShader(new LinearGradient(100,200,150,200, 0x00ffff00,0x44ffff00, Shader.TileMode.CLAMP));
canvas.drawLine(100, 200, 150, 200, paint);
paint.setShader(new LinearGradient(150,200,200,220, 0x44ffff00,0x88ffff00, Shader.TileMode.CLAMP));
canvas.drawLine(150, 200, 200, 220, paint);
paint.setShader(new LinearGradient(200, 220, 230, 230, 0x88ffff00, 0xccffff00, Shader.TileMode.CLAMP));
canvas.drawLine(200, 220, 230, 260, paint);
paint.setShader(new LinearGradient(230, 230, 230, 280, 0xccffff00, 0xffffff00, Shader.TileMode.CLAMP));
canvas.drawLine(230,260,230,310,paint);
setStrokeJoin() didn't help for drawLine/s
I converted the Lines into a Path, but the Gradient didn't curve with the whole path
I also draw shapes instead of Lines, to fill the empty space between them, but it took a lot time and CPU to draw on an animated lines.
Any other ideas ?
Path path = new Path();
path.moveTo(100, 200);
path.lineTo(150, 200);
path.lineTo(200, 220);
path.lineTo(230, 260);
path.lineTo(230,310);
Paint paint = new Paint();
paint.setShader(new LinearGradient(100,200,230,310, 0x00ffff00,0x44ffff00, TileMode.CLAMP));
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(50);
canvas.drawPath(path, paint);
Hope it helps!

Draw a border (Paint) on current clip (created by different Region.Op)

I want to draw an image into shape of a Path and then add border on the Path. I was able to clip the image with Path but can't find a way to add border on it. I though it would be simple because the API supports Paint object on Canvas.draw* methods.
I asked another question at: Draw bitmap on current clip in canvas with border (Paint) and I accepted the answer. However, after that I found that I need to do a little bit more complicated processing. Because I use two options for clipping instead of one.
Below is my code to clip and image with two different Region.Op parameters
Bitmap srcImage = BitmapFactory.decodeStream(getAssets().open("panda.jpg"));
Bitmap bitmapResult = Bitmap.createBitmap(srcImage.getWidth(), srcImage.getHeight(), Bitmap.Config.ARGB_8888);
Path path = new Path();
// This is my border
Paint paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setColor(Color.RED);
paint.setStrokeWidth(2);
paint.setAntiAlias(true);
Canvas canvas = new Canvas(bitmapResult);
// Overlay two rectangles
path.addRect(10, 10, 70, 70, Path.Direction.CCW);
path.addRect(40, 40, 120, 120, Path.Direction.CCW);
canvas.drawPath(path , paint);
canvas.clipPath(path, Region.Op.INTERSECT);
// Draw the circle
path.reset();
path.addCircle(40, 80, 20, Path.Direction.CCW);
canvas.drawPath(path , paint);
canvas.clipPath(path, Region.Op.DIFFERENCE);
// The image is drawn within the area of two rectangles and a circle
// Although I suppose that puting Paint object into drawBitmap() method will add a red border on result image but it doesn't work
canvas.drawBitmap(srcImage, 0, 0, paint);
((ImageView)this.findViewById(R.id.imageView1)).setImageBitmap(bitmapResult);
Here is the result from my code: http://i.stack.imgur.com/8j2Kg.png
And this is what I expect: http://i.stack.imgur.com/iKhIr.png
Do I miss anything to make it work ?

Categories

Resources