您好我正在使用android.I已经在我的应用程序中从左到右成功使用代码创建了一个覆盖暂挂转换动画
Intent in=new Intent(getApplicationContext(),MainActivity.class);
startActivity(in);
overridePendingTransition(R.anim.left_in, R.anim.left_out);
现在我希望在一段时间后执行此过程,以便我使用Timer函数,如下所示
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
Intent in=new Intent(getApplicationContext(),MainActivity.class);
startActivity(in);
overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
}, 10000);
但在这种情况下,活动会出现,但动画不起作用。它在第一种情况下工作。我不知道为什么会这样?请帮助我提前谢谢:)
答案 0 :(得分:3)
在ui线程中执行
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent in = new Intent(getApplicationContext(), MainActivity.class);
startActivity(in);
overridePendingTransition(R.anim.left_in, R.anim.left_out);
timer.cancel();
}
});
}
}, 10000);
或尝试此
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent in = new Intent(getApplicationContext(), MainActivity.class);
startActivity(in);
overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
}, 10000);
答案 1 :(得分:0)
您尝试做的事情需要在UI /主要线程上运行。实施TimerTask
的{{1}}将尝试从其自己的主题发送您的意图,这就是为什么(我怀疑)您无法从Runnable
内部开始您的活动{1}}。您应该能够使用runOnUiThread方法使用从活动开始的TimerTask
来解决此问题。
Runnable
如果您需要多次运行或延迟运行,您可以使用Runnable runStuff = new Runnable(){
public void run(){
//your code here
}
};
runOnUiThread(runStuff);
和postDelayed在一段固定时间后运行Handler
。
答案 2 :(得分:0)
确保您已在开发人员菜单中启用了“显示动画”选项。否则你可能看不到它们。