Custom view, 24 hours percent

Keywords: Mobile

Don't talk much, just go to the renderings

It mainly uses drawing tools to draw rectangles of different colors according to different time periods. Divided by 24 hours according to time periods, 6-24 points are in the project, that is, divided by 18, and then the percentage is calculated. After conversion with the screen of the mobile phone, the width of the small rectangle to be drawn is obtained, and the big achievement is achieved.
Custom view code

public class DrawView extends View {
    private List<DrawPara> mDrawParaList;
    private Context mContext;

    Paint alreadlyPaint = new Paint();// Appointment has been made.
    Paint yesPaint = new Paint();//Reservations
    Paint noPaint = new Paint();//No appointment
    Paint goPaint = new Paint();// To be approved

    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("draw rectangle:" + start + ":" + end);
            int bottom = dp2px(mContext, 50);
            if (mDrawParaList.get(i).getType() == 1) {
//                Booked
                canvas.drawRect(start, 0, end, bottom, alreadlyPaint);     //draw rectangle
            } else if (mDrawParaList.get(i).getType() == 2) {
//                Reservations
                canvas.drawRect(start, 0, end, bottom, yesPaint);     //draw rectangle
            } else if (mDrawParaList.get(i).getType() == 3) {
//                prohibit
                canvas.drawRect(start, 0, end, bottom, noPaint);     //draw rectangle
            } else if (mDrawParaList.get(i).getType() == 5) {
//                To be approved
                canvas.drawRect(start, 0, end, bottom, goPaint);     //draw rectangle
            } else if (mDrawParaList.get(i).getType() == 6) {
//                Have in hand
                canvas.drawRect(start, 0, end, bottom, ingPaint);     //draw rectangle
            } else {
                canvas.drawRect(start, 0, end, dp2px(mContext, 50), noPaint);     //draw rectangle
            }
        }
    }
}

Entity class

public class DrawPara implements Serializable {
    //    Drawing a long bean
    private String start;//start
    private String end;//End
    private int type;//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 +
                '}';
    }
}

Problems encountered:
1. The resolution of mobile phone is different, so dp should be converted to px;
2. When drawing, first draw the color of the bottom layer, one layer at a time;

Posted by rallan on Tue, 31 Mar 2020 11:17:53 -0700