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

Ste*_*eve 33 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>
Run Code Online (Sandbox Code Playgroud)

而我的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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激

干杯

史蒂夫

小智 99

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

如果您使用的是API级别11或更高级别,则可以在LinearLayout的背景颜色上使用ObjectAnimator.

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

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

希望这可以帮助!


xan*_*ndy 12

在循环中,您在背景上的设置非常快,以至于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();
}
Run Code Online (Sandbox Code Playgroud)

这是代码的线程版本.

  • 谢谢!:)通过一些小的改动,它得到了它.我不得不让ia全局化,因为它无法在run方法中访问(否则我可以通过它).我还必须调整try/catch,因为它不会像我那样编译.非常感谢,这让我疯了几天.我将玩线程,看看我是否可以使用此代码创建一些有趣的东西. (5认同)
  • 这不是一个好习惯.你不允许从bg线程更新UI.@艾米的建议是正确的.有一个项目http://nineoldandroids.com/实现了Android 3.0之前的ObjectAnimator (3认同)

小智 8

更好地回答这个问题就是使用 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();
Run Code Online (Sandbox Code Playgroud)