在画布中绘制多种颜色

e-n*_*ure 5 android

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

我找到了一些关于为此目的使用绘画(或路径)列表的论坛帖子.但是,我无法完全理解解决方案.任何人都可以发布一些工作示例的代码吗?

非常感谢你提前.

nic*_*ild 6

  1. 帆布
  2. 涂料

    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
    
    Run Code Online (Sandbox Code Playgroud)


Shr*_*jan 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;
            }
}
Run Code Online (Sandbox Code Playgroud)