功能:该方法用于在画布上绘制圆形,通过指定圆形圆心的坐标和半径来实现。该方法是绘制圆形的主要方法,同时也可以通过设置画笔的空心效果来绘制空心的圆形。
基本语法:public void drawCircle (float cx, float cy, float radius, Paint paint)
参数说明
cx:圆心的x坐标。
cy:圆心的y坐标。
radius:圆的半径。
paint:绘制时所使用的画笔。
源码分析:
/**
* Draw the specified circle using the specified paint. If radius is <= 0,
* then nothing will be drawn. The circle will be filled or framed based
* on the Style in the paint.//使用指定的画笔画出特定的圆圈,如果半径小于等于0将什么都不会画,这个圆圈将被填充或者基于画笔风格的架构画
*
* @param cx The x-coordinate of the center of the cirle to be drawn//被画的圆圈的中心X坐标
* @param cy The y-coordinate of the center of the cirle to be drawn//被画的圆圈的中心y坐标
* @param radius The radius of the cirle to be drawn//被画圆圈的中心半径
* @param paint The paint used to draw the circle//画笔
*/
public void drawCircle(float cx, float cy, float radius, Paint paint) {
native_drawCircle(mNativeCanvas, cx, cy, radius, paint.mNativePaint);
}
例子:下面通过代码来演示如何在画布上绘制圆形。
1. protected void onDraw(Canvas canvas) {
2. // TODO Auto-generated method stub
3. super.onDraw(canvas);
4. paint.setAntiAlias(true); //设置画笔为无锯齿
5. paint.setColor(Color.BLACK); //设置画笔颜色
6. canvas.drawColor(Color.WHITE); //白色背景
7. paint.setStrokeWidth((float) 3.0); //线宽
8. paint.setStyle(Style.STROKE); //空心效果
9.
10. canvas.drawCircle(50, 50, 10, paint); //绘制圆形
11. canvas.drawCircle(100, 100, 20, paint); //绘制圆形
12. canvas.drawCircle(150, 150, 30, paint); //绘制圆形
13. canvas.drawCircle(200, 200, 40, paint); //绘制圆形
14. canvas.drawCircle(250, 250, 50, paint); //绘制圆形
15. canvas.drawCircle(300, 300, 60, paint); //绘制圆形
16. canvas.drawCircle(350, 350, 70, paint); //绘制圆形
17. }
在这段代码中,首先设置了Paint画笔的颜色,并设置Canvas画布为白色背景。接着设置画笔的线宽以及空心效果,这样将绘制出空心圆形。最后,调用drawCircle方法绘制了多个圆形,坐标和半径都在不断变化。
这些我们一般设置为工具类的形式