Reputation: 31
I seem to be really stuck with the following problem:
I have a shape defined in an XML file, described as followed:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/star_shape_item">
<rotate android:fromDegrees="45">
<shape android:shape="oval" >
<gradient
android:endColor="#32816f6f"
android:gradientRadius="40%p"
android:startColor="#ffffff"
android:type="radial" >
</gradient>
<stroke
android:width="1dp"
android:color="@android:color/darker_gray" />
</shape>
</rotate>
</item>
</layer-list>
I am drawing this shape on a canvas multiple times.
Now, what i would like to do is to be able to dynamically change the center of the gradient in my programming code. I am able to get the shape from my resources with:
LayerDrawable layers = (LayerDrawable) ContextCompat.getDrawable(context, R.drawable.star_shape);
RotateDrawable starShape = (RotateDrawable)(layers.findDrawableByLayerId(R.id.star_shape_item));
But how would i get the GradientDrawable out of this?
Upvotes: 1
Views: 1242
Reputation: 31
finally found the answer in case someone else encounters a similar problem:
LayerDrawable layers = (LayerDrawable) ContextCompat.getDrawable(context, R.drawable.star_shape);
RotateDrawable starShape = (RotateDrawable) (layers.findDrawableByLayerId(R.id.star_shape_item));
GradientDrawable shapeGradient = (GradientDrawable) starShape.getDrawable();
Upvotes: 2