Drawing rainbow paint on canvas android

I am working on several finger drawing applications. My requirement is that I need to paint with multicolor paint. I have seen several apps on the play store that make this concept very fluid

https://play.google.com/store/apps/details?id=com.doodlejoy.studio.doodleworld

Here is the code for the view draw class

    int[] mColors = new int[] { 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF,
                0xFF00FF00, 0xFFFFFF00, 0xFFFF0000, Color.MAGENTA, Color.CYAN };

@Override
public boolean onTouchEvent(MotionEvent event) {

    int pointerCount = event.getPointerCount();
    int cappedPointerCount = pointerCount > MAX_FINGERS ? MAX_FINGERS
            : pointerCount;
    int actionIndex = event.getActionIndex();
    int action = event.getActionMasked();
    int id = event.getPointerId(actionIndex);

    if ((action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN)
            && id < MAX_FINGERS) {

        mFingerPaths[id] = new Path();
        mFingerPaths[id].moveTo(event.getX(actionIndex),
                event.getY(actionIndex));




    } 

    else if ((action == MotionEvent.ACTION_POINTER_UP || action == MotionEvent.ACTION_UP)
            && id < MAX_FINGERS) {

        mFingerPaths[id].setLastPoint(event.getX(actionIndex),
                event.getY(actionIndex));

        cor.setStrokeWidth(brushSize);
        // cor.setColor(isSetRanbow ?
        // mColors[random.nextInt(mColors.length)]
        // : paintColor);

        mFingerPaths[id].computeBounds(mPathBounds, true);

        mCompletedPaths.add(new PaintItem(mFingerPaths[id], brushSize, cor
                .getColor()));

        invalidate();
        mFingerPaths[id] = null;




    for (int i = 0; i < cappedPointerCount; i++) {
        if (mFingerPaths[i] != null) {
            int index = event.findPointerIndex(i);
            if (isSetRanbow) {
                cor.setColor(mColors[random.nextInt(mColors.length)]);
            }
            mFingerPaths[i].lineTo(event.getX(index), event.getY(index));

            mFingerPaths[i].computeBounds(mPathBounds, true);
            if (isSetRanbow) {
                 drawCanvas.drawPoint(event.getX(index),
                 event.getY(index), cor);

            } else {
                drawCanvas.drawPath(mFingerPaths[i], cor);
            }

            invalidate();
        }
    } 



    return true;
} 

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

        canvas.drawBitmap(canvasBitmap, 0, 0, mPaint);
        if (mPath != null)
            canvas.drawPath(mPath, mPaint);

    }

      

With the above code, I can draw points with many colors. But the problem comes when I used t draw fast. Then it misses some points and the drawing is not smooth.

Another big problem is that drawing is slow. How to make a drawing faster and smoother.

Genius guidance required!

+3


source to share





All Articles