Reputation: 41
I'm building an app that has some sort of compass in it, and I want to use a LayerDrawable to draw and animate the compass. The LayerDrawable consists of a static background image for the compass background, and a RotateDrawable for the rotating part. Here's my XML code for the drawable resources:
compass_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/compasstext"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%" />
compass_layers.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/compassbackground" />
<item android:drawable="@drawable/compass_text" />
</layer-list>
The LayerDrawable displays just fine, but how do I rotate it? This is the code I tried:
compassText = (RotateDrawable)getResources().getDrawable(R.drawable.compass_text);
compassText.setLevel(5000);
This does nothing. What am I doing wrong?
Upvotes: 3
Views: 2791
Reputation: 31
You need to make sure you get the specific Drawable that was inflated before you set the level, rather than the generic Drawable object from resources. Try this:
ImageView iv = (ImageView) findViewById(R.id.xxx); ////where xxx is the id of your ImageView (or whatever other view you are using) in your xml layout file
LayerDrawable lv = (LayerDrawable) iv.getDrawable();
compassText = (RotateDrawable) lv.getDrawable(1); //1 should be the index of your compassText since it is the second element in your xml file
compassText.setLevel(5000);
Upvotes: 3