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.
- 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
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;
}
}
精彩评论