Android FingerPaint示例不会绘制点?

Nou*_*tti 6 android

示例android api demo中的Fingerpaint示例不会通过触摸屏幕上的手指来绘制点/点.在代码中他们使用Path来绘制线条,有没有办法用路径绘制圆圈或点?

public class MyView extends View {
    // int bh = originalBitmap.getHeight();
    // int bw = originalBitmap.getWidth();

    public MyView(Context c, int w, int h) {
        super(c);
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        // Bitmap mBitmap =
        // Bitmap.createScaledBitmap(originalBitmap,200,200,true);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        //mBitmapPaint.setColor(Color.YELLOW);
        //mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // mBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        // mCanvas = new Canvas(mBitmap);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        //canvas.drawColor(customColor);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
    }

    // //////************touching evants for painting**************///////
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 5;

    private void touch_start(float x, float y) {
        //mCanvas.drawCircle(x, y, progress+1, mPaint); 

        mPath.reset();          
        mPath.moveTo(x, y);
        //mPaint.setStyle(Paint.Style.FILL);
        //mPath.addCircle(x, y, (float) (progress+0.15), Direction.CW);  
        mCanvas.drawPath(mPath, mPaint);
        mX = x;
        mY = y;
        //mPaint.setStyle(Paint.Style.STROKE);

    }

    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) {
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
    }

    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
        }
        return true;
    } // end of touch events for image
}
Run Code Online (Sandbox Code Playgroud)

这是代码,我应该在这段代码中编辑什么才能在finertouch上绘制点/点?

Jul*_*eau 10

有没有办法用路径绘制圆圈或点?

而不是尝试使用drawPoint(float x, float y, Paint paint)Canvas类中的方法.

要在API演示中使用它,您需要更改3件事:

  1. private boolean mDrawPoint;MyView课堂上有一个区分水龙头和幻灯片.
  2. 如果路径已更改(即在语句中),则设置mDrawPointtruein touch_start()和to falsein .touch_move()if
  3. touch_up()检查的价值mDrawPoint.如果它是假的,那么该函数之前做了什么,如果它是真的,那么在画布上绘制点: mCanvas.drawPoint(mX, mY, mPaint);

新版本touch_up():

private void touch_up() {
    if(mDrawPoint == true) {
        mCanvas.drawPoint(mX, mY, mPaint);          
    } else {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在绘制一条线后向上移动它时,它会自动在它旁边绘制一个点,线在那里结束.当我开始一个新的线/曲线时,先前绘制的线将从画布上移除/清除,只留下后面的点.

您不需要比我的答案更多的修改.你可能忘了实现它的一部分.

我在尝试它时遇到了同样的问题并在发布我的答案之前解决了它.这是因为你在两个不同的画布上绘制,在给定onDraw方法的情况下,当你的手指起来时它会丢失,另一个mCanvas是保存线的地方touch_up.这就是为什么touch_up有一个if:

如果我们没有移动手指(只是轻敲),那么我们将点绘制到mCanvas下一个位置onDraw(onDrawmCanvas其作为参数绘制到它接收的画布上,以便绘制旧的线条和点mCanvas仍然可见) .

如果我们移动手指,那么我们将绘制到画布的路径保存到手指onDraw,mCanvas以便在手指向上后它仍然存在.

你遇到的问题来自函数的else一部分touch_up永远不会被执行,因此touch_up无论是否应该绘制一个点,并且路径永远不会被提交mCanvas,因此在下次onDraw调用时会消失.

这很可能源于你在第2点中所说的那样设置mDrawPoint为真,但忘记如我在第2点中所说的那样设置为假.touch_start()mDrawPointtouch_move

这是我的touch_move样子:

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) {
        mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
        mX = x;
        mY = y;
        mDrawPoint = false;
        }
    }
Run Code Online (Sandbox Code Playgroud)