Code example for Path

Methods: computeBoundsisEmptylineTorewind

0
displayMatrix.setRectToRect(r, displayBounds, Matrix.ScaleToFit.FILL);
    } 
 
    private void drawDoodle(Canvas canvas) {
        if ((canvas != null) && !normalizedPath.isEmpty()) {
            drawingPath.set(normalizedPath);
            drawingPath.transform(pathMatrix);
            canvas.drawPath(drawingPath, doodlePaint);
        } 
    } 
 
    public void setColor(int color) {
        // Reset path to draw in a new color. 
        finishCurrentPath(); 
        normalizedPath.moveTo(lastPoint.x, lastPoint.y);
        doodlePaint.setColor(Color.argb(192, Color.red(color), Color.green(color),
                Color.blue(color)));
    } 
 
    private void finishCurrentPath() { 
        if (!normalizedPath.isEmpty()) {
            // Update the finished path to the bitmap. 
            drawDoodle(bitmapCanvas);
            if (listener != null) {
                listener.onDoodleFinished(new Path(normalizedPath), doodlePaint.getColor());
            } 
            normalizedPath.rewind();
            invalidate(); 
        } 
    } 
 
    private void checkCurrentPathInBounds() { 
        if ((listener != null) && !normalizedPath.isEmpty()) {
            RectF r = new RectF();
            normalizedPath.computeBounds(r, false);
            if (r.intersects(0, 0, 1, 1)) {
                listener.onDoodleInPhotoBounds();
            } 
        } 
    } 
 
    @Override 
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
 
        if (isEnabled()) { 
            float x = event.getX();
            float y = event.getY();
 
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mapPhotoPoint(x, y, lastPoint);
                    normalizedPath.moveTo(lastPoint.x, lastPoint.y);
                    break; 
 
                case MotionEvent.ACTION_MOVE:
                    float lastX = lastPoint.x;
                    float lastY = lastPoint.y;
                    mapPhotoPoint(x, y, lastPoint);
                    normalizedPath.quadTo(lastX, lastY, (lastX + lastPoint.x) / 2,
                            (lastY + lastPoint.y) / 2);
                    checkCurrentPathInBounds(); 
                    invalidate(); 
                    break; 
 
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    // Line to last position with offset to draw at least dots for single clicks. 
                    mapPhotoPoint(x + 1, y + 1, lastPoint);
                    normalizedPath.lineTo(lastPoint.x, lastPoint.y);
                    checkCurrentPathInBounds(); 
                    finishCurrentPath(); 
                    break; 
            } 
        }