用手指手势的Android绘画

时间:2015-12-03 09:12:37

标签: android android-gesture

我可以使用手指手势在画布上绘画和绘画。它真的很好用。我已经实现了this代码,它的工作就像一个魅力,我甚至修改了注入清除按钮以清除所有也清除最后绘制的线。这一切都很好。

但是当我开始移动手指时它正在画画。我想在画布上放置一些点,为此我已经通过代码看到了,但是在你轻轻移动手指之前,没有任何东西可以在一个水龙头上绘制单个点。

所以请告诉我如何用单指触摸填充/画一个点。

1 个答案:

答案 0 :(得分:-1)

这项工作对我来说:

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();
}

}