我正在创建一个customView但它没有显示。我已将它的属性添加到attrs.xml ..
我已经实现了它的构造函数,我只是绘制一个弧形和一个椭圆形,但它没有显示。
它没有给出任何错误但没有显示。
我已将它们添加到xml中,例如<package.above.Pie>
我错在哪里..
public class Pie extends View
{
Context cont;
public int eventValue = 0;
RectF mediumOval;
RectF oval;
Paint p1;
Paint p2;
Paint p3;
Paint p4;
Paint p5;
RectF smallOval;
int width;
public Pie(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
cont = context;
final TypedArray styled = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Pie, 0, 0);
eventValue = styled.getInteger(R.styleable.Pie_eventValue, 16);
styled.recycle();
myInit();
}
public Pie(Context context, AttributeSet attrs)
{
this(context,attrs,0);
}
public Pie(Context context)
{
super(context);
cont = context;
myInit();
}
public void setEventValue(int eValue)
{
eventValue = eValue;
}
public void myInit()
{
width = getWidth();
p1 = new Paint(1);
p1.setColor(Color.rgb(86, 86, 86));
p1.setStyle(android.graphics.Paint.Style.FILL);
p2 = new Paint(1);
p2.setColor(Color.rgb(245, 109, 89));
p2.setStyle(android.graphics.Paint.Style.FILL);
p3 = new Paint(1);
p3.setColor(Color.rgb(71, 71, 71));
p3.setStyle(android.graphics.Paint.Style.FILL);
p4 = new Paint(1);
p4.setColor(Color.rgb(247, 247, 247));
p4.setStyle(android.graphics.Paint.Style.FILL);
p5 = new Paint(1);
p5.setColor(Color.rgb(246, 246, 246));
p5.setStyle(android.graphics.Paint.Style.FILL);
mediumOval = new RectF();
oval = new RectF();
smallOval = new RectF();
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
int i = width / 4;
oval.set(i - i / 2, i / 2, i * 3 + i / 2, i * 3 + i / 2);
mediumOval.set(i - i / 4, i - i / 4, i * 3 + i / 4, i * 3 + i / 4);
smallOval.set(i, i, i * 3, i * 3);
canvas.drawOval(oval, p1);
canvas.drawArc(oval, -90F, (int)Math.round((360D * Double.valueOf(eventValue).doubleValue()) / 100D), true, p2);
canvas.drawOval(mediumOval, p3);
canvas.drawOval(smallOval, p1);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int desiredWidth = width;
int desiredHeight = width;
int widthMode = android.view.View.MeasureSpec.getMode(widthMeasureSpec);
int widthSize = android.view.View.MeasureSpec.getSize(widthMeasureSpec);
int heightMode = android.view.View.MeasureSpec.getMode(heightMeasureSpec);
int heightSize = android.view.View.MeasureSpec.getSize(heightMeasureSpec);
int measuredWidth;
int measuredHeight;
if (widthMode == MeasureSpec.EXACTLY)
{
measuredWidth = widthSize ;
}
else if (widthMode == MeasureSpec.AT_MOST)
{
measuredWidth = Math.min(desiredWidth , widthSize);
}
else
{
measuredWidth = desiredWidth;
}
if (heightMode == MeasureSpec.EXACTLY)
{
measuredHeight = heightSize ;
}
else if (heightMode == MeasureSpec.AT_MOST)
{
measuredHeight = Math.min(desiredHeight, heightSize );
}
else
{
measuredHeight = desiredHeight;
}
setMeasuredDimension(measuredWidth, measuredHeight);
}
}
答案 0 :(得分:1)
在你的onDraw()方法中,宽度值变为0(零)..
更改此行
int i = width / 4;
到
int i = getWidth() / 4;