在保持形状背景的同时淡入和淡出视图

时间:2013-12-20 10:09:42

标签: android animation

对于那些我遇到过一些麻烦的人来说,这是一个简单的问题。

目标:将RelativeLayout形状(xml)作为背景,当某些特定事件发生时,让它从透明变为纯红色(保持形状),然后再次变回透明状态。看起来很简单,但我无法达到目标。

首次尝试:

Relativelayout最初的形状为state_ok.xml

//state_error.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners android:radius="11dp" />
    <solid android:color="#ffff0000" />
</shape>

//state_ok.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners android:radius="11dp" />
    <solid android:color="#00ff0000" />
</shape>

//MyView with static runnable creator class for easiness's sake.
private static class FadeRunnable implements Runnable {
    final Context ctx; final RelativeLayout layout; final int from, to;
    //of course there's a constructor with these four params

    public void run() {
        Drawable[] color = {
                ctx.getResources().getDrawable(from), 
                ctx.getResources().getDrawable(to)
        };
        TransitionDrawable trans = new TransitionDrawable(color);
        layout.setBackgroundDrawable(trans);
        trans.startTransition(2500);
    }
}

final FadeRunnable in = new FadeRunnable(context, layout, R.drawable.state_error, R.drawable.state_ok);
final FadeRunnable out = new FadeRunnable(context, layout, R.drawable.state_ok, R.drawable.state_error);

Thread fading = new Thread() {
    public void run() {
        MyView.this.context.runOnUiThread(in);
        Thread.sleep(2500);
        MyView.this.context.runOnUiThread(out);
    }
};
fading.start();

结果:它立即变为红色,在2500ms后它立即变得透明并变为纯红色。我无法绕过它。

第二次尝试:使用Animations

final Runnable inRunnable = new Runnable() {
    @Override
    public void run() {
        Animation inAnim = AnimationUtils.loadAnimation(context, R.anim.fadein);
        layout.startAnimation(inAnim);
    }
};
final Runnable outRunnable = new Runnable() {
    @Override
    public void run() {
        Animation inAnim = AnimationUtils.loadAnimation(context, R.anim.fadeout);
        layout.startAnimation(inAnim);
    }
};
new Thread() {
    public void run() { 
        context.runOnUiThread(inRunnable);
        CommonToolkit.sleep(2500);
        context.runOnUiThread(outRunnable);
    }
}.start();

结果:布局的内容再次变得不透明和透明,而不是背景本身。

其他尝试

之后我用ValueAnimator尝试了一些东西,但这也没用。

然后我尝试更改背景颜色(使用for循环和setBackgroundColor(int color)),但这样就不会保持形状(我认为使用不同颜色制作256个drawable是太过分了。)

问题:
如何在保持形状xml背景的同时,将视图中的颜色从#00ff0000更改为#ffff0000并再次返回透明红色?

来源:
How to Apply Corner Radius to LinearLayout
Fade animation blinks - Android
Animate change of view background color on Android
How to reference colour attribute in drawable?
android: Animate color change from color to color
animation fade in and out
In Android, how do I smoothly fade the background from one color to another? (How to use threads)

PS
当我使用下面的代码时,它会保持循环:它从透明变为RED,然后立即再次透明等,但是,我没有一个真正的循环,所以我不知道这是如何可能的。我从xml中删除了布局中的背景条目。

Thread forThread = new Thread() {
    public void run() {
        for (int i = 0; i < 256; i++) {
            final int j = i;
            context.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    layout.setBackgroundColor(Color.argb(j, 0xff, 0, 0));
                }
            });
            CommonToolkit.sleep(10);
        }
        for (int k = 255; k > -1; k++) {
            final int l = k;
            context.runOnUiThread(new Runnable() { 
                @Override
                public void run() {
                    layout.setBackgroundColor(Color.argb(l, 0xff, 0, 0));
                }
            });
            CommonToolkit.sleep(10);
        }
    }
};
forThread.start();

2 个答案:

答案 0 :(得分:3)

制作2个可绘制的xmls,颜色为红色和透明。 (layout_bg_red,layout_bg_trans)。 创建另一个可绘制的“my_layout_transition”:

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/state_ok" />
    <item android:drawable="@drawable/state_error" />
</transition>

将此转换xml指定为您想要的视图的背景。

将这两种方法添加到活动中:

public void startTransition(int millis, View v) {
    if (v.getBackground() instanceof TransitionDrawable) {
        TransitionDrawable d = (TransitionDrawable)v.getBackground();
        d.startTransition(millis);
    }
}

public void reverseTransition(int millis, View v) {
    if (v.getBackground() instanceof TransitionDrawable) {
        TransitionDrawable d = (TransitionDrawable)v.getBackground();
        d.reverseTransition(millis);
    }
}

然后调用startTransition(500,myLayoutView)和reverseTransition(500,myLayoutView);

启动然后反向转换的示例:

public void startAndReverseTransition(final int millis, final View v) {
    if (v.getBackground() instanceof TransitionDrawable) {
        final TransitionDrawable d = (TransitionDrawable)v.getBackground();
        d.startTransition(millis);
        v.postDelayed(new Runnable() {
            @Override
            public void run() {
                d.reverseTransition(millis);
            }
        }, millis);
    }    
}

答案 1 :(得分:0)

试试这个: transparent.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="11dp" />
<solid android:color="#00000000" />

</shape>

red.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="11dp" />
<solid android:color="#FF0000" /></shape>

bg_translate.xml

<transition xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:drawable="@drawable/transparent"/>
<item android:drawable="@drawable/red"/></transition>

将bg_translate.xml设为背景android:background="@drawable/bg_translate" 现在代码更改backgroumd颜色:

  

TransitionDrawable transition = (TransitionDrawable) v.getBackground(); transition.startTransition(time);

并使用

反转它
transition.reverseTransition(time);

时间是以毫秒为单位的淡入淡出时间