首页 > 代码库 > 安卓使用Canvas绘制工作日程表

安卓使用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 = http://www.mamicode.com/"-1";>
主界面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>

显示效果:








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