如何获得嵌入式Drawable的引用?

时间:2015-03-29 16:45:39

标签: android xml drawable nested transitiondrawable

我有一个外部剪辑drawable(clip_drawable.xml),它嵌入了另一个过渡drawable(transition_drawable.xml),它本身指的是2个形状drawables。

clip_drawable.xml:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="vertical"
    android:drawable="@drawable/transition_drawable"
    android:gravity="top" >

</clip>  

transition_drawable.xml:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="oval" >
            <stroke
                android:width="3dp"
                android:color="@color/titan" />

            <gradient
                android:angle="270"
                android:centerColor="@android:color/transparent"
                android:centerY="0.4"
                android:startColor="@color/titan" />

            <padding
                android:bottom="10dp"
                android:left="15dp"
                android:right="15dp"
                android:top="10dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="oval" >
            <stroke
                android:width="3dp"
                android:color="@color/titan" />

            <gradient
                android:angle="270"
                android:centerColor="@android:color/transparent"
                android:centerY="0.1"
                android:startColor="@color/titan" />

            <padding
                android:bottom="10dp"
                android:left="15dp"
                android:right="15dp"
                android:top="10dp" />
        </shape>
    </item>

</transition>  

我想使用这个复合drawable作为按钮背景:

<Button
    android:id="@+id/continue_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/clip_drawable"
    android:text="@string/label_continue"
    android:textColor="@android:color/white"
    android:textSize="20sp" />

在代码中我可以引用ClipDrawable并通过说:

设置一个级别
final ClipDrawable clipDrawable = (ClipDrawable) button.getBackground();
clipDrawable.setLevel(3000);

但是如何才能获得对过渡drawable的引用,以便开始过渡?这不是写作的解决方案,当然这不会起作用:

final TransitionDrawable transitionDrawable = (TransitionDrawable) this.getResources()
                .getDrawable(R.drawable.transition_drawable);
transitionDrawable .startTransition(1000);

0 个答案:

没有答案