0
我正在創建一個自定義視圖但它不顯示。我已經將它的屬性添加到了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);
}
}
這意味着我們不能在onDraw調用之前getWidth,我是對的。 – Nepster
不,您可以在onLayout()和onMeasure()方法中的onDraw()方法之前獲取這些vies的寬度和高度...... – Chandrakanth
謝謝並且我的onMeasure方法絕對正確。我想了解創建customView背後的概念,請教我一些教程 – Nepster