【发布时间】:2011-03-05 00:42:07
【问题描述】:
我已经断断续续地玩了几个星期的 Android 编程,我试图让一些看起来很简单的东西工作起来,但我认为我错过了一些东西。
我想要做的是让背景从白色平滑地渐变到黑色。
我尝试了一些方法,但似乎都不起作用。
我做的第一件事是使用 for 循环和 LinearLayout 的 setBackgroundColor 方法,将 R、G 和 B 值一起从 0 更改为 255。它不起作用。
我可以做一个设置更改,但是当我做循环时,我只得到最后一个值。我认为正在发生的是 UI 在循环进行时锁定,而在循环结束时解冻。我尝试在循环中添加延迟(丑陋的嵌套循环延迟和 Thread.sleep),但都无济于事。
谁能给我任何关于如何让它工作的指示?我需要第二个线程来更改颜色吗?我对线程有一个模糊的概念,虽然我从未使用过它们。
我的示例代码大致显示了我正在尝试做的事情如下:
main.xml 是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/screen"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
而我的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);
}
}
}
任何帮助将不胜感激
干杯
史蒂夫
【问题讨论】:
标签: android