Painting with multiple colors in canvas.

I made a simple drawing application with which you can draw lines on a canvas. Now I wanted to add various color selection buttons. The problem that I see now is that if I click on the color button and continue to draw all the previous drawn lines, it also changes its color to the newly selected color.

I found several forum posts about using a list of paints (or paths) for this purpose. however, I could not fully understand the solution. Can someone post a working example code?

Thank you in advance.

+5
source share
2 answers

Try it, I did it, and it works for me.

    public void onClick(View view){

        switch (view.getId()){
            case R.id.colorRedBtn:

                //Toast.makeText(getApplicationContext(), "Red", Toast.LENGTH_SHORT).show();
                currentPaint = new Paint();
                currentPaint.setColor(0xFFFF0000);
                currentPaint.setDither(true);
                currentPaint.setStyle(Paint.Style.STROKE);
                currentPaint.setStrokeJoin(Paint.Join.ROUND);
                currentPaint.setStrokeCap(Paint.Cap.ROUND);
                currentPaint.setStrokeWidth(3);
                break;
            case R.id.colorBlueBtn:

               //Toast.makeText(getApplicationContext(), "Green", Toast.LENGTH_SHORT).show();
                   currentPaint = new Paint();
                currentPaint.setColor(0xFF00FF00);
                currentPaint.setDither(true);
                currentPaint.setStyle(Paint.Style.STROKE);
                currentPaint.setStrokeJoin(Paint.Join.ROUND);
                currentPaint.setStrokeCap(Paint.Cap.ROUND);
                currentPaint.setStrokeWidth(3);
                break;
            case R.id.colorGreenBtn:

                //Toast.makeText(getApplicationContext(), "Blue", Toast.LENGTH_SHORT).show();
                currentPaint = new Paint();
                currentPaint.setColor(0xFF0000FF);
                currentPaint.setDither(true);
                currentPaint.setStyle(Paint.Style.STROKE);
                currentPaint.setStrokeJoin(Paint.Join.ROUND);
                currentPaint.setStrokeCap(Paint.Cap.ROUND);
                currentPaint.setStrokeWidth(3);

                break;

            case R.id.colorBlackBtn:

               //Toast.makeText(getApplicationContext(), "Black", Toast.LENGTH_SHORT).show();
                currentPaint = new Paint();
                currentPaint.setColor(0xFF000000);
                currentPaint.setDither(true);
                currentPaint.setStyle(Paint.Style.STROKE);
                currentPaint.setStrokeJoin(Paint.Join.ROUND);
                currentPaint.setStrokeCap(Paint.Cap.ROUND);
                currentPaint.setStrokeWidth(3);
                break;
            }
}

Hope this helps you. Enjoy it.

+1
  • Canvas
  • Paint

    Paint bluePaint = new Paint();
    p1.setColor(Color.BLUE);
    
    Paint greenPaint = new Paint();
    p2.setColor(Color.GREEN);
    
    canvas.drawLine(1.0, 1.0, 2.0, 2.0, bluePaint); //blue line
    canvas.drawLine(2.0, 1.0, 1.0, 2.0, greenPaint); //green line
    
+6

All Articles