我已经玩了几个星期的Android编程,我正在尝试一些看起来很简单的工作,但我想我缺少一些东西。
我想做的是让背景从白色到黑色平滑地退色。
我已经尝试了一些事情,没有一个似乎工作。
我做的第一件事是使用for循环和LinearLayout的setBackgroundColor方法,将R,G和B值从0到255改变。它不起作用。
我可以做一些设置更改,但是当我做循环时,我只得到最后一个值。我认为正在发生的是,当循环正在进行时,UI被锁定,并且循环结束时解冻。我已经尝试在循环中延迟(丑陋的嵌套循环延迟和Thread.sleep),都无济于事。
有谁能给我任何关于如何使这个工作的指针?我需要第二个线程来改变颜色吗?虽然我从来没有使用过线程,但我有一个模糊的想法。
我的示例代码大致显示了我想要做的如下:
main.xml是:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/screen"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
而我的java代码是(0.01 inc。只是作为一个丑陋的延迟机制,试图看到颜色的观察变化缓慢):
package nz.co.et.bgfader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class bgfader extends Activity {
LinearLayout screen;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
screen = (LinearLayout) findViewById(R.id.screen);
for (int i = 0; i < 65535; i+=0.01) {
screen.setBackgroundColor(0xff000000 + i);
}
}
}
任何帮助将不胜感激
干杯
史蒂夫