我有一個繪製線條的應用程序。當我按下屏幕時,削減我的手指並停止觸摸,它畫出一條線。我想知道如何在我將屏幕上的手指削減時看到正在繪製的線條。在繪製時用預覽繪製直線
這是我的代碼繪製線條:
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;
}
}
請充分類代碼... – ravi
我剛纔編輯的代碼與全類代碼 – malaka