2011-07-08 34 views
5

我做了一个简单的绘图应用程序,可以在画布上绘制线条。现在我想添加各种颜色选择按钮。现在我遇到的问题是,如果我点击一个颜色按钮并继续绘制所有先前绘制的线条,也会将其颜色更改为新选择的颜色。用多种颜色在画布上绘画

我发现了一些关于使用油漆(或路径)列表的论坛帖子。但是,我不能完全理解解决方案。任何人都可以发布一个工作示例的代码?

非常感谢您提前。

+0

您可以创建Paint对象的ArrayList,并可以通过在每次Button单击时初始化新的Paint对象来设置线的颜色。 –

回答

1

试试这个, 我做了它,它为我的作品马丽娟。

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

希望这会帮助你。 享受。

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