2015-04-02 245 views
0

我有新的要求,使用android canvas.now在橢圓內繪製橢圓,但沒有繪製橢圓內的文本添加下面給出的參考。 enter image description here在橢圓內繪製文本橢圓內的另一個橢圓在畫布上android

+0

可能重複[如何使用Android XML來畫一個圓圈裏面一個圈形狀?](http://stackoverflow.com/questions/21613308/how-to-draw-a-circle-inside-a-circle-using-android-xml-shapes) – 2015-04-02 11:57:15

回答

1

爲()分辨率(480×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); 
    } 

}