我希望我的应用程序在使用线程进入另一个页面之前先有一个褪色的图片。下面是我使用的代码,它对我来说工作得很好。然而,它在线程结束时停在白页上。我该怎么做,这样它就会去下一个活动,而不点击任何东西?或者在页面变成白色后,我应该使用什么代码,以便它现在进入下一个活动?
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();
}
}
for循环退出后。添加代码以启动新活动。
startActivity(new Intent(Intro.this,NewACtivity.class));
你需要把它放在for循环之外。如果把它放在start方法之后,它将在线程完成之前执行。您可能还需要使用introthis来确定"this变量"的作用域。还要记住在manifest文件中以
的形式添加新活动<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活动对象。但是在线程内部,this将引用当前线程对象(我不确定它究竟指向什么),所以您需要使用'Intro '来确定它的作用域。this'意味着'使用this指向Intro活动'
当你对同一个视图使用setBackgroundColor时,你的背景图片将被覆盖。这样做的一种方法是使用布局,外部布局将有背景图片和内部布局将有setBackgroundColor应用。例如:
您还需要修改代码
screen.setBackgroundColor(Color.argb(255, i, i, i));
screen.setBackgroundColor(Color.argb(120, i, i, i));
alpha值被设置为255,这意味着不透明,将不显示背景图像