Click已注册,但未在canvas android中绘制

时间:2015-01-14 17:13:51

标签: android performance canvas draw

我正在使用绘图应用,这个应用程序已基本完成。现在我遇到了一个问题,即如果我触摸,那么我的点在触摸事件中注册并保存在数组中。但是这个单点触摸点并没有在画布上绘制。但如果我绘制一条路径,那么它就会被绘制出来。以下是我的代码:

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawBitmap(canvasBitmap, 0, 0, mPaint);



        for (int size = mCompletedPaths.size(), i = 0; i < size; i++) {

            PaintItem item1 = mCompletedPaths.get(i);

            mPaint.setColor(item1.brushColor);
            mPaint.setStrokeWidth(item1.brushSize);
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);

            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);  
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            HashMap<Integer, Path> hm = item1.paths;

            for (int msize = hm.size(), j = 0; j < msize; j++) {



                    canvas.drawPath(hm.get(j), mPaint);


            }

        }

    } 

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int maskedAction = event.getActionMasked();




        Log.d("", "onTouchEvent");

        switch (maskedAction) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN: {

            paths = new HashMap<Integer, Path>();
            cor.setStrokeWidth(brushSize);
            for (int size = event.getPointerCount(), i = 0; i < size; i++) {
                Path p = new Path();
                p.moveTo(event.getX(i), event.getY(i));

                paths.put(event.getPointerId(i), p);
                mX.put(event.getPointerId(i), event.getX(i));
                mY.put(event.getPointerId(i), event.getY(i));
                // mArrayList.add(paths);

                mCompletedPaths.add(new PaintItem(paths, brushSize, cor
                        .getColor()));
            }
            break;
        }
        case MotionEvent.ACTION_MOVE: {
            for (int size = event.getPointerCount(), i = 0; i < size; i++) {
                Path p = paths.get(event.getPointerId(i));
                if (p != null) { 
                    float x = event.getX(i);
                    float y = event.getY(i);
                    p.quadTo(mX.get(event.getPointerId(i)),
                            mY.get(event.getPointerId(i)),
                            (x + mX.get(event.getPointerId(i))) / 2,
                            (y + mY.get(event.getPointerId(i))) / 2);
                    mX.put(event.getPointerId(i), event.getX(i));
                    mY.put(event.getPointerId(i), event.getY(i));
                }
            }
            invalidate();
            break;
        }
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_CANCEL: { 


            }
}
        }

        return true;
    }

我需要绘制路径是用户绘制长线。我用户只需按(如果点已注册),那么它绘制简单的点。请帮帮我,这些问题拖延了我的应用程序。

1 个答案:

答案 0 :(得分:1)

想法解决这个问题的答案来自

Android FingerPaint sample does not draw dot?

我的代码:

private boolean mDrawPoint;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int maskedAction = event.getActionMasked();

        Log.d("onTouchEvent");

        switch (maskedAction) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN: {
            mDrawPoint = true;
            paths = new HashMap<Integer, Path>();
            cor.setStrokeWidth(brushSize);
            for (int size = event.getPointerCount(), i = 0; i < size; i++) {
                Path p = new Path();


                p.moveTo(event.getX(i), event.getY(i));

                paths.put(event.getPointerId(i), p);
                mX.put(event.getPointerId(i), event.getX(i));
                mY.put(event.getPointerId(i), event.getY(i));


                mCompletedPaths.add(new PaintItem(paths, brushSize, cor
                        .getColor(), event.getX(i), event.getY(i)));
            }
            invalidate();


            break;
        }
        case MotionEvent.ACTION_MOVE: {

            for (int size = event.getPointerCount(), i = 0; i < size; i++) {
                Path p = paths.get(event.getPointerId(i));
                if (p != null) {
                    float x = event.getX(i);
                    float y = event.getY(i);

                    Log.e("points  Move " + event.getX(i) + "========"
                            + event.getY(i));

                    float dx = Math.abs(x - mX.get(event.getPointerId(i)));
                    float dy = Math.abs(y - mY.get(event.getPointerId(i)));
                    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {

                        p.quadTo(mX.get(event.getPointerId(i)),
                                mY.get(event.getPointerId(i)),
                                (x + mX.get(event.getPointerId(i))) / 2,
                                (y + mY.get(event.getPointerId(i))) / 2);

                        mDrawPoint = false;
                        mX.put(event.getPointerId(i), event.getX(i));
                        mY.put(event.getPointerId(i), event.getY(i));

                        mPaint.setColor(cor.getColor());

                        mPaint.setStrokeWidth(brushSize);
                        mPaint.setAntiAlias(true);
                        mPaint.setDither(true);

                        mPaint.setStyle(Paint.Style.STROKE);
                        mPaint.setStrokeJoin(Paint.Join.ROUND);
                        mPaint.setStrokeCap(Paint.Cap.ROUND);
                        drawCanvas.drawPath(p, mPaint);
                    }

                } 
            }
            invalidate();
            break;
        }
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:

            for (int size = event.getPointerCount(), i = 0; i < size; i++) {
                if (mDrawPoint) {
                    drawCanvas.drawPoint(mX.get(event.getPointerId(i)),
                            mY.get(event.getPointerId(i)), cor);
                }


            }


            invalidate();

            break;

        case MotionEvent.ACTION_CANCEL: {



            }


        }