大概流程是这样的,首先一个游戏启动的逐帧动画,动画结束后,慢慢的出现“点击屏幕继续”的提示,然后点击屏幕后进入游戏主界面。
进入主界面后,总是出现一个问题。就是在一打开主界面的Activity后,会突然闪一下刚才开机动画的背景。每次都是这样,不是偶然。
我后来加了两个Log一个在开机动画跳转代码结束的时候,取名为loading,一个在主界面onCreate函数末尾,取名triangle。
打印的日志信息如下:
07-11 03:15:26.169: I/ActivityManager(68): Starting: Intent { cmp=com.asus.athz.squares/.TriangleBlockActivity } from pid 491
07-11 03:15:26.169: W/WindowManager(68): Failure taking screenshot for (216x135) to layer 21010
07-11 03:15:26.189: D/loding(491): finish
...
07-11 03:15:26.900: D/triangle(491): finish
07-11 03:15:26.969: I/ActivityManager(68): Starting: Intent { cmp=com.asus.athz.squares/.TriangleBlockActivity } from pid 491
07-11 03:15:26.969: W/ActivityManager(68): Duplicate finish request for ActivityRecord{4078e820 com.asus.athz.squares/.Loading}
07-11 03:15:26.979: D/loding(491): finish
07-11 03:15:27.099: W/InputManagerService(68): Starting input on non-focused client [email protected] (uid=10037 pid=491)
07-11 03:15:27.239: V/TLINE(491): new: [email protected]
07-11 03:15:27.360: D/triangle(491): finish
07-11 03:15:27.560: I/ActivityManager(68): Displayed com.asus.athz.squares/.TriangleBlockActivity: +1s363ms
...
从日志能看出在主界面初始化完毕后,开机动画的那个loading Activity又出现了一次。不知是什么原因很是郁闷。我跳转代码部分是这样写的:
- Java code
public boolean onTouchEvent(MotionEvent event) { Intent intent = new Intent(); intent.setClass(Loading.this, TriangleBlockActivity.class); startActivity(intent); finish(); Log.d("loading","finish"); return true; }
------解决方案--------------------
首先,onTouchEvent会触发多次,当用手指触屏时,我们理想中以为点一次就只会有一个onTouch事件,实际上会有多个,例如Down、Up、Move,所以,应该将这段代码放到这里面
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
Intent intent = new Intent();
intent.setClass(Loading.this, TriangleBlockActivity.class);
startActivity(intent);
finish();
Log.d("loading","finish");
}
return true;
上面的方法也不是非常保险,最好加一个全局对象变量,例如
private boolean hasFinish = false;
然后
if(event.getAction() == MotionEvent.ACTION_DOWN && hasFinish == false)
{
hasFinish = true;
Intent intent = new Intent();
intent.setClass(Loading.this, TriangleBlockActivity.class);
startActivity(intent);
finish();
Log.d("loading","finish");
}