Ruben has already pointed out the most useful comments, so I will just focus on the implementation of this story. There are several approaches using reflection that are likely to give you what you are looking for.
First, you need to (ab) use the private GradientDrawable constructor, which accepts a GradientState reference. Unfortunately, the latter is the final subclass with package visibility, so you cannot easily access it. To use it, you will need to dive even further into the use of reflection or imitate its functionality in your own code.
The second approach is to use reflection to get the private member variable mGradientState, which, fortunately, has a getter in the form getConstantState() . This will give you a ConstantState, which at run time is actually a GradientState, and so we can use reflection to access our members and change them at run time.
To support the above statements, here is a somewhat basic implementation for creating a ring shape extracted from code:
RingDrawable.java
public class RingDrawable extends GradientDrawable { private Class<?> mGradientState; public RingDrawable() { this(Orientation.TOP_BOTTOM, null); } public RingDrawable(int innerRadius, int thickness, float innerRadiusRatio, float thicknessRatio) { this(Orientation.TOP_BOTTOM, null, innerRadius, thickness, innerRadiusRatio, thicknessRatio); } public RingDrawable(GradientDrawable.Orientation orientation, int[] colors) { super(orientation, colors); setShape(RING); } public RingDrawable(GradientDrawable.Orientation orientation, int[] colors, int innerRadius, int thickness, float innerRadiusRatio, float thicknessRatio) { this(orientation, colors); try { setInnerRadius(innerRadius); setThickness(thickness); setInnerRadiusRatio(innerRadiusRatio); setThicknessRatio(thicknessRatio); } catch (Exception e) {
The above can be used as follows to create a RingDrawable from the code and display it in a standard ImageView.
ImageView target = (ImageView) findViewById(R.id.imageview); RingDrawable ring = new RingDrawable(10, 20, 0, 0); ring.setColor(Color.BLUE); target.setImageDrawable(ring);
This will show a simple opaque blue ring in ImageView (inner radius of 10 units, 20 units). You will need not to set the width and height of the ImageView in wrap_content unless you add ring.setSize(width, height) to the code above so that it displays.
Hope this helps you anyway.