在画布上的文字周围画一个椭圆形

时间:2011-02-16 05:04:37

标签: android

我想在Canvas上的文字周围画一个椭圆形状,我使用Canvas方法在drawwText()上显示3个文字。

现在,当我点击特定文本时,我需要在该文本周围绘制一个椭圆形,当我们点击另一个文本时,椭圆形状应该出现在单击的文本上。为此,给我一些代码建议。谢谢提前

2 个答案:

答案 0 :(得分:25)

使用drawOval方法()..这里是方法的签名..

public void drawOval (RectF oval, Paint paint)  

RectF是绘制矩形的类......其构造函数定义如下......

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

你可以按如下方式制作其对象

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

现在在drawOval方法中传递此对象....

canvas.drawOval(rect,paint);  

答案 1 :(得分:1)

分辨率(480 x 800)

onCreate()

中的

setContentView(new SampleView(this));

创建课程

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

}