当前位置: 首页 > 工具软件 > DrawView > 使用案例 >

自定义view之——根据手势来绘图DrawView

陆臻
2023-12-01

看代码实现如下: 

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class PictureDrawView extends View {

	private Bitmap cacheBitmap;
	public Canvas cacheCanvas;
	public Paint paint;
	private Paint BitmapPaint;
	public Path path;
	private int height;
	private int width;
	
	public float pX;
	public float pY;
	
	private int paintColor = Color.BLACK;
	private static Paint.Style paintStyle = Paint.Style.STROKE;
	private static int paintWidth = 10;

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		height = h;
		width = w;
		init();
	}
	
	private void init(){
		cacheBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		cacheCanvas = new Canvas(cacheBitmap);
		paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		path = new Path();
		BitmapPaint = new Paint();
		updatePaint();
	}
	
	private void updatePaint(){
		paint.setColor(paintColor);
		paint.setStyle(paintStyle);
		paint.setStrokeWidth(paintWidth);
	}
	
	public PictureDrawView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public PictureDrawView(Context context){
		super(context);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			path.moveTo(event.getX(), event.getY());
			pX = event.getX();
			pY = event.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			path.quadTo(pX, pY, event.getX(), event.getY());
			pX = event.getX();
			pY = event.getY();
			break;
		case MotionEvent.ACTION_UP:
			cacheCanvas.drawPath(path, paint);
			path.reset();
			break;
		}
		invalidate();

		return true;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		BitmapPaint = new Paint();
		canvas.drawBitmap(cacheBitmap, 0,0, BitmapPaint);
		canvas.drawPath(path, paint);
		
	}
	

	public void setColor(int color){
		paintColor = color;
		updatePaint();
	}

	public int getPaintColor() {
		return paintColor;
	}

	public void setPaintWidth(int width){
		paintWidth = width;
		updatePaint();
	}

	public static int getPaintWidth() {
		return paintWidth;
	}

	public static final int PEN = 1;
	public static final int PAIL = 2;
	

	public void setStyle(int style){
		switch(style){
		case PEN:
			paintStyle = Paint.Style.STROKE;
			break;
		case PAIL:
			paintStyle = Paint.Style.FILL;
			break;
		}
		updatePaint();
	}
	

	public void clearScreen(){
		if(cacheCanvas != null){
			Paint backPaint = new Paint();
			backPaint.setColor(Color.parseColor("#FFFDED"));
			cacheCanvas.drawRect(new Rect(0, 0, width, height), backPaint);
		}
		invalidate();
	}

	public Bitmap getBitmap(){
		return this.cacheBitmap;
	}
	
}

使用的原生api 类Canvas进行绘制 ,配合着Paint(画笔)和Path(路径)来使用。

Path可以理解为就是绘制的路径,路径是一条线,线就有起点和终点,线可以是直线或者曲线,起点和终点都是同一位置,那就绘制的是一个点。

Path主要有三个方法需要关注:

moveTo(),quadTo(),reset()

moveTo是来设置起点,quadTo是设置终点,Path被用来绘制后进行重置采用reset方法。

Paint可以理解为是绘制的工具——画笔,画笔就有一些属性,比如:颜色、线的粗细、线条的风格等。

Canvas是最终呈现给用户看到的画布,canvas画完的结果可以生成一个Bitmap,

 类似资料: