简单进度条的实现

来源:互联网 发布:appserv是什么软件 编辑:程序博客网 时间:2024/06/06 07:03
  • 测量高度 宽度 获取配置文件中设定的值
  • 设置前景色
  • 设置背景色 根据percent 百分比参数
  • 设置文本描述
  • 设置动画效果
    工程地址 https://github.com/gacmy/GacProgressBar
public class GacProgressBar extends View {    private int maxWidth;    private int maxHeight;    private Paint textPaint;    private int mBackgroudColor;    private int mForegroundColor;    private int textColor;    private int textSize;    private float mPercent;    private String mDescriptionText;    ValueAnimator animator;    private int currentWidth;    public GacProgressBar(Context context) {        super(context);        init();    }    public GacProgressBar(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        init();    }    public GacProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    private void init(){        if(mBackgroudColor == 0){            mBackgroudColor = Color.parseColor("#eeeeee");        }        if(mForegroundColor == 0){            mForegroundColor = Color.parseColor("#feaaaa");        }        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);        textColor = Color.parseColor("#000000");        mDescriptionText = "剩余20%";        textSize = 40;        mPercent = 0.2f;    }    private void setBackgroundPaint(){        textPaint.setColor(mBackgroudColor);        textPaint.setStyle(Paint.Style.FILL);    }    private void setForegroundPaint(){        textPaint.setColor(mForegroundColor);        textPaint.setStyle(Paint.Style.FILL);    }    /*      设置动画效果    */    private void setAnimation(){        animator = ValueAnimator.ofFloat(0,1);        //animator.ofFloat(0,1);        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {                float value = (float)animation.getAnimatedValue();               // float width = (int)maxWidth*mPercent*value;                currentWidth =(int)(maxWidth* value*mPercent);               // Log.e("gac","value"+mPercent);                invalidate();            }        });        animator.setDuration(1000);        animator.start();    }    private void setTextPaint(){        textPaint.setColor(textColor);        textPaint.setTextSize(textSize);    }    public void setProgressBarConfig(ProgressBarConfig config){        mDescriptionText = config.getDescriptionText();        if(config.getForegroundColor() == 0){            mForegroundColor = Color.parseColor("#feaaaa");        }else{            mForegroundColor = convertColor(config.getForegroundColor());        }        if(config.getBackgroudColor() == 0){            mBackgroudColor = Color.parseColor("#eeeeee");        }else{            mBackgroudColor = convertColor(config.getBackgroudColor());        }        mPercent = config.getPercent();        if(config.getTextColor() == 0){            textColor = Color.parseColor("000000");        }else{            textColor = convertColor(config.getTextColor());        }        if(config.getTextSize() == 0){            textSize = convertsize(16);        }else{            textSize = convertsize(config.getTextSize());        }        //invalidate();        if(config.isAnimation()){            setAnimation();        }else{            invalidate();            Log.e("maxWidth","maxWidth:"+maxWidth+" curWidth:"+currentWidth+" percent:"+mPercent);        }    }    //资源id convert color rgb    private int convertColor(int color){        return ContextCompat.getColor(getContext(), color);    }    //dp to px    private int convertsize(int size){        return  (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size,                getResources().getDisplayMetrics());    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        maxWidth = measureWidth(widthMeasureSpec);        maxHeight = measureHeight(heightMeasureSpec);        setMeasuredDimension(maxWidth, maxHeight);        currentWidth = (int)(maxWidth *mPercent);    }    private int measureWidth(int measureSpec) {        int specMode = MeasureSpec.getMode(measureSpec);        int specSize = MeasureSpec.getSize(measureSpec);        //设置一个默认值,就是这个View的默认宽度为500,这个看我们自定义View的要求        int result = 500;        if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content            result = specSize;        } else if (specMode == MeasureSpec.EXACTLY) {//相当于我们设置为match_parent或者为一个具体的值            result = specSize;        }        return result;    }    private int measureHeight(int measureSpec) {        int specMode = MeasureSpec.getMode(measureSpec);        int specSize = MeasureSpec.getSize(measureSpec);        int result = 500;        if (specMode == MeasureSpec.AT_MOST) {            result = specSize;        } else if (specMode == MeasureSpec.EXACTLY) {            result = specSize;        }        return result;    }    @Override    protected void onDraw(Canvas canvas) {        drawBackground(canvas);        drawForeground(canvas);        drawText(canvas);    }    private void drawBackground(Canvas canvas){        setBackgroundPaint();        canvas.drawRect(0,0,maxWidth,maxHeight,textPaint);    }    private void  drawForeground(Canvas canvas){        setForegroundPaint();        canvas.drawRect(0,0,currentWidth,maxHeight,textPaint);    }    private void drawText(Canvas canvas){        if(TextUtils.isEmpty(mDescriptionText)){            return;        }        setTextPaint();        canvas.drawText(mDescriptionText,40,maxHeight,textPaint);    }    @Override    protected void onDetachedFromWindow() {        super.onDetachedFromWindow();        if(animator != null){            animator.cancel();        }    }}
原创粉丝点击