3

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

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>  

我想将此复合可绘制对象用作按钮背景:

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

但是我怎样才能获得对可绘制过渡的引用,以便开始过渡呢?这不是只写的解决方案,这当然行不通:

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

0 回答 0