要实现圆形进度条只能使用自定义view了,实现代码比较简单,自定义view代码

package com.example.apple.memory;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;/** * Created by apple on 17/5/26. */public class CircleProgressView extends View {    private int mMaxProgress = 100;    private int Progress = 0;    private int Progress1 = 0;    private int Progress2 = 0;    private final int mCircleLineStrokeWidth = 100;//设置圆形画笔宽度    private final int mTxtStrokeWidth =  2;//设置文字画笔宽度    // 画圆所在的距形区域    private  RectF mRectF;    private  Paint mPaint;    public CircleProgressView(Context context, AttributeSet attrs) {        super(context, attrs,0);        mRectF = new RectF();        mPaint = new Paint();    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        int width = this.getWidth();        int height = this.getHeight();        if (width != height) {            int min = Math.min(width, height);            width = min;            height = min;        }        // 设置画笔相关属性        mPaint.setAntiAlias(true);        mPaint.setColor(Color.WHITE);        canvas.drawColor(Color.TRANSPARENT);        mPaint.setStrokeWidth(mCircleLineStrokeWidth);        mPaint.setStyle(Paint.Style.STROKE);        // 位置        mRectF.left = mCircleLineStrokeWidth / 2;        mRectF.top = mCircleLineStrokeWidth / 2;        mRectF.right = width - mCircleLineStrokeWidth / 2;        mRectF.bottom = height - mCircleLineStrokeWidth / 2;        // 绘制圆圈,进度条背景        canvas.drawArc(mRectF, -180, 360, false, mPaint);        mPaint.setColor(Color.RED);        canvas.drawArc(mRectF, -180, ((float) Progress / mMaxProgress) * 360, false, mPaint);        //绘制第二段长度        mPaint.setColor(Color.BLUE);        canvas.drawArc(mRectF,-180+ ((float) Progress / mMaxProgress) * 360, ((float) Progress1 / mMaxProgress) * 360, false, mPaint);        //绘制第三段长度        mPaint.setColor(Color.BLACK);        canvas.drawArc(mRectF,-180+ ((float) (Progress+Progress1) / mMaxProgress) * 360, ((float) Progress2 / mMaxProgress) * 360, false, mPaint);        //绘制百分百        mPaint.setStrokeWidth(mTxtStrokeWidth);        String text = Progress + "%";        int textHeight = height / 4;        mPaint.setTextSize(textHeight);        int textWidth = (int) mPaint.measureText(text, 0, text.length());        mPaint.setStyle(Paint.Style.FILL);        canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, mPaint);    }    public int getMaxProgress() {        return mMaxProgress;    }    public void setMaxProgress(int maxProgress) {        this.mMaxProgress = maxProgress;    }    public void setProgress(int progress) {        this.Progress = progress;        this.invalidate();    }    public void setProgress1(int progress) {        this.Progress1 = progress;        this.invalidate();    }    public void setProgress2(int progress) {        this.Progress2 = progress;        this.invalidate();    }}
2、引用的话首先布局中直接添加就好

<?xml version="1.0" encoding="utf-8"?>    
3、代码中使用,为了看到有个动画效果所以加了个定时器,可以看到慢慢增加到指定值。

package com.example.apple.memory;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {    private CircleProgressView circleProgressView;    private final int UPDATE_PROGRESS = 0;    private int number = 0;    private Handler mHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            switch (msg.what) {                case UPDATE_PROGRESS:                    circleProgressView.setProgress(2 + number);                    circleProgressView.setProgress1(10 + number);                    circleProgressView.setProgress2(20 + number);                    break;            }            super.handleMessage(msg);        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        circleProgressView = (CircleProgressView) findViewById(R.id.circleProgressBar);        circleProgressView.setMaxProgress(100);        new TestThread().start();    }    class TestThread extends Thread {        @Override        public void run() {            while (number < 20) {                number++;                mHandler.sendEmptyMessage(UPDATE_PROGRESS);                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }    @Override    protected void onDestroy() {        if (mHandler != null) {            mHandler.removeCallbacksAndMessages(0);            mHandler = null;        }        super.onDestroy();    }}

实现效果

android 实现圆形进度条_第1张图片

更多相关文章

  1. android 关于gallery 的平滑代码滚动
  2. Android客户端自动更新代码
  3. [CSDN]Android应用程序启动过程源代码分析
  4. android连续按两次返回退出程序(完整代码)
  5. Android Java代码执行adb Shell命令
  6. 探寻 Android 代码抄袭细节,情节还不算严重
  7. Android清除本地数据缓存代码案例
  8. 【Android学习笔记】AutoCompleteTextView(数组在代码中)
  9. Android 高通代码预制apk可卸载,恢复出厂设置apk可恢复 Android M

随机推荐

  1. Android(安卓)限制EditText只能输入数字
  2. android布局居中
  3. Android布局LinearLayout子控件无法填充
  4. android 二 BMI 小例子
  5. Android的SDK与ADT不匹配问题
  6. android控件之ImageButton android:scale
  7. Android(安卓)3.1 r1 中文API文档 (120)
  8. android消息处理机制学习(一)-Handler,Mess
  9. Android(安卓)安卓让LinearLayout放置于
  10. android aapt (android asset packaging t