自定義的時間進度條,主要是分一種是黃色,一種是暗灰色,比如某個時間段占用了那么改時間段就是黃色....
首先我們先要準備畫布大小代碼如下:
大家會疑問:progressIndex是多少呢? 或者又是什么意思呢。這個在之前已經定義了。效果圖可以看出時間段從08:00-20:00(一共12小時) 。如果按5分鐘為一個小單位的話,那么半小時就是6個小單位。一個小時就是12個小單位。那么也就是說progressInde就是12*12.那么每一個單位寬度就是preIndex=mWidth/progressIndex;
第二步就是準備畫筆,畫布,并且在畫筆上畫畫了。在onDraw()方法里面搞事情:
第三步:就是填充色值
最后貼上代碼:
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();
}
}