【发布时间】:2014-01-09 04:02:36
【问题描述】:
给你们一个简单的问题,我遇到了很多麻烦。
目标:拥有一个带有形状 (xml) 作为背景的RelativeLayout,当特定的事情发生时,让它从透明变为纯红色(保持形状),然后再次变回透明。看起来很容易,但我一直无法达到我的目标。
第一次尝试:
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();
结果:它立即变为红色,并在 2500 毫秒后立即变为透明并逐渐变为纯红色。我无法绕开它。
第二次尝试:使用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 个可绘制对象有点过头了)。
问题:
如何在保持形状 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:
当我使用下面的代码时,它会一直循环:它从透明变为红色,然后立即再次透明等等,但是,我没有真正的循环,所以我不知道这是怎么可能的。我从 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();
【问题讨论】: