安卓使用Canvas绘制工作日程表

有一个项目要使用工作表,选择使用canvas来绘制,实现显示工作日程的选择,可点击添加和取消。

自定义控件FormView:

package com.sun.elderly.comm.widget;

import com.sun.elderly.comm.ui.interfaces.IFormListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;

/**
 * 绘制表格
 *
 * @author jwzhangjie
 */
public class FormView extends View {

	private int firstX = 0; // 起始点x
	private int firstY = 0; // 起始点y
	private int secondX = 80; // 第二点x
	private int secondY = 50; // 第二点y
	private int widthNum = 8; // 列
	private int heightNum = 10; // 行
	private int secondSideX = 150; // 第二列的宽
	private int sideY = 50; // 行高
	private int firstSidesX = 80; // 第一列的宽
	private int workColor = 0xffADFF2F;
	private int restColor = 0xffAD0F2F;
	private int selectColor = 0xffAD00FF;

	private String[] rowText = null;
	private String[] colText = null;
	private String noData = "-1";

	public void setRowAndColText(String[] rowText, String[] colText) {
		this.rowText = rowText;
		this.colText = colText;
		invalidate();
	}

	public SparseArray<String> list = new SparseArray<String>();
	private SparseArray<String> backList = new SparseArray<String>();

	IFormListener myFormListener;

	public IFormListener getFormListener() {
		return myFormListener;
	}

	public void setFormListener(IFormListener myFormListener) {
		this.myFormListener = myFormListener;
	}

	public FormView(Context context) {
		super(context);
	}

	public FormView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void init(int width, int height) {
		secondSideX = (width - firstSidesX) / (widthNum - 1);
		// invalidate();
	}

	public void setList(SparseArray<String> list) {
		this.list = list;
		backList.clear();// 重置
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (colText != null && rowText != null) {
			drawForm(canvas);
		}
	}

	private void drawForm(Canvas canvas) {
		Paint paint = new Paint();

		paint.setAntiAlias(true);
		paint.setColor(Color.BLACK);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(2);
		paint.setStyle(Paint.Style.FILL);

		paint.setColor(Color.BLACK);
		paint.setStyle(Paint.Style.STROKE);
		int cellX = 0, cellY = 0, cellBX = 0, cellBY = 0;
		for (int i = 0; i < widthNum; i++){

			for (int j = 0; j < heightNum; j++) {
				if (i == 0) { // 如果是第一列绘制第一列的宽度
					cellX = firstX + i * firstSidesX;
					cellY = firstY + j * sideY;
					cellBX = firstX + (i + 1) * firstSidesX;
					cellBY = firstY + (j + 1) * sideY;
				} else {
					cellX = secondX + (i - 1) * secondSideX;
					cellY = secondY + (j - 1) * sideY;
					cellBX = secondX + i * secondSideX;
					cellBY = secondY + j * sideY;
				}

				canvas.drawRect(cellX, cellY, cellBX, cellBY, paint);
				int cellsNum = i + j * widthNum;
				if (j == 0) {
					drawCellText(canvas, cellX, cellY, cellBX, cellBY,
							rowText[i]);
				}
				if (cellsNum % widthNum != 0) {
					if (!noData.equals(list.get(cellsNum, noData))) {
						if (list.get(cellsNum).equals("0")) {
							drawCellColor(canvas, cellX, cellY, cellBX, cellBY,
									workColor);
						} else {
							drawCellColor(canvas, cellX, cellY, cellBX, cellBY,
									restColor);
						}
					} else if (!noData.equals(backList.get(cellsNum, noData))) {
						drawCellColor(canvas, cellX, cellY, cellBX, cellBY,
								selectColor);
					} else {
						drawCellColor(canvas, cellX, cellY, cellBX, cellBY,
								0xFFF);
					}
				} else {// 绘制第一列
					drawCellText(canvas, cellX, cellY, cellBX, cellBY,
							colText[cellsNum / widthNum]);
				}
			}
		}
	}

	// 绘制单元格中的文字
	private void drawCellText(Canvas canvas, int cellX, int cellY, int cellBX,
			int cellBY, String text) {
		Paint paint = new Paint();
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.BLUE);
		int textSize = (cellBY - cellY) / 5 * 2;
		paint.setTextSize(textSize);
		int textX = cellX + (cellBX - cellX) / 10;
		int textY = cellBY - (cellBY - cellY) / 3;
		canvas.drawText(text, textX, textY, paint);
	}

	// 绘制单元格中的颜色
	private void drawCellColor(Canvas canvas, int cellX, int cellY, int cellBX,
			int cellBY, int color) {
		Paint paint = new Paint();
		// 绘制备选颜色边框以及其中颜色
		paint.setColor(color);
		paint.setStyle(Paint.Style.FILL);
		canvas.drawRect(cellX + 1, cellY + 1, cellBX - 1, cellBY - 1, paint);
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		float touchX = event.getX();
		float touchY = event.getY();

		int antion = event.getAction();
		if (antion == MotionEvent.ACTION_DOWN) {
			testTouchColorPanel(touchX, touchY);
		}
		return super.onTouchEvent(event);
	}

	// 检测点击事件所在的格数
	public boolean testTouchColorPanel(float x, float y) {
		if (x > secondX && y > secondY
				&& x < firstX + firstSidesX + secondSideX * widthNum
				&& y < firstY + sideY * heightNum) {

			int ty = (int) ((y - firstY) / sideY);
			int tx;

			if (x - firstX - firstSidesX > 0) {
				tx = (int) ((x - firstX - firstSidesX) / secondSideX + 1);
			} else {
				tx = 0;
			}
			int index = ty * widthNum + tx;
			if (noData.equals(list.get(index, noData))) {
				if (!noData.equals(backList.get(index, noData))) {
					backList.remove(index);
					myFormListener.showNum(index, 1);
				} else {
					backList.put(index, "2");
					myFormListener.showNum(index, 2);
				}
				invalidate();
			} else {
				myFormListener.showNum(index, 0);
			}
			return true;
		}
		return false;
	}
}

主界面DrawFormActivity:

package com.sun.elderly.test;

import com.sun.elderly.R;
import com.sun.elderly.comm.ui.interfaces.IFormListener;
import com.sun.elderly.comm.widget.FormView;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.SparseArray;
import android.view.ViewTreeObserver;
import android.widget.Toast;

public class DrawFormActivity extends FragmentActivity implements IFormListener {

	private FormView myFormView;
	private SparseArray<String> list = new SparseArray<String>();
	private String[] rowText = { "", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六",
			"星期日" };
	private String[] colText = { "", "9:00", "10:00", "11:00", "12:00",
			"13:00", "14:00", "15:00", "16:00", "17:00" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("工作表");
		setContentView(R.layout.test_drawform);
		myFormView = (FormView) findViewById(R.id.formId);
		list.put(12, "0");// key:位置 ,value:0--已经预约 1--休息时间 2--预备预约占用
		list.put(18, "1");
		list.put(19, "1");
		list.put(21, "0");
		list.put(33, "0");
		list.put(46, "1");
		myFormView.setList(list);
		myFormView.setFormListener(this);
		ViewTreeObserver vto = myFormView.getViewTreeObserver();
		vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
			public boolean onPreDraw() {
				myFormView.getViewTreeObserver().removeOnPreDrawListener(this);
				int height = myFormView.getMeasuredHeight();
				int width = myFormView.getMeasuredWidth();
				myFormView.init(width, height);
				myFormView.setRowAndColText(rowText, colText);
				return true;
			}
		});
	}

	@Override
	public void showNum(int num, int status) {
		switch (status) {
		case 0:
			showInfo(num + "当前时间已经被占用");
			break;
		case 1:
			showInfo("取消选择:" + num);
			break;
		case 2:
			showInfo("选择工作时间: " + num);
			break;
		}
	}

	private void showInfo(String text) {
		Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT)
				.show();
	}
}

布局test_drawform:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.sun.elderly.comm.widget.FormView
        android:id="@+id/formId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
		android:layout_margin="10dip"
        />

</LinearLayout>

显示效果:

时间: 2024-10-01 08:43:17

安卓使用Canvas绘制工作日程表的相关文章

用canvas绘制折线图

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>用canvas绘制折线图</title> 6 </head> 7 <body> 8 <canvas id="cv"></canvas> 9 </body> 1

封装 用canvas绘制直线的函数--面向对象

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>用面向对象的思想 封装 在canvas绘制直线的函数</title> 6 </head> 7 <body> 8 <canvas id="cv"></canvas> 9 &

canvas绘制时钟

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas绘制钟表</title> <style type="text/css"> #canvas{ /*border: 2px solid red;*/ display: block; margin: 0 auto; } </style> </

html5 Canvas绘制图形入门详解

html5,这个应该就不需要多作介绍了,只要是开发人员应该都不会陌生.html5是「新兴」的网页技术标准,目前,除IE8及其以下版本的IE浏览器之外,几乎所有主流浏览器(FireFox.Chrome.Opera.Safari.IE9+)都已经开始支持html5了.除此之外,在移动浏览器市场上,众多的移动浏览器也纷纷展开关于「html5的支持能力以及性能表现」的军备竞赛.html作为革命性的网页技术标准,再加上众多浏览器厂商或组织的鼎力支持,可以想见,html5将会成为未来网页技术的领头羊. ht

用h5中的canvas 绘制八卦图

1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>canvas绘制八卦图</title> 6 </head> 7 8 <body> 9 <canvas id="canvas" width="600" height="500"><

使用Canvas绘制图形的基本教程

原文地址:http://www.cnblogs.com/picaso/archive/2012/11/26/2789077.html HTML5火的正热,最近有个想法也是要用到HTML的相关功能,所以也要好好学习一把. 好好看了一下Canvas的功能,感觉HTML5在客户端交互的功能性越来越强了,今天看了一下Canvas绘图,下边是几个实例,记下以备后用. 1.使用Canvas绘制直线: <!doctype html> <html> <head> <meta ch

Canvas绘制线条模糊的解决方案

前段时间,做一个跨平台app项目,需要绘制分时图和K线图.找了很多开源的js的图表库,包括echarts.highcharts等等,都不是很满意,原因有2: 1.太臃肿,我实际上只要一个分时和一个K线图表,最多搭配几个线形图 2.不满足需求.主要就是分时图,国内玩的js图表库,几乎都没有分时图.都是用1分钟线的收盘价线来做的,和中国股民的使用习惯完全不搭界. 多年前有人开源了一个js绘制股票图形的库,叫做html54stock,图像表现上很符合中国人的使用习惯,但是也有问题: 1.封装不好,很多

用canvas绘制的饼状图,6等分,颜色随机

都说程序员懒,生活还是不能懒的,比如洗头.洗脸,当然也包括是分享咯.下面分享一下我用H5的新标签canvas绘制的饼状图,颜色是随机生成的,动态生成6等分,我还做了随机生成数据的, 配上文字,每份所占的比例就显而意见.,这点用在分析数据还是挺好的.图片如下; canvas 是 HTML5 提供的一个用于展示绘图效果的标签. canvas 原意画布, 帆布. 在 HTML 页面中用于展示绘图效果. 最早 canvas 是苹果提出的一个方案, 今天已经在大多数浏览器中实现.. 基本使用语法: 使用

canvas绘制清晰的方法

很早就开始使用canvas,包括自己绘制各种图形,以及作为画布提供给诸如echarts,当canvas绘制细线条,特别是关于文字绘制会出现很模糊或者锯齿的感觉. 1 <canvas ref="canvas" width="200" height="200"/> 正常情况下一般都是这么写的,但是实际上对于canvas来说,这个canvas.width canvas.height 与canvas.style.width canvas.st