1.自定义View,用来涂鸦,签名
/**
* 自定义View
* @author VanishMagic
*
*/
public class DrawView extends View{
//笔触当前坐标
float preX;
float preY;
//路径
private Path path;
//画笔
public Paint paint = null;
//想要保存成的图片的背景大小
final int VIEW_WIDTH = 1000;
final int VIEW_HEIGHT = 1000;
//笔迹图像
Bitmap cacheBitmap = null;
//画布
Canvas cacheCanvas = null;
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT,
Config.ARGB_8888);
cacheCanvas = new Canvas();
path = new Path();
cacheCanvas.setBitmap(cacheBitmap);
//Paint.DITHER_FLAG为抗抖动常量
paint = new Paint(Paint.DITHER_FLAG);
//设置笔触颜色
paint.setColor(Color.BLACK);
//设置笔触粗细
paint.setStrokeWidth(3);
//设置画笔类型
paint.setStyle(Paint.Style.STROKE);
//设置抗锯齿
paint.setAntiAlias(true);
//抗抖动
paint.setDither(true);
}
public void clear(){
//清除画布
cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT,
Config.ARGB_8888);
cacheCanvas.setBitmap(cacheBitmap);
//更新View invalidate vt. 使无效;使无价值
invalidate();
}
/**
* 为了保存成图片
* @return
*/
public Bitmap save(){
return cacheBitmap;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//获得当前笔触位置
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
//当按下时
case MotionEvent.ACTION_DOWN:
//路径起始点移动到此处,并记录
path.moveTo(x, y);
preX = x;
preY = y;
break;