我剛剛進入Android基本繪圖。我從幾個簡單的形狀開始,但我遇到了一些問題。我想在畫布的中心畫一個圓圈。我看了幾個例子,但似乎無法使其工作。我認爲這是因爲我不太瞭解變量在哪裏。在畫布中心繪製圓圈
有人可以請解釋一下在我的屏幕中心繪製我的圈子的正確方法。這裏是我的代碼:
public class Circle extends View{
int width = this.getWidth();
int height = this.getHeight();
public Circle(Context context) {
super(context);
setFocusable(true);
}
protected void onDraw(Canvas canvas){
canvas.drawColor(Color.CYAN);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
//canvas.drawCircle(100, 100, 50, paint);
canvas.drawCircle(width/2, height/2, 100, paint);
Display disp = ((WindowManager)this.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
float radius = 0;//Radius caused an error so I initialized this variable
canvas.drawCircle(disp.getWidth()/2, disp.getHeight()/2, radius, paint);
}
}
我對android編程知之甚少,但我知道半徑爲0意味着你沒有圈子。 http://en.wikipedia.org/wiki/Radius – Kevin
好夠了。謝謝。 – David
[Draw Circle on touch]可能重複(http://stackoverflow.com/questions/11796357/draw-circle-on-touch) – bummi