【问题标题】:Fade animation in shape background gradient形状背景渐变中的淡入淡出动画
【发布时间】:2013-01-19 08:02:06
【问题描述】:

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

<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" />

上述形状在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" >

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

【问题讨论】:

    标签: android animation gradient


    【解决方案1】:

    创建一个 TransitionDrawable 以在您用于褪色和未褪色背景的两个可绘制对象之间淡入淡出。

    <?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>
    

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

    然后在代码中使用,开始过渡。我已经硬编码了 2 秒(2000 毫秒)

    TransitionDrawable trans = (TransitionDrawable) myLayout.getBackground();
    trans.startTransition(2000);
    

    【讨论】:

    • 太棒了。我会尝试一下。谢谢。
    • 这很重要:我在 XML 中没有 &lt;solid android:color="#000000"/&gt; 属性,它在 4.3 上不起作用。仅使用渐变属性是行不通的。 (我知道这是一个旧线程)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    相关资源
    最近更新 更多