在Android中,如何顺利褪色从一种颜色背景到另一个? (如何使用线程) [英] In Android, how do I smoothly fade the background from one color to another? (How to use threads)

查看:118
本文介绍了在Android中,如何顺利褪色从一种颜色背景到另一个? (如何使用线程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩和关闭了几个星期的机器人编程,和我想要得到的东西的工作,看似简单,但我想我失去了一些东西。

I've been playing with Android programming on and off for a couple of weeks, and I'm trying to get something to work that seems simple, but I think I am missing something.

我所试图做的是有,比如说背景褪色,白到黑的顺利进行。

What I am trying to do is have the background fade from, say, white to black smoothly.

我已经尝试了一些东西,其中没有一个似乎工作。

I've tried a few things, none of which seem to work.

第一件事是用一个for循环和setBackgroundColor方法的LinearLayout,改变R,G,B值一起从0到255这是行不通的。

The first thing I did was using a for loop and the setBackgroundColor method for LinearLayout, changing the R, G, and B values together from 0 to 255. It doesn't work.

我可以做的设置更改单的,但是当我做循环我什么也没有,最后一个值。我认为正在发生的事情是用户界面锁定,而循环过程中和解冻时,循环结束。我试图把拖延环路(丑陋的嵌套循环延迟和视频下载),都没有用。

I can do one-of settings changes, but when I do the loop I get nothing but the last value. What I think is happening is the UI is locking up while the loop is in progress and unfreezing when the loop ends. I've tried putting delays in the loop (ugly nested loop delays and Thread.sleep), all to no avail.

谁能给我任何指针就如何得到这个工作?我是否需要第二个线程做改变颜色?我有螺纹的一个模糊的概念,但我从来没有使用过。

Can anyone give me any pointers as to how to get this working? Do I need a second thread to do the change to the color? I've got a vague idea of threads, although I have never used them.

我的样品code表示大概是我想要做的是如下:

My sample code showing roughly what I am trying to do is as follows:

main.xml中是:

main.xml is:

<?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 code是(0.01 INC只是作为一个丑陋的延迟机制来试试,看看颜色的观察缓慢变化。):

And my java code is (the 0.01 inc. is just to act as an ugly delay mechanism to try to see the viewing of colors change slowly):

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);
        }
    }
}

任何帮助将大大AP preciated

Any help would be greatly appreciated

干杯

史蒂夫

推荐答案

在你的循环,您的背景设置是如此之快,用户界面​​是不(不会)能够调度显示的更新。是的,你最好使用第二个线程来更新后台,否则你将停止UI线程。请尝试以下操作:

In your loop, your setting on background is so fast that the UI is not (will not) able to schedule the update of display. Yes, you better use a second thread to update the background or else you will stall the UI thread. Try following:

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();
}

这是你的code螺纹版本。

This is a threaded version of your code.

这篇关于在Android中,如何顺利褪色从一种颜色背景到另一个? (如何使用线程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆