我在这里阅读关于此主题的不同问题,但我仍然无法找到答案。出于任何原因,请随时关闭此问题。自定义视图不能自己绘制
我有一个简单的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