2011-07-08 13 views
5

Hice una aplicación de dibujo simple con la que puedo dibujar líneas en un lienzo. Ahora quería agregar varios botones de selección de color. El problema que ahora tengo es que si hago clic en un botón de color y continúo dibujando todas las líneas dibujadas anteriormente también cambian su color al color recién seleccionado.Dibujo con varios colores en lienzo

Encontré algunas publicaciones en el foro sobre el uso de una lista de pintura (o ruta) para ese propósito. sin embargo, no pude entender completamente la solución. ¿Alguien podría publicar algún código de un ejemplo de trabajo?

Muchas gracias de antemano.

+0

Puede crear objetos ArrayList of Paint y puede establecer el color de las líneas inicializando el nuevo objeto Paint en cada clic del botón. –

Respuesta

1

Prueba de esto, lo he hecho y funciona greate para mí.

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

Espero que esto te ayude. Disfruta.

6
  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 
    
Cuestiones relacionadas