Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing with multiple colors in canvas

Tags:

android

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

I found some forum posts about using a paint (or path) list for that purpose. however, I could not entirely understand the solution. Could anyone post some code of a working example?

Thank you very much in advance.

like image 289
Dominik Avatar asked Jul 08 '11 14:07

Dominik


2 Answers

  1. Canvas
  2. 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
    
like image 87
nicholas.hauschild Avatar answered Sep 28 '22 03:09

nicholas.hauschild


Try this, I have done it and it works great 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;
            }
}
like image 41
Shreyash Mahajan Avatar answered Sep 28 '22 01:09

Shreyash Mahajan