开发者

Drawing with multiple colors in canvas

开发者 https://www.devze.com 2023-03-18 02:04 出处:网络
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 drawi

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 开发者_StackOverflowunderstand the solution. Could anyone post some code of a working example?

Thank you very much in advance.


  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
    


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;
            }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消