drawArc方法:绘制圆弧

【功能说明】该方法用于在画布上绘制圆弧,通过指定圆弧所在的椭圆对象、起始角度、终止角度来实现。该方法是绘制圆弧的主要方法。

【基本语法】public void drawArc (RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

参数说明

oval:圆弧所在的椭圆对象。

startAngle:圆弧的起始角度。

sweepAngle:圆弧的角度。

useCenter:是否显示半径连线,true表示显示圆弧与圆心的半径连线,false表示不显示。

paint:绘制时所使用的画笔。

【实例演示】下面通过代码来演示如何在画布上绘制圆弧。

protected void onDraw(Canvas canvas) {  
    // TODO Auto-generated method stub  
    super.onDraw(canvas);  
    paint.setAntiAlias(true);                       //设置画笔为无锯齿  
    paint.setColor(Color.BLACK);                    //设置画笔颜色  
    canvas.drawColor(Color.WHITE);                  //白色背景  
    paint.setStrokeWidth((float) 3.0);              //线宽  
    paint.setStyle(Style.STROKE);  

    RectF oval=new RectF();                     //RectF对象  
    oval.left=100;                              //左边  
    oval.top=100;                                   //上边  
    oval.right=400;                             //右边  
    oval.bottom=300;                                //下边  
    canvas.drawArc(oval, 225, 90, false, paint);    //绘制圆弧  

    //RectF oval=new RectF();                       //RectF对象  
    oval.left=100;                              //左边  
    oval.top=400;                                   //上边  
    oval.right=400;                             //右边  
    oval.bottom=700;                                //下边  
    canvas.drawArc(oval, 200, 135, true, paint);    //绘制圆弧  
}  

在这段代码中,首先设置了Paint画笔的颜色,并设置Canvas画布为白色背景。接着设置画笔的线宽以及空心效果。然后,定义一个RectF对象,并设置了其坐标,调用drawArc方法绘制第一个圆弧,这里设置不显示半径连线。最后,重新设置了RectF对象坐标,调用drawArc方法绘制第二个圆弧,这里设置显示半径连线。读者运行这段代码,可以在手机屏幕上看到如图8.22所示的显示效果。
图8.22

原文

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
package com.example.task; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; public class CircleProgressBar extends View { private int maxProgress = 100; private int progress = 10; private int progressStrokeWidth = 20; // 画圆所在的距形区域 RectF oval; Paint paint; public CircleProgressBar(Context context, AttributeSet attrs) { super(context, attrs); // TODO 自动生成的构造函数存根 oval = new RectF(); paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { // TODO 自动生成的方法存根 super.onDraw(canvas); int width = this.getWidth(); int height = this.getHeight(); if (width != height) { int min = Math.min(width, height); width = min; height = min; } paint.setAntiAlias(true); // 设置画笔为抗锯齿 paint.setColor(0xFFE2E2E2); // 设置画笔颜色 canvas.drawColor(Color.TRANSPARENT); // 白色背景 paint.setStrokeWidth(progressStrokeWidth); // 线宽 paint.setStyle(Style.STROKE); oval.left = progressStrokeWidth / 2; // 左上角x oval.top = progressStrokeWidth / 2; // 左上角y oval.right = width - progressStrokeWidth / 2; // 左下角x oval.bottom = height - progressStrokeWidth / 2; // 右下角y canvas.drawArc(oval, -90, 360, false, paint); // 绘制白色圆圈,即进度条背景 // paint.setColor(Color.rgb(0x57, 0x87, 0xb6)); paint.setColor(0xFFFF4700); canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint); // 绘制进度圆弧,这里是蓝色 paint.setStrokeWidth(1); String text = progress + "分"; int textHeight = height / 4; paint.setTextSize(textHeight); int textWidth = (int) paint.measureText(text, 0, text.length()); paint.setStyle(Style.FILL); // canvas.drawText(text, width / 2 - textWidth / 2, height / 2 // + textHeight / 2, paint); } public int getMaxProgress() { return maxProgress; } public void setMaxProgress(int maxProgress) { this.maxProgress = maxProgress; } public void setProgress(int progress) { this.progress = progress; this.invalidate(); } /** * 非UI线程调用 */ public void setProgressNotInUiThread(int progress) { this.progress = progress; this.postInvalidate(); } } 上面的代码就是画两个圆弧 package com.example.task; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity { CircleProgressBar completedView1; CircleProgressBar completedView2; CircleProgressBar completedView3; CircleProgressBar completedView4; CircleProgressBar completedView5; CircleProgressBar completedView6; CircleProgressBar completedView7; CircleProgressBar completedView8; CircleProgressBar completedView9; CircleProgressBar completedView10; CircleProgressBar completedView11; CircleProgressBar completedView12; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); completedView1 = (CircleProgressBar) findViewById(R.id.circleProgressBar1); completedView2 = (CircleProgressBar) findViewById(R.id.circleProgressBar2); completedView3 = (CircleProgressBar) findViewById(R.id.circleProgressBar3); completedView4 = (CircleProgressBar) findViewById(R.id.circleProgressBar4); completedView5 = (CircleProgressBar) findViewById(R.id.circleProgressBar5); completedView6 = (CircleProgressBar) findViewById(R.id.circleProgressBar6); completedView7 = (CircleProgressBar) findViewById(R.id.circleProgressBar7); completedView8 = (CircleProgressBar) findViewById(R.id.circleProgressBar8); completedView9 = (CircleProgressBar) findViewById(R.id.circleProgressBar9); completedView10 = (CircleProgressBar) findViewById(R.id.circleProgressBar10); completedView11 = (CircleProgressBar) findViewById(R.id.circleProgressBar11); completedView12 = (CircleProgressBar) findViewById(R.id.circleProgressBar12); this.findViewById(R.id.button_show).setOnClickListener( new OnClickListener() { private int i2 = 40; @Override public void onClick(View v) { // completedView1.setProgressNotInUiThread(i2 % 100); // completedView2.setProgressNotInUiThread(i2 % 100); // completedView3.setProgressNotInUiThread(i2 % 100); // completedView4.setProgressNotInUiThread(i2 % 100); // completedView5.setProgressNotInUiThread(i2 % 100); // completedView6.setProgressNotInUiThread(i2 % 100); // completedView7.setProgressNotInUiThread(i2 % 100); // completedView8.setProgressNotInUiThread(i2 % 100); // completedView9.setProgressNotInUiThread(i2 % 100); // completedView10.setProgressNotInUiThread(i2 % 100); // completedView11.setProgressNotInUiThread(i2 % 100); // completedView12.setProgressNotInUiThread(i2 % 100); // i2 += 10; new Thread() { public void run() { int i = 0; while (i <= i2) { completedView1.setProgressNotInUiThread(i); completedView2.setProgressNotInUiThread(i); completedView3.setProgressNotInUiThread(i); completedView4.setProgressNotInUiThread(i); completedView5.setProgressNotInUiThread(i); completedView6.setProgressNotInUiThread(i); completedView7.setProgressNotInUiThread(i); completedView8.setProgressNotInUiThread(i); completedView9.setProgressNotInUiThread(i); completedView10.setProgressNotInUiThread(i); completedView11.setProgressNotInUiThread(i); completedView12.setProgressNotInUiThread(i); i++; try { sleep(10); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } }.start(); i2 += 10; } }); } } 上面的主活动界面使用线程画图

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值