Zar E Ahmer
Zar E Ahmer

Reputation: 34380

custom view not drawing

I am creating a customView but it is not displaying. I have added it's attribute to attrs.xml..

I have implemented its constructor, I simply draw an arc and an oval but it is not displaying.

It is not giving any error but not displaying.

I have added them in xml like <package.above.Pie>

Where am i wrong..

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);
    }
}

Upvotes: 0

Views: 300

Answers (1)

Chandrakanth
Chandrakanth

Reputation: 3831

In your onDraw() method the width value is getting as 0(zero)..

Change this line

int i = width / 4;

to

int i = getWidth() / 4;

Upvotes: 1

Related Questions