我在主要活動中創建了一個名爲DrawView
的自定義視圖。我在DrawView
課上實施了onDraw()
方法,它最初繪製一個圓圈。然後我添加了一個觸摸偵聽器,這樣當用戶點擊時,它將繪製一個正方形。我達到了用戶點擊並繪製正方形的部分。我不確定如何去解決這個問題。Android,從主要活動繪圖
public class TestActivity extends Activity {
DrawView drawing;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup myLayout = (ViewGroup) findViewById(R.id.mainLayout);
drawing = new DrawView(this);
myLayout.addView(drawing);
drawing.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// draw a square
}
return true;
}
});
}
private class DrawView extends View {
public DrawView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
Paint myPaint = new Paint();
myPaint.setColor(Color.BLACK);
// draw a circle
}
}
}
幫助將不勝感激。
我想我已經得到了這個工作,但是,有沒有一種繪製圓的方法,然後繪製廣場沒有刪除圓?即最初繪製一個圓,用戶點擊,然後有一個正方形,顯示正方形和圓形。我在谷歌環顧四周,我是否必須有某種數組來跟蹤已繪製的內容,並且每次都重繪所有內容?即'Shapes [] ShapesDrawn = {circle,square,triangle,square}',將它放在on循環中的onDraw方法? – 1user1160804