package com.handson.game;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.PorterDuff.Mode;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class TypeNoteDemo extends SurfaceView implements
SurfaceHolder.Callback, Runnable {
// 定义一个SurfaceHolder
private SurfaceHolder holder;
// 定义已经写好的文字数组
private Bitmap[] images = new Bitmap[100];
// 当前手写使用的Canvas
private Canvas currentCanvas;
// 手写绘制的图片
private Bitmap currentBitmap;
// 每一行的高度
private int heightPerLine = 32;
// 每一个文字的宽度
private int widthForWord = 32;
// 每一行的文字数量
private int wordsForLine = 10;
// 画笔的宽度
private int widthForPaint = 3;
private Paint paintForLine = new Paint();
{
paintForLine.setColor(Color.GRAY);
}
private Paint paintForWord = new Paint();
{
paintForWord.setColor(Color.RED);
}
//用于清空屏幕的Paint
private Paint paintForClear = new Paint();
{
paintForClear.setColor(Color.TRANSPARENT);
paintForClear.setXfermode(new PorterDuffXfermode(Mode.DST));
}
// 定义触屏时的坐标
private float oldX, oldY, x, y;
// 是否有新画笔
private boolean write = false;
// 间隔时间
private int time;
//已经写过的文字数量
private int count;
public TypeNoteDemo(Context context) {
super(context);
holder = getHolder();
// 注册回调方法
holder.addCallback(this);
// 设置键盘和屏幕可用
setFocusable(true);
setFocusableInTouchMode(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
init();
reDraw();
new Thread(this).start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void init() {
currentBitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(),
Bitmap.Config.ARGB_8888);
currentCanvas = new Canvas(currentBitmap);
}
public void reDraw() {
// 锁定画布
Canvas canvas = holder.lockCanvas();
// 设置画布颜色
canvas.drawColor(0xff556677);
// 画出直线
for (int i = 0; i < this.getHeight() / heightPerLine; i++) {
canvas.drawLine(0, i * heightPerLine, this.getWidth(), i
* heightPerLine, paintForLine);
}
// 绘制写过的文字
for (int i = 0; i < images.length; i++) {
if (images[i] != null) {
canvas.drawBitmap(images[i], i * widthForWord,
(i / wordsForLine) * heightPerLine, null);
}
}
// 绘制手写的Canvas绘制的Bitmap
if (currentBitmap != null) {
canvas.drawBitmap(currentBitmap, 0, 0, null);
}
// 解锁,显示内容
holder.unlockCanvasAndPost(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
oldX = event.getX();
oldY = event.getY();
lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_UP:
write = true;
default:
break;
}
reDraw();
return true;
}
public void lineTo(float x, float y) {
float dx = x - oldX;
float dy = y - oldY;
if (dx == 0 && dy == 0) {
currentCanvas.drawCircle(x, y, widthForPaint, paintForWord);
} else if (dx != 0 && dy / dx >= -1 && dy / dx <= 1) {
float tan = dy / dx;
for (float tmpx = 0; tmpx < dx; tmpx++) {
float tmpy = tmpx * tan;
currentCanvas.drawCircle(oldX + tmpx, oldY + tmpy,
widthForPaint, paintForWord);
}
for (float tmpx = dx; tmpx < 0; tmpx++) {
float tmpy = tmpx * tan;
currentCanvas.drawCircle(oldX + tmpx, oldY + tmpy,
widthForPaint, paintForWord);
}
} else if (dy != 0 && dx / dy >= -1 && dx / dy <= 1) {
float cot = dx / dy;
for (float tmpy = 0; tmpy < dy; tmpy++) {
float tmpx = tmpy * cot;
currentCanvas.drawCircle(oldX + tmpx, oldY + tmpy,
widthForPaint, paintForWord);
}
for (float tmpy = dy; tmpy < 0; tmpy++) {
float tmpx = tmpy * cot;
currentCanvas.drawCircle(oldX + tmpx, oldY + tmpy,
widthForPaint, paintForWord);
}
}
oldX = x;
oldY = y;
time = 0;
}
@Override
public void run() {
while (true) {
long start = System.currentTimeMillis();
try {
Thread.sleep(200);
} catch (Exception e) {
// TODO: handle exception
}
long end = System.currentTimeMillis();
time += (end - start);
if (write && time >= 1000) {
write = false;
//新建一个小图片
Bitmap imageBitmap = Bitmap.createBitmap(this.widthForWord,
this.heightPerLine, Bitmap.Config.ARGB_8888);
Canvas imageCanvas = new Canvas(imageBitmap);
//手写的文字绘制在小图片中
imageCanvas.drawBitmap(currentBitmap, new Rect(0, 0, this
.getWidth(), this.getHeight()), new Rect(0, 0,
this.widthForWord, this.heightPerLine), null);
images[count]= imageBitmap;
count++;
//需要清空CurrentCanvas
// currentCanvas.drawRect(0, 0, currentCanvas.getWidth(), currentCanvas.getHeight(), paintForClear);
currentBitmap.eraseColor(Color.TRANSPARENT);
reDraw();
}
}
}
}
- 1
- 2
前往页