美文网首页
自定义ProgressBar

自定义ProgressBar

作者: wintersweett | 来源:发表于2017-10-09 14:47 被阅读0次

    自己写了个简单的demo

    实现后的效果是这样的

    这个demo是用了线程去发消息,然后ui线程中做了progressbar的更新显示

    也可以用view.post(Runnable)去实现;或者onWindowsFocusedChanged去实现(此法,由于onPause与OnResum都会调用,太频繁)

    直接上代码干货:

    为自定义的progressbar

    public class MyProgressBar extends View {

    /**进度条最大值*/

    private float maxCount;

    /**进度条当前值*/

    private float currentCount;

    /**画笔*/

    private Paint mPaint;

    private int mWidth,mHeight;

    publicMyProgressBar(Context context, AttributeSet attrs, int defStyle) {

    super(context, attrs, defStyle);

    }

    publicMyProgressBar(Context context, AttributeSet attrs) {

    super(context, attrs);

    }

    publicMyProgressBar(Context context) {

    super(context);

    }

    /***

    *设置最大的进度值

    * @param maxCount

    */

    public void setMaxCount(float maxCount) {

    this.maxCount = maxCount;

    Log.i("zhm","macout=="+maxCount);

    }

    /**

    *得到最大进度值

    */

    public double getMaxCount(){

    return maxCount;

    }

    /***

    *设置当前的进度值

    * @param currentCount

    */

    public void setCurrentCount(float currentCount) {

    this.currentCount = currentCount > maxCount ? maxCount : currentCount;

    Log.i("zhm","currentCount=="+currentCount);

    /**

    * invalidate()是用来刷新View的,必须是在UI线程中进行工作。比如在修改某个view的显示时,

    *调用invalidate()才能看到重新绘制的界面。invalidate()的调用是把之前的旧的view从主UI

    *线程队列中pop掉。

    */

    invalidate();

    }

    @Override

    protected void onDraw(Canvas canvas) {

    //TODO Auto-generated method stub

    super.onDraw(canvas);

    Log.i("zhm","onDraw");

    mPaint = new Paint();

    //设置抗锯齿效果

    mPaint.setAntiAlias(true);

    //设置画笔颜色

    mPaint.setColor(Color.BLACK);

    int round = mHeight/2;

    Log.i("zhm","round==="+round);

    /**

    * RectF:绘制矩形,四个参数分别是left,top,right,bottom

    *类型是单精度浮点数

    */

    RectF rf = new RectF(0, 0, mWidth, mHeight);

    /*绘制圆角矩形,背景色为画笔颜色*/

    canvas.drawRoundRect(rf, round, round, mPaint);

    ///*设置progress内部是灰色*/

    mPaint.setColor(Color.rgb(211, 211, 211));

    RectF rectBlackBg = new RectF(2, 2, mWidth-2, mHeight-2);

    canvas.drawRoundRect(rectBlackBg, round, round, mPaint);

    //设置进度条进度及颜色

    float section = currentCount/maxCount;

    Log.i("zhm","currentCount=="+currentCount+" ==maxCount===="+maxCount);

    RectF rectProgressBg = new RectF(3, 3, (mWidth-3)*section, mHeight-3);

    if(section!=0.0f){

    mPaint.setColor(Color.GREEN);

    }else{

    mPaint.setColor(Color.TRANSPARENT);

    }

    canvas.drawRoundRect(rectProgressBg, round, round, mPaint);

    }

    //dip * scale + 0.5f * (dip >= 0 ? 1 : -1)

    private int dipToPx(int dip){

    float scale = getContext().getResources().getDisplayMetrics().density;

    return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));//加0.5是为了四舍五入

    }

    /**指定自定义控件在屏幕上的大小,onMeasure方法的两个参数是由上一层控件

    *传入的大小,而且是模式和尺寸混合在一起的数值,需要MeasureSpec.getMode(widthMeasureSpec)

    *得到模式,MeasureSpec.getSize(widthMeasureSpec)得到尺寸

    *

    */

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    Log.i("zhm","onMeasure");

    int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);

    int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

    int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);

    int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

    //MeasureSpec.EXACTLY,精确尺寸

    if (widthSpecMode == MeasureSpec.EXACTLY|| widthSpecMode == MeasureSpec.AT_MOST) {

    mWidth = widthSpecSize;

    Log.i("zhm","mWidh==="+mWidth);

    } else {

    mWidth = 0;

    }

    //MeasureSpec.AT_MOST,最大尺寸,只要不超过父控件允许的最大尺寸即可,MeasureSpec.UNSPECIFIED未指定尺寸

    if (heightSpecMode == MeasureSpec.AT_MOST|| heightSpecMode == MeasureSpec.UNSPECIFIED) {

    mHeight = dipToPx(20);

    Log.i("zhm","mHeight==="+mHeight);

    } else {

    mHeight = heightSpecSize;

    Log.i("zhm","esle de mHeight==="+mHeight);

    }

    //设置控件实际大小

    setMeasuredDimension(mWidth, mHeight);

    }

    }

    如下为demo的activity:

    public class ProgressBarTestActivity extends Activity {

    private MyProgressBar progress;

    private int total_length;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_progress_bar_test);

    progress = (MyProgressBar) findViewById(R.id.pb);

    progress.setMaxCount(100);

    new Thread(new Runnable() {

    @Override

    public void run() {

    //  for (int i = 0; i <=progress.getMaxCount(); i++) {

    Message msg = new Message();

    int[] ar=calculateMethod("50","100");

    msg.arg1 = ar[0];

    msg.what = 0x01;

    handler.sendMessage(msg);

    try {

    //每隔0.1秒进度前进1

    Thread.sleep(100);

    } catch (InterruptedException e) {

    e.printStackTrace();

    }

    //        }

    }

    }).start();

    }

    private int[] calculateMethod(String num1, String num2) {

    int[] arr = new int[2];

    try {

    if (null == num1 || null == num2) {

    return arr;

    }

    int newNum1 = Integer.parseInt(num1);

    int newNum2 = Integer.parseInt(num2);

    Log.i("zhm","newnum1=="+newNum1);

    Log.i("zhm","newNum2=="+newNum2);

    if (newNum1 != 0){

    arr[0] = newNum1*100  / (newNum1 + newNum2);

    Log.i("zhm"," !=0 arr[0]=="+arr[0]);

    }else {

    arr[0] = 0;

    Log.i("zhm"," =0  arr[0]=="+arr[0]);

    }

    return arr;

    } catch (Exception e) {

    }

    return arr;

    }

    Handler handler = new Handler(){

    public void handleMessage(Message msg) {

    if(msg.what==0x01){

    progress.setCurrentCount(msg.arg1) ;

    }

    };

    };

    }

    相关文章

      网友评论

          本文标题:自定义ProgressBar

          本文链接:https://www.haomeiwen.com/subject/whcpyxtx.html