Saturday, May 12, 2012

Android ShapeDrawable Tutorial - RoundRectShape()

I want to draw rectangles in my android application and discovered that ShapeDrawable is the tool I need to do it.  If you look through the provided Android demo's that come with your Android SDK you will find a ShapeDrawable demonstration.   .../ApiDemos/src/com/example/android/apis/graphics/ShapeDrawable1.java


I looked at it and played around with it to better understand the ShapeDrawable library.


Below is the code snippet pulled from ShapeDrawable1.java

float[] outerR = new float[] { 35, 35, 35, 35, 0, 0, 0, 0 };
RectF   inset = new RectF(6, 6, 6, 6);
float[] innerR = new float[] { 12, 12, 0, 0, 12, 12, 0, 0 };

mDrawables[4] = new ShapeDrawable(new RoundRectShape(outerR, inset, innerR));


So let's focus on the parameters on RoundRectShape().

  1. inset defines the shape of the rectangle that will be inside our RoundRectShape.  It basically defines where the left side, right side, top and bottom of the rectangle will be.
  2. outerR defines the the radius of the outer corner of the RoundRectShape.  So in this case, we've defined the top left and top right corners of RoundRectShape to have a rounded corner of 35.  The larger the numbers, the curvier your corner will look.
  3. innerR defines the radius of the inner corner of the RoundRectShape.  Once more, the larger the numbers, the curvier your corner will look.
I recommend you try changing the numbers on your own to get a better feel for what the numbers of outerR and innerR do.  

I still haven't figured out how the PathShape works. When I do, I'll add it this blog.

1 comment:

  1. Not sure if this post really answered my question, but I love that I found your blog. I'm a programmer/mom as well, though I stopped working after my second was born.

    It is interesting to read your perspective. Before having kids I worked at a very small company where there was only one other developer. It was a low stress work environment, but as such, paid quite a bit less. It was good $$ for entry level, but didn't really scale.

    Now that my kids are getting older I decided to try my hand at mobile apps. In March I released my first android app and am hoping to release my next one within the next week or two.

    ReplyDelete