在Android中,如何将背景从一种颜色平滑淡化到另一种颜色? (如何使用线程)

时间:2011-03-05 00:42:07

标签: android

我已经玩了几个星期的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);
        }
    }
}

非常感谢任何帮助

干杯

史蒂夫

6 个答案:

答案 0 :(得分:96)

如果您感兴趣,我发现了另一种更改背景颜色的方法 - 我认为使用动画会比您现有的更容易:)

如果您使用的是API Level 11或更高版本,则可以在LinearLayout的背景颜色上使用ObjectAnimator。

 ObjectAnimator colorFade = ObjectAnimator.ofObject(screen, "backgroundColor", new ArgbEvaluator(), Color.argb(255,255,255,255), 0xff000000);
  colorFade.setDuration(7000);
  colorFade.start();

另外,请注意,必须使用32位的int颜色代码。有关详细信息,请参阅http://developer.android.com/reference/android/graphics/Color.html,但您可以使用Color.argb,Color.rgb,我在上面使用的十六进制,或者查看int颜色常量。

希望这有帮助!

答案 1 :(得分:11)

在你的循环中,你在背景上的设置太快,以至于UI不能(不会)安排显示更新。是的,您最好使用第二个线程来更新背景,否则您将停止UI线程。请尝试以下方法:

LinearLayout screen;
Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    screen = (LinearLayout) findViewById(R.id.screen);

    (new Thread(){
        @Override
        public void run(){
            for(int i=0; i<255; i++){
                handler.post(new Runnable(){
                    public void run(){
                        screen.setBackgroundColor(Color.argb(255, i, i, i));
                    }
                });
                // next will pause the thread for some time
                try{ sleep(10); }
                catch{ break; }
            }
        }
    }).start();
}

这是代码的线程版本。

答案 2 :(得分:6)

更好地回答这个问题就是使用ObjectAnimator

ObjectAnimator colorFade = ObjectAnimator.ofObject(view, "backgroundColor" /*view attribute name*/, new ArgbEvaluator(), mContext.getResources().getColor(R.color.colorMenuOverlay) /*from color*/, Color.WHITE /*to color*/);
                colorFade.setDuration(3500);
                colorFade.setStartDelay(200);
                colorFade.start();

答案 3 :(得分:3)

如果有人在寻找3.0之前的解决方案,Nine Old Androids将允许您在设备上使用动画API返回到Android 1.0。 http://nineoldandroids.com/

答案 4 :(得分:1)

针对旧设备的更多解决方案:

TIMER

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Timer myTimer = new Timer();
    final View myview = getWindow().getDecorView().findViewById(
            android.R.id.content);

    myTimer.schedule(new TimerTask() {
        int color = 0;
        @Override
        public void run() {
            // If you want to modify a view in your Activity
            runOnUiThread(new Runnable() {
                public void run() {
                    color++;
                    myview.setBackgroundColor(Color.argb(255, color, color,
                            color));
                    if (color == 255)
                        myTimer.cancel();
                }
            });
        }
    }, 1000, 20); // initial delay 1 second, interval 1 second

}

主题

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new Thread() {
        int color = 0;
        View myview = getWindow().getDecorView().findViewById(
                android.R.id.content);
        @Override
        public void run() {
            for (color = 0; color < 255; color++) {
                try {
                    sleep(20);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            myview.setBackgroundColor(Color.argb(255,
                                    color, color, color));
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
}

答案 5 :(得分:0)

private fun applyColorFade(fromColor: Int, toColor: Int) {
    ObjectAnimator.ofObject(
        toolbar, "backgroundColor", ArgbEvaluator(), 
        fromColor,
        toColor
    ).apply {
      duration = 2000
      startDelay = 200
      start()
    }
}

//这是一种可重用的用于视图颜色淡入淡出动画的方法