How to draw circle with partitioned in android? - 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);

Related

Draw canvas drawRect with dynamic co ordinates

I am having canvas which will generate rectangle co-ordinates in image.
In this image need to draw rectangle shape on above Icon using canvas co-ordinates
My sample code
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.e("PICASA", "Loaded");
setImageBitmap(bitmap);
Bitmap drawableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(drawableBitmap);
List<ClickableArea> clickableAreas = IBSTFragment.getClickableAreas();
for (ClickableArea clickArea : clickableAreas) {
Paint paint = new Paint();
paint.setColor(Color.TRANSPARENT);
paint.setStyle(Paint.Style.FILL);
int x1 = clickArea.getX();
int y1 = clickArea.getY();
int w = clickArea.getW();
int h = clickArea.getH();
Rect rect = new Rect(x1, y1, w, h);
// FILL
canvas.drawRect(rect, paint);
paint.setStrokeWidth(10);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(rect, paint);
rect.width();
rect.height();
}
setImageBitmap(drawableBitmap);
}
My Co-ordinates
X Y Width Height
600, 100, 50, 50
440, 125, 50, 50
685, 270, 50, 50
420, 350, 50, 50
370, 245, 50, 50
170, 280, 50, 50
30, 280, 50, 50
570, 250, 50, 50
I got output like this
You are copying the Bitmap into memory and never displaying, draw on the one that you set as ImageResource:
setImageBitmap(bitmap);
Canvas canvas = new Canvas(bitmap);
If you need to copy as ARGB_8888 then
Bitmap drawableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(drawableBitmap);
//Draw everything, then after (at end of method)
setImageBitmap(drawableBitmap);
As for your coordinates:
X Y Width Height
600, 100, 50, 50
440, 125, 50, 50
685, 270, 50, 50
420, 350, 50, 50
...
They are (X,Y,W,H) while Android Rectangles are (L,T,R,B)
To convert your coordinates use:
Rectangle area = new Rectangle(x, y, x + w, y + h);
Then draw the area into the Canvas.

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

RectF is not painted on canvas

I have a small question. I'm painting some RectFs on my canvas, but nothing happened.
I paint one filled RectF and another stroked RectF => filled background with a border.
For the first "field" everything is ok, but not for the second "field" (rect2).
The positions are checked. The last text is printed in that RectF (rect2) where the rect is not painted.
I uploaded an image on my FTP server. For the description of my problem it might be easier to understand.
Here is my code. I think it is a logical error?
// Fill rect
rect.set(fPosition, fPositionY, pts[2] - 10, fRectHeight);
paint.setColor(Color.GREEN);
paint.setStyle(android.graphics.Paint.Style.FILL);
paint.setStrokeWidth(0);
canvas.drawRoundRect(rect, 10, 10, paint);
// Border
paint.setColor(Color.BLACK);
paint.setStyle(android.graphics.Paint.Style.STROKE);
paint.setStrokeWidth(3);
canvas.drawRoundRect(rect, 10, 10, paint);
// Text
float fTextSize = fRectHeight - 40;
TextPaint textPaint = new TextPaint();
textPaint.setColor(Color.BLACK);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(fTextSize);
float textHeight = textPaint.descent() - textPaint.ascent();
float textOffset = (textHeight / 2) - textPaint.descent();
canvas.drawText(dataStrings.getGewicht(), rect.centerX(), rect.centerY() + textOffset, textPaint);
// Leistung pro Stunde
rect2 = new RectF();
paint = new android.graphics.Paint();
fPositionY = fPositionY + fRectHeight + 50;
float fRectWidth = pts[2] / 2;
fRectHeight = (float) (pts[17] / 5);
// Fill rect
rect2.set(fPosition, fPositionY, fRectWidth, fRectHeight);
paint.setColor(Color.YELLOW);
paint.setStyle(android.graphics.Paint.Style.FILL);
paint.setStrokeWidth(0);
canvas.drawRoundRect(rect2, 10, 10, paint);
// Border
paint.setColor(Color.BLACK);
paint.setStyle(android.graphics.Paint.Style.STROKE);
paint.setStrokeWidth(3);
canvas.drawRoundRect(rect2, 10, 10, paint);
// Text
fTextSize = fRectHeight - 40;
textPaint = new TextPaint();
textPaint.setColor(Color.BLACK);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(fTextSize);
textHeight = textPaint.descent() - textPaint.ascent();
textOffset = (textHeight / 2) - textPaint.descent();
canvas.drawText(dataStrings.getLeistung(), rect2.centerX(), rect2.centerY() + textOffset, textPaint);

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!

Paint : How to draw arc using gradient image or Bitmap image

i am able to draw arc using Paint and passing some gradient color to it, my problem is that i need draw the arc using a gradient image.Is it possible to draw arc using an image?
If so how to do it?
this is my current code:
Paint nPaint = new Paint();
nPaint.setAntiAlias(true);
nPaint.setDither(true);
nPaint.setStrokeJoin(Paint.Join.ROUND);
nPaint.setStrokeCap(Paint.Cap.ROUND);
int gradientStart = getResources().getInteger(R.integer.gradient_start);
int gradientend = getResources().getInteger(R.integer.gradient_end);
nPaint.setShader(new RadialGradient(getWidth() / 2, getHeight() / 2, getWidth() / 2,
gradientStart, gradientend, Shader.TileMode.CLAMP));
Try this code i hope this will help you
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.CYAN);
Paint p = new Paint();
// smooths
p.setAntiAlias(true);
p.setColor(Color.RED);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(5);
// opacity
//p.setAlpha(0x80); //
RectF rectF = new RectF(50, 20, 100, 80);
canvas.drawOval(rectF, p);
p.setColor(Color.BLACK);
canvas.drawArc (rectF, 90, 45, true, p);
}

Categories

Resources