Now I have xml to set the view And I have one View and on button.Now With the onDraw() function I just draw the ColorDotView but I can not draw the button.
<?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>
And this is my View Setting 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);
}
}
This is my MainActivity.class
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ColorDotView(this));
}
}
I want to add one more button into the view and use the button to control the color change (now I can use screen to change the color)
Thanks in advance!
Because there is no one answer this question. So I will add my method here.
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();
}
});
}
}
Here is the MainActivity function. The most Important is that we should call the invalidate() function to force the Canvas to draw the new picture when we click the Button