android 时间进度条自定义View

作者: kecai | 来源:发表于2017-06-15 12:43 被阅读149次
    B0CCQC~OXT~Q`VIM2A1Z{CM.png

    自定义的时间进度条,主要是分一种是黄色,一种是暗灰色,比如某个时间段占用了那么改时间段就是黄色....

    首先我们先要准备画布大小代码如下:

    QQ图片20170615121413.png

    大家会疑问:progressIndex是多少呢? 或者又是什么意思呢。这个在之前已经定义了。效果图可以看出时间段从08:00-20:00(一共12小时) 。如果按5分钟为一个小单位的话,那么半小时就是6个小单位。一个小时就是12个小单位。那么也就是说progressInde就是12*12.那么每一个单位宽度就是preIndex=mWidth/progressIndex;

    第二步就是准备画笔,画布,并且在画笔上画画了。在onDraw()方法里面搞事情:


    QQ图片20170615123334.png

    第三步:就是填充色值

    QQ图片20170615124217.png

    最后贴上代码:

    public class MeetingRoomBookView extends View {

    /**进度条最大值*/
    private float maxCount;
    /**进度条当前值*/
    private float currentCount;
    /**画笔*/
    private Paint mPaint;
    private int mWidth,mHeight;
    private int preT=6;
    //分為24份每一份30分鐘
    private long progressIndex=24*preT;
    private List<String> timeListIndex=new ArrayList<>();
    //每30分鐘有多寬
    private long preIndex=0;
    private List<MeetingManaBean> bookViewList=new ArrayList<>();
    
    public MeetingRoomBookView(Context context) {
        super(context);
    }
    
    public MeetingRoomBookView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setMaxTimeIndex();
    }
    
    public MeetingRoomBookView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    //設置時間標誌
    private void setMaxTimeIndex(){
        timeListIndex.add("8:00");
        timeListIndex.add("12:00");
        timeListIndex.add("14:00");
        timeListIndex.add("18:00");
        timeListIndex.add("20:00");
    }
    
    /***
     * 设置最大的进度值
     * @param maxCount
     */
    public void setMaxCount(float maxCount) {
        this.maxCount = maxCount;
    }
    /**
     * 得到最大进度值
     */
    public double getMaxCount(){
        return maxCount;
    }
    /***
     * 设置当前的进度值
     * @param currentCount
     */
    public void setCurrentCount(float currentCount) {
        this.currentCount = currentCount > maxCount ? maxCount : 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);
      mPaint = new Paint();
      //设置抗锯齿效果
      mPaint.setAntiAlias(true);
      //设置画笔颜色
      mPaint.setColor(Color.WHITE);
      int round = mHeight;
      /
      *

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

    • 类型是单精度浮点数
      /
      RectF rf = new RectF(0, 0, mWidth, mHeight);
      /
      绘制圆角矩形,背景色为画笔颜色/
      canvas.drawRoundRect(rf, round, round, mPaint);
      /
      设置progress内部是灰色*/
      mPaint.setColor(Color.parseColor("#f8f9fd"));
      RectF rectBlackBg = new RectF(0, 0, mWidth-2, mHeight-2);
      canvas.drawRoundRect(rectBlackBg, round, round, mPaint);
      //设置进度条进度及颜色
      setBookIndex(canvas,round);
      //寫文字
      paintText(canvas);
      }

      private void setFreeTimeColor(){//設置空閒的

      }

      private void setBookIndex(Canvas canvas,int round){
      //設置8-10點定制了 11-12:30 14:30-15:00 18:30-20:00
      if(bookViewList.size()>0){
      mPaint.setColor(Color.parseColor("#f6d125"));
      for(int i=0;i<bookViewList.size();i++){
      MeetingManaBean bean= bookViewList.get(i);
      setHasBookTimeColor(canvas,round,bean.begin,bean.end,mPaint);
      }
      }

    // //11-12:30
    // setHasBookTimeColor(canvas,round,6,9,mPaint);
    // //14:30-16:00
    // setHasBookTimeColor(canvas,round,13,16,mPaint);
    // //18:30-20:00
    // setHasBookTimeColor(canvas,round,21,24,mPaint);

    }
    
    private void setHasBookTimeColor(Canvas canvas,int round,int startIndex,int endIndex,Paint mPaint){
        RectF rectProgressBg = new RectF(((mWidth-1)*startIndex)/progressIndex, 3, ((mWidth-1)*endIndex)/progressIndex, mHeight-3);
        if(endIndex==progressIndex||startIndex==0){
            canvas.drawRoundRect(rectProgressBg, round, round, mPaint);
        }else {
            canvas.drawRect(rectProgressBg, mPaint);
        }
    }
    
    
    
    private void paintText(Canvas canvas){
        mPaint.setColor(Color.parseColor("#888888"));
        mPaint.setTextSize(20);
        canvas.drawText(timeListIndex.get(0),0,mHeight*2,mPaint);
        canvas.drawText(timeListIndex.get(1),(8*preT*mWidth/progressIndex),mHeight*2,mPaint);
        canvas.drawText(timeListIndex.get(2),(12*preT*mWidth/progressIndex),mHeight*2,mPaint);
        canvas.drawText(timeListIndex.get(3),(20*preT*mWidth/progressIndex),mHeight*2,mPaint);
        canvas.drawText(timeListIndex.get(4),(22*preT*mWidth/progressIndex),mHeight*2,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) {
    

    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    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;
    } else {
    mWidth = 0;
    }

        preIndex=mWidth/progressIndex;//每30分鐘有多寬
    

    //MeasureSpec.AT_MOST,最大尺寸,只要不超过父控件允许的最大尺寸即可,MeasureSpec.UNSPECIFIED未指定尺寸
    if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
    mHeight = dipToPx(20);
    } else {
    mHeight = heightSpecSize;
    }
    mHeight=30;
    //设置控件实际大小
    setMeasuredDimension(mWidth, mHeight*2);
    }

    public void setBookViewList(List<MeetingManaBean> bookViewList) {
        this.bookViewList = bookViewList;
        this.postInvalidate();
    }
    

    }

    相关文章

      网友评论

        本文标题:android 时间进度条自定义View

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