Canvas。单指画。如果点击按钮线颜色必须改变

Canvas. Single finger draw. if click on button line color must change

我有 2 个类 DrawLine.class 画线的地方和 Main.class 调用 xml 文件的地方,其中注册了按钮和视图元素。我需要点击一个按钮就可以绘制某种颜色。例如,按下按钮 "blue" 时,该行变为蓝色等。尝试按照 DrawLine.class 中指定的方式实施,但日志中的错误指向字符串 setContentView (R.layout.activity_main);总的来说,到底错在什么地方,按下按钮渲染颜色的方法如何实现,并不是那么重要。 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnW10"
            android:text="Width10"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnW40"
            android:text="Width40"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnW70"
            android:text="Width70"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="20dp"></LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnBlue"
            android:text="Blue"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnRed"
            android:text="Red"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnGreen"
            android:text="Green"/>

    </LinearLayout>

    <com.vladislav.canvaswc.DrawLine
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

Main.class

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

DrawLine.class

public class DrawLine extends View implements View.OnClickListener
{

    private Paint paint = new Paint();
    private Path path = new Path();

    float width=5f;
    int color;
   //

   // btnB.OnClickListener(this);

    public DrawLine(Context context)
    {
        super(context);
        init(context);

    }
    public DrawLine(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DrawLine(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public void init(Context context) {
        paint.setColor(color);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.MITER);
        paint.setStrokeWidth(width);
        Button btnB=(Button) findViewById(R.id.btnBlue);
        btnB.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch(view.getId())
        {
            case R.id.btnBlue: color=Color.BLUE; break;
        }

    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        canvas.drawPath(path, paint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(x, y);
                break;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:

                invalidate();
                break;
        }
        invalidate();
        return true;
    }
}

设置一个方法来更改 DrawLine 中的绘制实例class并从按钮的 onClick 方法中调用它

public void changeColor(int color) {
    paint.setColor(color);
}