Android: 如何使用 onDraw() 函数在一个视图中绘制一个圆和一个按钮

Android: how to draw a circle and a button in one view with onDraw() function

现在我 xml 可以设置视图 我有一个 View 和 on button.Now 使用 onDraw() 函数我只绘制了 ColorDotView 但我无法绘制按钮。

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

    <com.example.chg.colordotview2_button.ColorDotView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/dotView"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/button"/>
</LinearLayout>

这是我的视图设置class

public class ColorDotView extends View {

    int count = 0;

    public ColorDotView(Context context) {
        super(context);
    }
    // Constructor required for inflation from resource file
    public ColorDotView(Context context, AttributeSet ats, int defaultStyle)

    {
        super(context, ats, defaultStyle );
    }
    //Constructor required for inflation from resource file
    public ColorDotView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
        return true; // Return true if the event was handled.
    }
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent keyEvent) {
        return true; // Return true if the event was handled.
    }
    @Override
    public boolean onTrackballEvent(MotionEvent event ) {
        // Get the type of action this event represents
        int actionPerformed = event.getAction();
        return true; // Return true if the event was handled.
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Get the type of action this event represents
        invalidate();
        return true; // Return true if the event was handled.
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);  // background color
        Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  

        if (count%8==0|count%8==1){
            mPaint.setColor(Color.RED); } // circle color
        if (count%8==2|count%8==3) {
            mPaint.setColor(Color.BLUE);  // circle color
        }
        if (count%8==4|count%8==5){
            mPaint.setColor(Color.GREEN); } // circle color
        if (count%8==6|count%8==7) {
            mPaint.setColor(Color.YELLOW);  // circle color
        }

        ++count;

        //canvas.drawCircle(cx, cy,radios,paint);
        canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, 
                         canvas.getWidth() / 4, mPaint);
    }
}

这是我的MainActivity.class

public class MainActivity extends AppCompatActivity {

    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new ColorDotView(this));  
    }
}

我想在视图中再添加一个按钮并使用按钮来控制颜色变化(现在我可以使用屏幕来改变颜色)

提前致谢!

因为没有人回答这个问题。 所以我会在这里添加我的方法。

public class MainActivity extends AppCompatActivity {

    Button button;
    ColorDotView colorDotView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseView_ColorButton cet = new BaseView_ColorButton(this);
        setContentView(cet);

        button = (Button) findViewById(R.id.button);
        colorDotView = (ColorDotView) findViewById(R.id.dotView);

        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {

                colorDotView.invalidate();


            }
        });
    }
}

这里是 MainActivity 函数。 最重要的是我们应该调用invalidate()函数来强制Canvas绘制新图片当我们点击按钮