【问题标题】:Rotate a single RotateDrawable in a LayerDrawable在 LayerDrawable 中旋转单个 RotateDrawable
【发布时间】:2012-02-07 17:01:11
【问题描述】:

我正在构建一个包含某种指南针的应用程序,我想使用 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);

这没有任何作用。我做错了什么?

【问题讨论】:

    标签: android


    【解决方案1】:

    您需要确保在设置关卡之前获得特定的 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);
    

    【讨论】:

    • 你救了我!谢谢!
    猜你喜欢
    • 2020-12-06
    • 2014-12-16
    • 2013-03-26
    • 2016-05-04
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多