-2

I am able to draw and paint on the canvas using my finger gesture. It is really working well. I have implemented this code and it is working like a charm and I have even modified to inject clear button to clear all also to clear last drawn line. and this all working fine.

But it is drawing when I start to move the finger. Where as I want to place some dots on the canvas, for this I have seen through code but there is nothing which is drawing the single dot on a single tap until you move your finger a little bit.

So please tell me How can I fill/draw a dot on a single finger touch.

Community
  • 1
  • 1
Allay Khalil
  • 674
  • 3
  • 11
  • 31

1 Answers1

-1

This work for me:

public class DrawingView extends View {

// drawing path
private Path drawPath;
// drawing and canvas paint
private Paint drawPaint, canvasPaint;
// initial color
private int paintColor = 0xFF660000;
// canvas
private Canvas drawCanvas;
// canvas bitmap
private Bitmap canvasBitmap;

private float brushSize, lastBrushSize;

private boolean erase = false;  

Bitmap mBitmap;
Matrix mMatrix;
RectF mSrcRectF;
RectF mDestRectF;
boolean mPause;

public DrawingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    setupDrawing();
}

public void setupDrawing() {
    drawPath = new Path();
    drawPaint = new Paint();
    drawPaint.setColor(paintColor);
    drawPaint.setAntiAlias(true);
    drawPaint.setStrokeWidth(20);
    drawPaint.setStyle(Paint.Style.STROKE);
    drawPaint.setStrokeJoin(Paint.Join.ROUND);
    drawPaint.setStrokeCap(Paint.Cap.ROUND);
    canvasPaint = new Paint(Paint.DITHER_FLAG);
    brushSize = getResources().getInteger(R.integer.medium_size);
    lastBrushSize = brushSize;
    drawPaint.setStrokeWidth(brushSize);

    mMatrix = new Matrix();
    mSrcRectF = new RectF();
    mDestRectF = new RectF();
    mPause = false;
}

public void addBitmap(Bitmap bitmap){
    mBitmap = bitmap;       
}

public Bitmap getBitmap(){
    return mBitmap;
}

public void setBrushSize(float newSize) {
    // update size
    float pixelAmount = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, newSize, getResources()
                    .getDisplayMetrics());
    brushSize = pixelAmount;
    drawPaint.setStrokeWidth(brushSize);
}

public void setLastBrushSize(float lastSize) {
    lastBrushSize = lastSize;
}

public float getLastBrushSize() {
    return lastBrushSize;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    // TODO Auto-generated method stub
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    drawCanvas = new Canvas(canvasBitmap);
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    if(mBitmap!=null){

        // Setting size of Source Rect
        mSrcRectF.set(0, 0,mBitmap.getWidth(),mBitmap.getHeight());

        // Setting size of Destination Rect
        mDestRectF.set(0, 0, getWidth(), getHeight());

        // Scaling the bitmap to fit the PaintView
        mMatrix.setRectToRect( mSrcRectF , mDestRectF, ScaleToFit.FILL);

        // Drawing the bitmap in the canvas
        canvas.drawBitmap(mBitmap, mMatrix, canvasPaint);
    }
    canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
    canvas.drawPath(drawPath, drawPaint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    float touchX = event.getX();
    float touchY = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        drawPath.moveTo(touchX, touchY);
        break;
    case MotionEvent.ACTION_MOVE:
        drawPath.lineTo(touchX, touchY);
        break;
    case MotionEvent.ACTION_UP:
        drawCanvas.drawPath(drawPath, drawPaint);
        drawPath.reset();
        break;
    default:
        return false;
    }
    invalidate();
    return true;
}

public void setColor(String newColor) {
    // set color
    paintColor = Color.parseColor(newColor);
    drawPaint.setColor(paintColor);
    invalidate();
}

public void setErase(boolean isErase) {
    // set erase true or false
    erase = isErase;
    if (erase)
        drawPaint
                .setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    else
        drawPaint.setXfermode(null);
}

public void startNew() {
    drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
    mBitmap=null;
    invalidate();
}

}