Android自定义课程格子

一两年之前,项目中有大佬写过课程表这个功能,今天复习自定义View,就自己动手写写;有不对的地方,请各位大佬指出来,歇歇-_-

写给我自己,用作学习记录

效果如下:(数据是自己瞎编的)


接下来直接上代码:

1.MainActivity.java:


2.main_activity.xml


3.ScheduleView:课程表控件

package com.example.admin.test.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.example.admin.test.R;
import com.example.admin.test.utils.DensityUtils;

import java.util.ArrayList;

/**
 * Created by Admin on 2018/4/11.
 */

public class ScheduleView extends View {

    private static String TAG = ScheduleView.class.getSimpleName();
    /**
     * 单个课程宽度
     */
    private int width = 30;

    /**
     * 单个课程高度
     */
    private int height = 15;

    /**
     * 课程表第一行高度
     */
    private int topHeight = 20;

    /**
     * 课程表左边第一列宽度
     */
    private int leftWidth = 20;

    /**
     * 课程中间间隔高度
     */
    private int middleHeight = 20;
    private ArrayList<String> list;//课程数据
    private String[] days = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
    private String[] courseNumber = {"1", "2", "3", "4", "5", "6", "7", "8"};
    private int[] colors = {R.color.color_8cbf52, R.color.color_cccccc, R.color.color_ffffff, R.color.color_65686a, R.color.colorPrimary,
            R.color.colorPrimaryDark, R.color.colorAccent, R.color.color_ffffff, R.color.color_65686a, R.color.colorPrimary};//课程背景颜色
    private int scheduleWidth = 0;//课程表总宽度
    private int scheduleheight = 0;//课程表总高度
    private Paint paint1;//绘制线条
    private Paint paint2;//绘制文字
    private Paint paint3;//绘制格子背景
    private Paint pFont;//测量文字使用
    private Rect rect;//测量文字使用
    private ArrayList<String[]> allCourse;//将传进来的数据整理后存在这里
    private String[] str;//每一天的课程
    private String course;//单个课程名字
    private int num = 0;//取颜色用的随机数

    public ScheduleView(Context context) {
        super(context);
        init(context);
    }

    public ScheduleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        allCourse = new ArrayList();
        width = DensityUtils.dp2px(context, 50);
        height = DensityUtils.dp2px(context, 40);
        topHeight = DensityUtils.dp2px(context, 30);
        leftWidth = DensityUtils.dp2px(context, 30);
        middleHeight = DensityUtils.dp2px(context, 30);
        scheduleWidth = leftWidth + days.length * width;
        scheduleheight = topHeight + courseNumber.length * height;
        paint1 = new Paint();
        paint1.setColor(getResources().getColor(R.color.color_cccccc));
        paint1.setAntiAlias(true);
        paint1.setStrokeWidth(5);
        paint2 = new Paint();
        paint2.setColor(getResources().getColor(R.color.color_000000));
        paint2.setAntiAlias(true);
        paint2.setTextSize(DensityUtils.sp2px(context, 14));
        paint2.setStrokeWidth(5);
        paint3 = new Paint();
        paint3.setColor(colors[(int) (Math.random() * 10)]);
        paint3.setAntiAlias(true);
        paint3.setStrokeWidth(5);
        pFont = new Paint();
        pFont.setTextSize(DensityUtils.sp2px(context, 14));
        rect = new Rect();
    }

    public void setData(ArrayList<String> list) {
        this.list = list;
        if (this.list == null) {
            this.list = new ArrayList<>();
        }
        for (int i = 0; i < 56; i += 8) {
            str = new String[8];
            for (int j = 0; j < 8; j++) {
                str[j] = this.list.get(i + j);
            }
            allCourse.add(str);
            str = null;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawTop(canvas);
        drawLeft(canvas);
        drawOthers(canvas);
    }

    private void drawOthers(Canvas canvas) {
        for (int i = 0; i < days.length; i++) {
            for (int j = 0; j < courseNumber.length; j++) {
                num = (int) (Math.random() * 10);
                paint3.setColor(getResources().getColor(colors[num]));
                if (j == 3) {
                    //绘制背景色
                    canvas.drawRect(leftWidth + i * width, topHeight + j * height, leftWidth + (i + 1) * width, topHeight + (j + 1) * height, paint3);
                    //绘制文字
                    course = (allCourse.get(i))[j];
                    pFont.getTextBounds(course, 0, TextUtils.isEmpty(course) ? 0 : course.length(), rect);
                    canvas.drawText(course, leftWidth + i * width + width / 2 - rect.width() / 2, topHeight + j * height + height / 2 + rect.height() / 2, paint2);
                    //绘制边线
                    canvas.drawLine(leftWidth + i * width, topHeight + (j + 1) * height, leftWidth + (i + 1) * width, topHeight + (j + 1) * height, paint1);
                    canvas.drawLine(leftWidth + (i + 1) * width, topHeight + j * height, leftWidth + (i + 1) * width, topHeight + (j + 1) * height, paint1);
                    //绘制中间分隔条
                    paint3.setColor(getResources().getColor(R.color.color_FFF68F));
                    canvas.drawRect(leftWidth + i * width, topHeight + (j + 1) * height, leftWidth + (i + 1) * width, topHeight + middleHeight + (j + 1) * height, paint3);
                    canvas.drawLine(leftWidth + i * width, topHeight + (j + 1) * height + middleHeight, leftWidth + (i + 1) * width, topHeight + (j + 1) * height + middleHeight, paint1);

                } else if (j > 3) {
                    //绘制背景色
                    canvas.drawRect(leftWidth + i * width, topHeight + middleHeight + j * height, leftWidth + (i + 1) * width, topHeight + middleHeight + (j + 1) * height, paint3);
                    //绘制文字
                    course = (allCourse.get(i))[j];
                    pFont.getTextBounds(course, 0, TextUtils.isEmpty(course) ? 0 : course.length(), rect);
                    canvas.drawText(course, leftWidth + i * width + width / 2 - rect.width() / 2, middleHeight + topHeight + j * height + height / 2 + rect.height() / 2, paint2);
                    //绘制边线
                    canvas.drawLine(leftWidth + i * width, topHeight + (j + 1) * height + middleHeight, leftWidth + (i + 1) * width, topHeight + (j + 1) * height + middleHeight, paint1);
                    canvas.drawLine(leftWidth + (i + 1) * width, topHeight + j * height + middleHeight, leftWidth + (i + 1) * width, topHeight + (j + 1) * height + middleHeight, paint1);
                } else {
                    //绘制背景色
                    canvas.drawRect(leftWidth + i * width, topHeight + j * height, leftWidth + (i + 1) * width, topHeight + (j + 1) * height, paint3);
                    //绘制文字
                    course = (allCourse.get(i))[j];
                    pFont.getTextBounds(course, 0, TextUtils.isEmpty(course) ? 0 : course.length(), rect);
                    canvas.drawText(course, leftWidth + i * width + width / 2 - rect.width() / 2, topHeight + j * height + height / 2 + rect.height() / 2, paint2);
                    //绘制边线
                    canvas.drawLine(leftWidth + i * width, topHeight + (j + 1) * height, leftWidth + (i + 1) * width, topHeight + (j + 1) * height, paint1);
                    canvas.drawLine(leftWidth + (i + 1) * width, topHeight + j * height, leftWidth + (i + 1) * width, topHeight + (j + 1) * height, paint1);
                }


            }
        }
    }

    private void drawLeft(Canvas canvas) {
        paint3.setColor(getResources().getColor(R.color.color_FFF68F));
        canvas.drawRect(0, topHeight, leftWidth, scheduleheight + middleHeight, paint3);
        canvas.drawLine(0, topHeight, 0, scheduleheight + middleHeight, paint1);
        canvas.drawLine(leftWidth, topHeight, leftWidth, scheduleheight + middleHeight, paint1);
        for (int j = 0; j < courseNumber.length + 1; j++) {
            if (j != 0 && j != courseNumber.length + 1) {
                pFont.getTextBounds(courseNumber[j - 1], 0, courseNumber[j - 1].length(), rect);
                if (j > 4) {
                    canvas.drawText(courseNumber[j - 1], leftWidth / 2 - rect.width() / 2, middleHeight + topHeight + j * height - height / 2 + rect.height() / 2, paint2);
                } else {
                    canvas.drawText(courseNumber[j - 1], leftWidth / 2 - rect.width() / 2, topHeight + j * height - height / 2 + rect.height() / 2, paint2);
                }

            }
            if (j == 4) {
                canvas.drawLine(0, topHeight + j * height, leftWidth, topHeight + j * height, paint1);
                canvas.drawRect(0, topHeight + 4 * height, leftWidth, topHeight + 4 * height + middleHeight, paint3);
                canvas.drawLine(0, topHeight + j * height + middleHeight, leftWidth, middleHeight + topHeight + j * height, paint1);
            } else if (j > 4) {
                canvas.drawLine(0, topHeight + j * height + middleHeight, leftWidth, topHeight + j * height + middleHeight, paint1);
            } else {
                canvas.drawLine(0, topHeight + j * height, leftWidth, topHeight + j * height, paint1);
            }
        }
    }

    private void drawTop(Canvas canvas) {
        paint3.setColor(getResources().getColor(R.color.color_FFF68F));
        canvas.drawRect(0, 0, scheduleWidth, topHeight, paint3);
        canvas.drawLine(0, 0, scheduleWidth, 0, paint1);
        canvas.drawLine(0, 0, 0, topHeight, paint1);
        canvas.drawLine(0, topHeight, scheduleWidth, topHeight, paint1);
        for (int j = 0; j < days.length + 1; j++) {
            if (j != 0 && j != days.length + 1) {
                pFont.getTextBounds(days[j - 1], 0, days[j - 1].length(), rect);
                canvas.drawText(days[j - 1], leftWidth + j * width - width / 2 - rect.width() / 2, topHeight / 2 + rect.height() / 2, paint2);
            }
            canvas.drawLine(leftWidth + j * width, 0, leftWidth + j * width, topHeight, paint1);
        }
    }
}


还有很多需要改进的地方,之后会继续修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值