Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

draw an oval shape around the text on canvas

Tags:

android

I want to draw an oval shape around the text on Canvas, I am displaying the 3 texts on Canvas using drawwText() method.

Now when I click on a particular text, I need to draw an oval around that text and again when we click on another text, the oval shape should appear on the clicked text. For this give me some code suggestions.Thanks in advance

like image 643
Pinki Avatar asked Feb 16 '11 05:02

Pinki


2 Answers

use drawOval method().. here is the signature of the method..

public void drawOval (RectF oval, Paint paint)  

RectF is class for drawing rectangle...whose constructor is defined as following...

RectF(x,y,x+width,y+height); 

you can make its object as follows

RectF rect = new RectF(x,y,x+width,y+height);... 

now pass this object in drawOval method....

canvas.drawOval(rect,paint);  
like image 115
N-JOY Avatar answered Oct 25 '22 20:10

N-JOY


for resolution (480 x 800)

in onCreate()

setContentView(new SampleView(this));

create class

private static class SampleView extends View {

    // CONSTRUCTOR
    public SampleView(Context context) {
        super(context);
        setFocusable(true);

    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawColor(Color.WHITE);

        //1
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.GRAY);
        RectF oval1 = new RectF(0, 0, 250,250);

        Paint p1 = new Paint();
        p1.setColor(Color.BLACK);

        canvas.drawText("Parent", 30, 50, p1);
        canvas.drawOval(oval1, paint);


        //2
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLUE);
        RectF oval2 = new RectF(50, 50, 150, 150);

        Paint p2 = new Paint();
        p2.setColor(Color.GREEN);

        canvas.drawText("Child", 75, 75, p2);
        canvas.drawOval(oval2, paint);
    }

}
like image 29
krunal shah Avatar answered Oct 25 '22 21:10

krunal shah