自定义view,24小时百分比
程序员文章站
2024-02-05 20:44:16
...
话不多说,直接上效果图
主要是使用画图工具,根据不同的时间段绘制不同颜色的矩形,根据时间段除以24小时,项目中是6点-24点,就是除以18,然后算出百分比,与手机的屏幕进行换算,得出要绘制的小矩形的宽,大功告成。
自定义view代码
public class DrawView extends View {
private List<DrawPara> mDrawParaList;
private Context mContext;
Paint alreadlyPaint = new Paint();// 已经预约
Paint yesPaint = new Paint();//可预约
Paint noPaint = new Paint();//不可预约
Paint goPaint = new Paint();// 待批准
public DrawView(Context context, List<DrawPara> drawParaList) {
super(context);
this.mDrawParaList = drawParaList;
this.mContext = context;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
alreadlyPaint.setColor(getResources().getColor(R.color.draw_o));
alreadlyPaint.setStyle(Paint.Style.FILL);
alreadlyPaint.setAntiAlias(true);//
yesPaint.setColor(getResources().getColor(R.color.draw_c));
yesPaint.setStyle(Paint.Style.FILL);
yesPaint.setAntiAlias(true);//
noPaint.setColor(getResources().getColor(R.color.main));
noPaint.setStyle(Paint.Style.FILL);
noPaint.setAntiAlias(true);//
goPaint.setColor(getResources().getColor(R.color.red_pressed));
goPaint.setStyle(Paint.Style.FILL);
goPaint.setAntiAlias(true);//
ingPaint.setColor(getResources().getColor(R.color.green_normal));
ingPaint.setStyle(Paint.Style.FILL);
ingPaint.setAntiAlias(true);//
for (int i = 0; i < mDrawParaList.size(); i++) {
int start = DateUtils.StringToStandardHm(mContext, mDrawParaList.get(i).getStart());
int end = DateUtils.StringToStandardHm(mContext, mDrawParaList.get(i).getEnd());
// LogUtils.e("绘制矩形:" + start + ":" + end);
int bottom = dp2px(mContext, 50);
if (mDrawParaList.get(i).getType() == 1) {
// 已预约
canvas.drawRect(start, 0, end, bottom, alreadlyPaint); //绘制矩形
} else if (mDrawParaList.get(i).getType() == 2) {
// 可预约
canvas.drawRect(start, 0, end, bottom, yesPaint); //绘制矩形
} else if (mDrawParaList.get(i).getType() == 3) {
// 禁止
canvas.drawRect(start, 0, end, bottom, noPaint); //绘制矩形
} else if (mDrawParaList.get(i).getType() == 5) {
// 待批准
canvas.drawRect(start, 0, end, bottom, goPaint); //绘制矩形
} else if (mDrawParaList.get(i).getType() == 6) {
// 进行中
canvas.drawRect(start, 0, end, bottom, ingPaint); //绘制矩形
} else {
canvas.drawRect(start, 0, end, dp2px(mContext, 50), noPaint); //绘制矩形
}
}
}
}
实体类
public class DrawPara implements Serializable {
// 绘制长条的bean
private String start;//开始
private String end;//结束
private int type;//类型
public DrawPara(String start, String end, int type) {
this.start = start;
this.end = end;
this.type = type;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String toString() {
return "DrawPara{" +
"start='" + start + '\'' +
", end='" + end + '\'' +
", type=" + type +
'}';
}
}
遇到的问题:
1.手机分辨率不同,要dp转化为px;
2.绘制的时候要先绘制底层的颜色,一层层绘制;
上一篇: Android 流程指示器
下一篇: 访问进度指示器