今天介绍一个比较简单的功能手绘。但想做一个再现过程的。但不知道如何去保存当事人画的实际变速度和 不连贯的地方。欢迎大家讨论
原谅我 就是不喜欢提供DEMO 哈
package com.rexviewforchildcandrag;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Rex on 2016/9/30.
*/
public class DrawView extends View {
private Paint p;
private float downX;
private float downY;
private float moveX;
private float moveY;
private Paint paint;
private Canvas cacheCanvas;
private Bitmap cachebBitmap;
private Path path;
private int clr_bg, clr_fg;
private boolean isMoving;
private int width;
private int height;
private boolean isTrans = false;
private Path lastPath = null;
private List<Path> lastPaths = new ArrayList<>();
private List<float[]> lasts = new ArrayList<>();
private int i;
private boolean isShowing;
public DrawView(Context context) {
this(context, null, 0);
}
public DrawView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
p = new Paint();
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(10);
p.setColor(Color.parseColor("#EB7A77"));
path = new Path();
// 创建一张屏幕大小的位图,作为缓冲
reset();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
}
@Override
protected void onDraw(Canvas canvas) {
Log.i("rex", "moveX " + moveX + "moveY " + moveY);
canvas.drawColor(Color.GREEN);
if (!isTrans) {
isTrans = true;
cacheCanvas.translate(-(width - cachebBitmap.getWidth()) * 1f / 2, -(height - cachebBitmap.getHeight()) * 1f / 2);
}
// 绘制上一次的,否则不连贯
if (null != cachebBitmap)
canvas.drawBitmap(cachebBitmap, (width - cachebBitmap.getWidth()) * 1f / 2, (height - cachebBitmap.getHeight()) * 1f / 2, null);
canvas.drawPath(path, p);
}
public void clear() {
reset();
invalidate();
}
public void reset() {
cachebBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
cacheCanvas = new Canvas(cachebBitmap);
cacheCanvas.drawColor(Color.GRAY);
lastPath = new Path();
lastPath.addPath(path);
path.reset();
isTrans = false;
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i("rex", "Message" + Thread.currentThread());
cacheCanvas.drawPath(path, p);
path.reset();
invalidate();
super.handleMessage(msg);
switch (msg.what) {
case 0:
break;
default:
break;
}
}
};
public void showLastPath() {
path.moveTo(lasts.get(0)[0], lasts.get(0)[1]);
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i < lasts.size(); i++) {
if (i == lasts.size() - 1) {
cacheCanvas.drawPath(path, p);
path.reset();
postInvalidate();
return;
}
isShowing = true;
path.quadTo(lasts.get(i - 1)[0], lasts.get(i - 1)[1], lasts.get(i)[0], lasts.get(i)[1]);
SystemClock.sleep(50);
// mHandler.sendMessage(mHandler.obtainMessage(0));
postInvalidate();
}
}
}).start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
path.moveTo(downX, downY);
isMoving = true;
lasts.add(new float[]{downX, downY});
break;
case MotionEvent.ACTION_MOVE:
moveX = event.getX();
moveY = event.getY();
if (!isMoving)
break;
// 二次曲线方式绘制
path.quadTo(downX, downY, moveX, moveY);
downX = moveX;
downY = moveY;
lasts.add(new float[]{downX, downY});
break;
case MotionEvent.ACTION_UP:
cacheCanvas.drawPath(path, p);
path.reset();
isMoving = false;
break;
}
invalidate();
return true;
}
}