我在這裏閱讀關於此主題的不同問題,但我仍然無法找到答案。出於任何原因,請隨時關閉此問題。自定義視圖不能自己繪製
我有一個簡單的Circle
類延伸View
。
這個類的代碼是:
public class ProgressCircle extends View {
Paint mCirclePaint;
float extRadius;
float viewWidth, viewHeight;
float centerX, centerY;
public ProgressCircle(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
init();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
float xpad = (float) getPaddingLeft() + getPaddingRight();
float ypad = (float) getPaddingTop() + getPaddingBottom();
float ww = (float)w - xpad; float hh = (float)h - ypad;
extRadius = Math.min(ww, hh)/2;
viewWidth = this.getWidth();
viewHeight = this.getHeight();
centerX = viewWidth/2; centerY = viewHeight/2;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(centerX, centerY, extRadius, mCirclePaint);
canvas.drawText("ciao", 0, 0, mCirclePaint);
}
private void init() {
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setColor(0x666666);
mCirclePaint.setStyle(Paint.Style.FILL_AND_STROKE);
}
我確認所創建的主活動時(通過使用一些Log.d()
S的)在此歸類每一個方法被調用。我在主要活動的LinearLayout
中添加了一個<com.mypackage.Circle>
元素,之後我添加了一個樣本測試按鈕。
我實現的是按鈕顯示,而我的Circle
不大,但仍然按鈕(這在LinearLayout
的Circle
後自帶)不是佈局的第一個元素:這讓我覺得這事其實發生,但沒有任何繪製。
嘗試刪除'super.onDraw(canvas);' – aragaer 2013-02-22 13:50:34
@aragaer沒有任何改變。 – whatyouhide 2013-02-22 13:54:23