我有一个绘制线条的应用程序。当我按下屏幕时,斜杠并停止触摸,它会画一条线。我想知道当我在屏幕上削减手指时,如何看到我画的线。
这是我绘制线条的代码:
public class DrawingView extends View {
private Path drawPath;
private Paint drawPaint;
private Paint canvasPaint;
private int paintColor=Color.BLACK;
private Canvas drawCanvas;
private Bitmap canvasBitmap;
float tX;
float tY;
public DrawingView(Context context, AttributeSet attrs){
super(context, attrs);
setupDrawing();
}
private void setupDrawing(){
drawPath=new Path();
drawPaint=new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(1);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint=new Paint(Paint.DITHER_FLAG);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas=new Canvas(canvasBitmap);
}
protected void onDraw(Canvas canvas){
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
private void touch_start(float x, float y){
drawPath.reset();
drawPath.moveTo(x, y);
mX=x;
mY=y;
}
private void touch_move(float x, float y){
float dx=Math.abs(x-mX);
float dy=Math.abs(y-mY);
if(dx>=TOUCH_TOLERANCE||dy>=TOUCH_TOLERANCE){
mX=x;
mY=y;
}
}
private void touch_up(){
drawPath.lineTo(mX, mY);
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
}
public boolean onTouchEvent(MotionEvent event){
tX=event.getX();
tY=event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
touch_start(tX,tY);
break;
case MotionEvent.ACTION_MOVE:
touch_move(tX,tY);
break;
case MotionEvent.ACTION_UP:
touch_up();
break;
default:
return false;
}
invalidate();
return true;
}
}
答案 0 :(得分:2)
public class DrawingView extends View {
private static final float TOUCH_TOLERANCE = 0;
private Path drawPath;
private Paint drawPaint;
private Paint canvasPaint;
private int paintColor=Color.BLACK;
private Canvas drawCanvas;
private Bitmap canvasBitmap;
float tX;
float tY;
private float mX;
private float mY;
private float a,b;
public DrawingView(Context context, AttributeSet attrs){
super(context, attrs);
setupDrawing();
}
private void setupDrawing(){
drawPath=new Path();
drawPaint=new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(1);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint=new Paint(Paint.DITHER_FLAG);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas=new Canvas(canvasBitmap);
}
protected void onDraw(Canvas canvas){
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
private void touch_start(float x, float y){
drawPath.reset();
drawPath.moveTo(x, y);
mX=x;
mY=y;
a=x;
b=y;
}
private void touch_move(float x, float y){
float dx=Math.abs(x-mX);
float dy=Math.abs(y-mY);
if(dx>=TOUCH_TOLERANCE||dy>=TOUCH_TOLERANCE){
mX=x;
mY=y;
}
drawPath.lineTo(mX, mY);
//invalidate();
}
private void touch_up(float tX2, float tY2){
drawPath.lineTo(mX, mY);
//drawCanvas.drawPath(drawPath, drawPaint);
drawCanvas.drawLine(a, b, tX2, tY2, drawPaint);
drawPath.reset();
}
public boolean onTouchEvent(MotionEvent event){
tX=event.getX();
tY=event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
touch_start(tX,tY);
break;
case MotionEvent.ACTION_MOVE:
touch_move(tX,tY);
break;
case MotionEvent.ACTION_UP:
touch_up(tX,tY);
break;
default:
return false;
}
invalidate();
return true;
}
}
请试试这个......