How to draw a graphics in view on android platform - android

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

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: Drawing 2D Rectangle over a Background Image

I'm trying to draw a 2D Rectangle on my Android project. It's working great, but the only problem is that I'm using a Background image for my project. The 2D Rectangle won't allow the Background image to show. For example, if I draw the rectangle before showing the Background image, the Background will hide the rectangle. It'll do the same if I show the Background first and then draw the rectangle, only this time the rectangle will hide the Background image completely.
public class DrawView extends View
{
Paint paint = new Paint();
public DrawView(Context context)
{
super(context);
}
#Override
public void onDraw(Canvas canvas)
{
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(30, 30, 80, 80, paint);
paint.setColor(Color.CYAN);
canvas.drawRect(33, 60, 77, 77, paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(33, 33, 77, 60, paint);
}
}
On the main form:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
DrawView drawview = new DrawView(this);
setContentView(drawview);
setContentView(R.layout.activity_login);
}
Try to add super.onDraw() into your onDraw() override, to make view's onDraw() execute before you start draw custom elements on it:
#Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(30, 30, 80, 80, paint);
paint.setColor(Color.CYAN);
canvas.drawRect(33, 60, 77, 77, paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(33, 33, 77, 60, paint);
}
I've figured it out. All that's left is changing the rectangle's location.
Add the following:
addContentView(drawview, new LayoutParams(Width, Height));
And remove the setContentView() command.

Arc on canvas has no antialiasing

I want to draw two colored circle on canvas. Everything is right but the circle isn't smooth.
This is the effect:
And this is code:
private void drawCircle(Canvas c)
{
RectF oval = new RectF(20, 20, 100, 100);
c.drawArc(oval, -90, 180, false, getPaintWithColor(R.color.background));
c.drawArc(oval, 90, 180, false, getPaintWithColor(R.color.font_grey));
}
private Paint getPaintWithColor(int colorId){
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(4);
paint.setColor(getResources().getColor(colorId));
return paint;
}
paint.setFlags(Paint.ANTI_ALIAS_FLAG)
See in Can I draw with antialiasing on canvas?

Drawing double colored square on Android canvas transparent rectangle

I have several lines in my application. If somebody touches a line I have to highlight the line touched. I think if I could draw a transparent rectangle with light color, other than the clicked line color, then it will be highlighted properly.. So can anybody tell me how can I draw a transparent rectangle on Android canvas?
My line color is black. Please see the pic.
This will draw green 50% transparent rectangle on canvas:
Paint myPaint = new Paint();
myPaint.setStyle(Paint.Style.FILL);
myPaint.setColor(Color.rgba(0, 256, 0, 128)); // green color with 50% transparency
// c is your canvas
c.drawRect(100, 100, 200, 200, myPaint);
try this way
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(3);
mPaint.setPathEffect(null);
canvas.drawRect(x, y, x + width, y + height, mPaint);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(3);
mPaint.setPathEffect(new DashPathEffect(new float[] { 5, 5 }, 5));
canvas.drawRect(x, y, x + width, y + height, mPaint);
you can use this:
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.MAGENTA);
DashPathEffect dashPath =new DashPathEffect(new float[ ]{70,2}, 30);
paint.setPathEffect(dashPath);
paint.setStrokeWidth(80);
canvas.drawRect(30, 300 , 320, 300, paint);

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