问题
I create a view and use canvas drawing something.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyViewCircle myViewCircle = new MyViewCircle(this);
setContentView(R.layout.main);
Button evolve = (Button) findViewById(R.id.evolve);
img01.addView(myViewCircle);
evolve.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
private class MyViewCircle extends View {
public MyViewCircle(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
canvas.drawCircle(50, 50, 100, paint);
}
}
How can I write the code to clean up my canvas when I click the button? Also, how can change my shape when I click the button? I want to do change coordinates:
canvas.drawCircle(150, 150, 200, paint);
回答1:
onDraw() is called if you click the button and your button changes, so you can check in the onDraw() method if the button was clicked.
回答2:
Try
@Override
protected void onDraw(Canvas canvas) {
if(shouldDraw){
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
canvas.drawCircle(50, 50, 100, paint);
}else
Canvas.drawColor(Color.BLACK);
}
and in the onClick
shouldDraw = false;
view.invalidate();
回答3:
what is img01, if its another layout/view, you can call Ex: img01.removeAllViews().
来源:https://stackoverflow.com/questions/8261239/android-clean-my-view-when-i-click-button