在我正在开发的应用程序中,我对加载屏幕的介绍感兴趣。它会在持续时间后自动移动到下一个屏幕。
介绍本身,工作得很好。以及延迟系统的线程。我的问题是让他们一起工作。
守则:
public class MainActivity extends Activity implements OnClickListener {
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_screen);
final Thread t1=new Thread(new Runnable() {
public void run() {
iv=(ImageView)findViewById(R.id.imgBtn1);
iv.setBackgroundResource(R.anim.loading_i_animation);
AnimationDrawable anim=(AnimationDrawable) iv.getBackground();
anim.start();
}
});
t1.start();
try {
Thread.sleep(2000);
finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
Intent st=new Intent(MainActivity.this,Welcome.class);
startActivity(st);
}
}此代码的结果是打开线程睡眠时间持续时间的白色屏幕。然后通过意图打开"Welcome.class“屏幕。
它只是跳过了loading_screen,因为它根本就不存在。
希望你们能帮我解决这个问题。
发布于 2014-01-23 14:56:52
你把你的sleep放在UI线程上,这会阻止安卓在它完成之前显示任何东西。尝试以下代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_screen);
iv=(ImageView)findViewById(R.id.imgBtn1);
iv.setBackgroundResource(R.anim.loading_i_animation);
AnimationDrawable anim=(AnimationDrawable) iv.getBackground();
anim.start();
new Handler().postDelayed(
new Runnable() {
public void run() {
Intent st=new Intent(MainActivity.this,Welcome.class);
startActivity(st);
finish();
}
}, 2000);
}这样,延迟将在一个分离的线程上运行,但2秒后它将更改回主线程,并运行您在Runnable中指定的代码。
https://stackoverflow.com/questions/21311499
复制相似问题