简体   繁体   中英

Draw text inside oval within another oval on canvas android

I have new requirement to draw oval within oval using android canvas.now done oval within oval but did not draw text inside oval.sample image added given below for reference. 在此处输入图片说明

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM