在形状背景渐变淡化动画

Mik*_*wig 6 animation android gradient

我找不到任何符合我想要的帖子......我有一个矩形:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp" android:color="#FFFFFF" />
<padding android:left="4dp"
    android:top="4dp"
    android:right="4dp"
    android:bottom="4dp" />
<corners android:radius="6dp" />
<gradient
        android:angle="90"
        android:startColor="#FF000000"
        android:centerColor="#FF000000"
        android:endColor="#FF0000cd"
        android:type="linear" />
Run Code Online (Sandbox Code Playgroud)

上述形状用作以下背景中的背景属性RelativeLayout:

<RelativeLayout
    android:id="@+id/layoutBottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animation="@anim/slide_top_to_bottom"
    android:background="@drawable/rectangle4"
    android:orientation="vertical" >
Run Code Online (Sandbox Code Playgroud)

我想在这个矩形中慢慢淡入和淡出不同的渐变.问题是,这RelativeLayout会随着内容的变化而动态地改变大小,所以我不能只使用图像文件作为背景(这实际上会隐藏经常变化的布局的文本内容).我已经尝试删除android:background属性,只是在我的图像文件中使用,RelativeLayout但是这个图像占据了整个屏幕,因为我必须使用wrap_content,这个图像文件必须放在代码中才能占用文本视图RelativeLayout(否则文本被覆盖图片).我知道如何制作Alpha补间动画,但我不知道如何将android:background属性分配给动画.有任何想法吗?

Sim*_*mon 7

创建一个TransitionDrawable,在用于淡化和未消除背景的两个drawable之间淡入淡出.

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- use whatever drawables you want, gradients, shapes etc-->
    <item android:drawable="@drawable/faded" />
    <item android:drawable="@drawable/unfaded" />
</transition>
Run Code Online (Sandbox Code Playgroud)

http://developer.android.com/reference/android/graphics/drawable/TransitionDrawable.html

然后在代码中使用,开始转换.我硬编码2秒(2000毫秒)

TransitionDrawable trans = (TransitionDrawable) myLayout.getBackground();
trans.startTransition(2000);
Run Code Online (Sandbox Code Playgroud)