代码:(WelcomeActivity ---- hyb project)
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.welcome);
settings = getSharedPreferences(SETTINGS, 0);
flagIsShowIntro=settings.getBoolean(INTRO_SETTING, false);
Runnable r = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
try{
Thread.sleep(2000);// wait 2s
if(flagIsShowIntro)
startActivityForResult(IntentHelperUtil.getInstance().getIntent(WelcomeActivity.this, IntroductionActivity.class),0);
else
startActivityForResult(IntentHelperUtil.getInstance().getIntent(WelcomeActivity.this, HomeActivity.class),0);
overridePendingTransition(R.anim.fade, R.anim.hold); //animation fade in , fade out
}catch(InterruptedException e){
e.printStackTrace();
}
}
};
new Thread(r).start();
}
这是项目 的第一个页面,要达到的效果是等待2秒钟之后,淡入淡出到第二个页面。
运行后,发现,其实地方的动画效果都ok, 可是这个地方activity切换显示的还是系统默认的animation
why ?
尝试着解决:
修改代码:
使用 activty.runOnUIThread()方法,结果要黑屏2秒之后直到到了第二个页面,不成功。
使用
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.welcome);
settings = getSharedPreferences(SETTINGS, 0);
flagIsShowIntro=settings.getBoolean(INTRO_SETTING, false);
TimerTask tt = new MyTask();
Timer timer = new Timer();
timer.schedule(tt, 2000);
}
private class MyTask extends TimerTask{
@Override
public void run() {
if(flagIsShowIntro)
startActivityForResult(IntentHelperUtil.getInstance().getIntent(WelcomeActivity.this, IntroductionActivity.class),0);
else
startActivityForResult(IntentHelperUtil.getInstance().getIntent(WelcomeActivity.this, HomeActivity.class),0);
overridePendingTransition(R.anim.fade, R.anim.hold);
}
}
还是不成功!
看来只能使用 Handler 来解决了:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.welcome);
settings = getSharedPreferences(SETTINGS, 0);
flagIsShowIntro=settings.getBoolean(INTRO_SETTING, false);
Timer timer = new Timer();
timer.schedule(tt, 2000);
mHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
{
if(flagIsShowIntro)
startActivityForResult(IntentHelperUtil.getInstance().getIntent(WelcomeActivity.this, IntroductionActivity.class),0);
else
startActivityForResult(IntentHelperUtil.getInstance().getIntent(WelcomeActivity.this, HomeActivity.class),0);
overridePendingTransition(R.anim.fade, R.anim.hold);
break;
}
}
};
};
}
private class MyTask extends TimerTask{
@Override
public void run() {
Message message = new Message();
message.what = 1;
mHandler.sendMessage(message);
}
}
解决了。。。。