在LayerDrawable中旋转单个RotateDrawable

3

我正在开发一款带有指南针功能的应用程序,我想使用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);

这没做任何事情。我做错了什么吗?

1个回答

3

在设置级别之前,您需要确保获取到的是在充气之前具体的Drawable对象,而不是来自资源的通用Drawable对象。请尝试以下方法:

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);

你救了我!谢谢! - Tgo1014

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接