0

我希望我的应用程序在使用线程进入另一个页面之前先有一个褪色的图片。下面是我使用的代码,它对我来说效果很好。但是,它在线程末尾的白页中停止。我该怎么做才能在不点击任何东西的情况下进入下一个活动?或者在页面变白后,我应该使用什么代码才能进入下一个活动?

package com.kfc;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.*;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class Intro extends Activity {
    LinearLayout screen;
    Handler handler = new Handler();
    int i;
    Intent intent;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.introxml);

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

        (new Thread(){
            @Override
            public void run(){
                for(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(Exception e){ break; }
                }
                for(i=255; i>0; 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(Exception e){ break; }
                }
                startActivity(new Intent(Intro.this,NewKFCActivity.class));
            }
        }).start();
    }
}
4

1 回答 1

1

在 for 循环退出后。添加代码以开始新活动。

startActivity(new Intent(Intro.this,NewACtivity.class));

您需要将其放在 for 循环之外。如果你把它放在 start 方法之后,它将在线程完成之前执行。您可能还需要使用 Intro.this 来确定“this 变量”的范围。还记得在清单文件中添加新活动为

<activity android:name=".NewActivity"/>

就用这个

screen = (FrameLayout) findViewById(R.id.layout);
(new Thread(){
    @Override
public void run(){
    for(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(100); }
           catch(Exception e){ break; }
        }
        startActivity(new Intent(TabTester.this,NewKFCActivity.class));
    }
}).start();

此指针应指向 Intro 活动对象。但是在线程内部,这将引用当前线程对象(不确定它究竟指向什么),因此您需要使用“Intro.this”来限定它的范围,这意味着“使用指向 Intro 活动的 this”

当您将 setBackgroundColor 用于同一视图时,您的背景图片将被覆盖。一种方法是使用布局,外部布局将具有背景图片,内部布局将应用 setBackgroundColor。例如:

您还需要更改代码

screen.setBackgroundColor(Color.argb(255, i, i, i));

screen.setBackgroundColor(Color.argb(120, i, i, i));

alpha 值设置为 255,这意味着不透明并且不会显示背景图像。

于 2011-11-07T12:26:48.257 回答