我正在构建一个应用程序,其中包含某种类型的指南针,我想使用LayerDrawable来绘制和动画指南针。LayerDrawable由指南针背景的静态背景图像和旋转部分的RotateDrawable组成。下面是我的可绘制资源的XML代码:
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>LayerDrawable显示得很好,但是如何旋转它呢?这是我尝试过的代码:
compassText = (RotateDrawable)getResources().getDrawable(R.drawable.compass_text);
compassText.setLevel(5000);这不会做任何事情。我做错了什么?
发布于 2012-02-07 11:34:41
您需要确保获得在设置级别之前已膨胀的特定可绘制对象,而不是来自资源的通用可绘制对象。试试这个:
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);https://stackoverflow.com/questions/9141695
复制相似问题