StrokeJoin with drawLine? or Gradient on path? - android

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!

Related

How to draw dashed border around bitmap

I have a bitmap of cup image and i want to add dashed border around cup.
Here is what i did try, but it doesn't work :
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setARGB(255, 0, 0, 0);
paint.setStyle(Paint.Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[]{5, 10, 15, 20}, 0));
canvas.drawBitmap(cupBitmap, x, y, paint);

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

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

How to set stroke color to draw a rectangle on canvas?

I want to draw a round rectangle which its stroke is blue and its fill is red, but I can't find a method in Paint class to set stroke color. How can I do that?
mCanvas.drawColor(mBackgroundColor, PorterDuff.Mode.CLEAR);
mCanvas.setDrawFilter(mPaintFlagsDrawFilter);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(2);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mRectF.set(0, 0, mWidth, mHeight);
mCanvas.drawRoundRect(mRectF, 10, 10, mPaint);
Paint only allows for one color at a time.
mCanvas.drawColor(mBackgroundColor, PorterDuff.Mode.CLEAR);
mCanvas.setDrawFilter(mPaintFlagsDrawFilter);
mFillPaint.setStyle(Paint.Style.FILL);
mFillPaint.setColor(Color.RED);
mStrokePaint.setStyle(Paint.Style.STROKE);
mStrokePaint.setColor(Color.BLUE);
mStrokePaint.setStrokeWidth(2);
mStrokePaint.setStrokeCap(Paint.Cap.ROUND);
mRectF.set(0, 0, mWidth, mHeight);
mCanvas.drawRoundRect(mRectF, 10, 10, mFillPaint);
mCanvas.drawRoundRect(mRectF, 10, 10, mStrokePaint);
If you find your rounded rectangle doesn't look right, it may be clipped at the bounds of the view. Adjust the RectF to allow for half the StrokeWidth:
mRectF.set(1, 1, mWidth - 1, mHeight - 1);

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

How to draw circle with partitioned in android?

I want to draw this type of circle in my application. I am able to draw circle using Canvas but I can't get any idea about how to make partitioned?
Can anyone suggest me how can I make partitioned of circle?
Edit:- I want to draw line that are in inner circle.
Thanks in advance.
Here's the working code for your requirement....
Editing the code:-
Paint paint1 = new Paint();
Paint paint2 = new Paint();
Paint paint3 = new Paint();
Paint paint4 = new Paint();
Paint paint5 = new Paint();
final RectF rect = new RectF();
int mRadius = 130;
//Example values
rect.set(getWidth()/2- mRadius, getHeight()/2 - mRadius, getWidth()/2 + mRadius, getHeight()/2 + mRadius);
paint1.setColor(Color.GREEN);
paint1.setStrokeWidth(mRadius/2);
paint1.setAntiAlias(true);
paint1.setStrokeCap(Paint.Cap.BUTT);
paint1.setStyle(Paint.Style.STROKE);
paint2.setColor(Color.RED);
paint2.setStrokeWidth(mRadius/2);
paint2.setAntiAlias(true);
paint2.setStrokeCap(Paint.Cap.BUTT);
paint2.setStyle(Paint.Style.STROKE);
paint3.setColor(Color.BLUE);
paint3.setStrokeWidth(5);
paint3.setAntiAlias(true);
paint3.setStrokeCap(Paint.Cap.BUTT);
paint3.setStyle(Paint.Style.STROKE);
canvas.drawArc(rect, 0, 60, false, paint1);
canvas.drawArc(rect, 60, 60, false, paint2);
canvas.drawArc(rect, 120, 60, false, paint1);
canvas.drawArc(rect, 180, 60, false, paint2);
canvas.drawArc(rect, 240, 60, false, paint1);
canvas.drawArc(rect, 300, 60, false, paint2);
canvas.drawLine(getWidth()/2,
getHeight()/2, getWidth()/2-mRadius/2, getHeight()/2-mRadius/2,paint3);
canvas.drawLine(getWidth()/2,
getHeight()/2, getWidth()/2+mRadius/2, getHeight()/2-mRadius/2,paint3);
canvas.drawLine(getWidth()/2,
getHeight()/2, getWidth()/2-mRadius/2, getHeight()/2+mRadius/2,paint3);
canvas.drawLine(getWidth()/2,
getHeight()/2, getWidth()/2+mRadius/2, getHeight()/2+mRadius/2,paint3);
canvas.drawLine(getWidth()/2,
getHeight()/2, getWidth()/2-mRadius/4-mRadius/2, getHeight()/2,paint3);
canvas.drawLine(getWidth()/2,
getHeight()/2, getWidth()/2+mRadius/4+mRadius/2, getHeight()/2,paint3);
paint4.setColor(Color.BLACK);
canvas.drawCircle(getWidth()/2, getHeight()/2, mRadius/2, paint4);
paint5.setColor(Color.YELLOW);
paint5.setStrokeWidth(3);
paint5.setAntiAlias(true);
paint5.setStrokeCap(Paint.Cap.BUTT);
paint5.setStyle(Paint.Style.STROKE);
canvas.drawCircle(getWidth()/2, getHeight()/2, mRadius/2, paint5);
I hope now you satisfy with my answer....
I have an idea first draw inside circle with partition using
can.drawArc(oval, startAngle, sweepAngle, useCenter, paint)
Take angle value like 0 t0 60, and then again draw another arc with same center take angle value from 60 to 120 and so on.Every time set different color in Paint.After completion of inside circle, almost all work done.Now draw white circle with same center but small radius after first circle.So it will create over first
Hope it will help you :)
Hey I found the solution of my query,
final RectF rect1 = new RectF();
int mWidth = this.getWidth()/2;
int mHeight = this.getHeight()/2;
int mRadius = 130, mRadius1 = 50;
rect1.set(mWidth -(mRadius-mRadius1), mHeight - (mRadius-mRadius1), mWidth + (mRadius-mRadius1), mHeight + (mRadius-mRadius1));
Paint paintLines = new Paint();
paintLines.setColor(Color.BLACK);
paintLines.setStrokeWidth((mRadius-mRadius1)/2);
paintLines.setAntiAlias(false);
paintLines.setStrokeCap(Paint.Cap.BUTT);
paintLines.setStyle(Paint.Style.STROKE);
canvas.drawArc(rect1, 0, 1, false, paintLines);
canvas.drawArc(rect1, 30, 1, false, paintLines);
canvas.drawArc(rect1, 60, 1, false, paintLines);
canvas.drawArc(rect1, 90, 1, false, paintLines);
canvas.drawArc(rect1, 120, 1, false, paintLines);
canvas.drawArc(rect1, 150, 1, false, paintLines);

Categories

Resources