0

I am just trying to implement a customView from scratch i.e by extending the view class and overriding the onDraw() method. Just trying to build a simple view, a view which just draws a circle for now. I am facing some issue in aligning it and i am not able to understand how android is calculating the views dimensions. Just having only the view i.e setContentView(new MyCustomView(this)) works fine... it takes the entire space and draws the circle. But if i impose any constraints i.e giving margin, or aligning it in centreparent makes my view completely lost and it doesnt draw anything. The issue is the view is getting clipped by its parent but not able to understand why its getting clipped. Any help around this would be greatly appreciated. Here is my code.

Here is my customView

    public class MyCustomView extends View {

    private Paint myPaint=null;
    private boolean useCenters;
    private float xCoordinate;
    private float yCoordinate;
    private float viewWidth;
    private float viewHeight;
    private int totalTime;
    private static float SWEEP_INC ;
    private RectF myRect;
    private boolean shouldInvalidate;
    private float mSweep;

    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initPaintComponents();
    }

    public MyCustomView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyCustomView(Context context) {
        this(context,null);
    }

    private void initPaintComponents() {

        myPaint = new Paint();
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(4);
        myPaint.setColor(0x880000FF);
        useCenters = false;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        calculateCoordinates();
    }

    private void calculateCoordinates() {

        xCoordinate = getX();
        yCoordinate = getY();
        viewWidth = getWidth();
        viewHeight = getHeight();
        myRect = new RectF(xCoordinate+3, yCoordinate+3, xCoordinate+viewWidth-(viewWidth/10), yCoordinate+viewHeight-(viewHeight/10));
        Log.i("SAMPLEARC","xcoordinate: "+xCoordinate+" ycoordinate: "+yCoordinate+" view width:"+viewWidth+" view height:"+viewHeight+" measured width: "+getMeasuredWidth()+"measured height:"+getMeasuredHeight());
    }

    public int getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(int totalTime) {
        this.totalTime = totalTime;
        SWEEP_INC = (float)6/totalTime;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(myRect, 0, mSweep, useCenters, myPaint);

         mSweep += SWEEP_INC;
         if(mSweep > 280)
         {
             myPaint.setColor(0x888800FF);
         }

        invalidate();
    }
}

MyActivity:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        MyCustomView myView = (MyCustomView) findViewById(R.id.customimg);
        myView.setTotalTime(10);
    }

main.xml

  RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
   com.example.anim.MyCustomView android:id="@+id/customimg"
  android:layout_width="100dp"
  android:layout_height="100dp"
  android:layout_centerInParent="true"

if i remove that centerInParent in xml it gets drawn. so callling setMeasureDimentions() in onMeasure() doesnt have any affect either. But the xcoodinate,ycoordinate,viewWidth and viewHeight seems to give the correct values. Just need to understand why the view is getting clipped and how android is calculating the dimensions at runtime. And how do i consider the margin paramters while drawing these customViews. Any help would be greatly appreciated.

Thanks in advance

  • see this it will help you to solve your problem...http://stackoverflow.com/questions/9205838/how-to-draw-circle-with-partitioned-in-android – himanshu May 15 '12 at 10:04
  • Hi Himanshu thanks for your help.... but i am actually not trying to draw a static circle....i am palnning to design my own customview which would be similar to any View in android (textview,edittext), a re-usable view which i can use in any application and can include in any xml. Is there anything that i am missing in onMeasure() of my view? do i have to consider any other parameters while calculating the dimensions of my rectF? – user1395403 May 15 '12 at 11:03

2 Answers2

0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:lib="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

  <com.ind.Custom_Attribute.LibView  
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"    
  android:text=""
  lib:xattr="Custom attribute APPLIED!"
  />

</LinearLayout>     
sss
  • 140
  • 1
  • 12
0
<resources>
   <declare-styleable name="CustomAttrs">
    <attr name="xattr" format="string" />
</declare-styleable>
</resources>

in values/attrs.xml

sss
  • 140
  • 1
  • 12
  • this is for adding extra paramters to the custom component. But thats not my case... i want the existing parameters(margin,centerinparent) to work with my custom view.thanks a lot for your help. – user1395403 May 15 '12 at 10:41
  • Is there anything that i am missing in onMeasure() of my view? do i have to consider any other parameters while calculating the dimensions of my rectF? – user1395403 May 15 '12 at 11:06