我創建一個視圖並使用畫布繪製一些東西。android:清理我的視圖,當我點擊按鈕
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);
}
}
當我點擊按鈕時,如何編寫清理畫布的代碼?另外,當我點擊按鈕後,如何改變我的形狀?我想要做的改變座標:
canvas.drawCircle(150, 150, 200, paint);
我創建了shouldDraw。 (private Boolean shouldDraw = false;)但我不能在我的onclickListener中使用它,我不能這樣做(shouldDraw = True),你能告訴我爲什麼嗎? – nich
你是否在onCreate之上初始化它? –