自定义控件Android技术知识Android开发

android 时间进度条自定义View

2017-06-15  本文已影响149人  kecai
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;

/**

// //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();
}

}

上一篇 下一篇

猜你喜欢

热点阅读