Posted By

Znupi on 05/05/11


Tagged

android


Versions (?)

Dynamic rounded rectangle [android]


 / Published in: Java
 

This creates a rounded rectangle (10x20) with a given color (such as #AABBCC) as a drawable, which you can later use anywhere (I use it in TextView.setCompoundDrawables).

  1. public static ShapeDrawable createUserDrawable(String color) {
  2. ShapeDrawable drawable = new ShapeDrawable();
  3. drawable.setBounds(0, 0, 10, 20);
  4. float radius = 4;
  5. float[] radii = new float[] {radius, radius, radius, radius, radius, radius, radius, radius};
  6. Shape shape = new RoundRectShape(radii, new RectF(), radii);
  7. drawable.setShape(shape);
  8. Paint paint = drawable.getPaint();
  9. paint.setColor(Color.parseColor(color));
  10. return drawable;
  11. }

Report this snippet  

You need to login to post a comment.