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

时间:2017-09-26 00:25:06

标签: android xml button view ondraw

现在我有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>

这是我的视图设置类

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

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

提前致谢!

1 个答案:

答案 0 :(得分:-1)

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

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()函数调用强制画布绘制新图片 p>