我正在使用JavaScript编写一款游戏。在我的浏览器上,这个应用程序运行良好(速度快),但是我在Android WebView上运行时遇到了一些问题。
- The time to start the app takes 5s or more (I think that is kind of slow, but maybe that is normal?)
In the menu of the game I have a method like:
this.showCredits = function() { document.getElementById('core-credits-layer').style.display = 'block'; document.getElementById('core-credits').style.display = 'block'; var parent = this; $.ajax({ url: 'content/credits.html', dataType: 'html', success: function(data, status, response) { var now = new Date(); var s = now.getSeconds()-parent.test.getSeconds(); console.log('success ajax: '+s); document.getElementById('core-credits').scrollTop = 0; document.getElementById('core-credits').innerHTML = response.responseText; console.log('finished'); }, error: function() { console.error('failed fetch credits'); } }); }
So the console log ("finished", last line in success()), comes immediately after clicking the menu "credits". But it can takes 6s (more or less) until I see the the div #core-credits. In my browser, I see #core-credits immediately after clicking. But the 2nd time clicking on that menu point I get the div after 1-2s. I don't now what that is, I don't think so, that is a caching thing, because I get into the success() callback really fast.
Java端:
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Context context = this.getApplicationContext();
SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
if(isFirstRun) {
this.copyAudioFiles(context);
}
WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
String data = context.getFilesDir().getPath();
mWebView.getSettings().setDatabasePath(data+"data/"+this.getPackageName()+"/databases/");
mWebView.getSettings().setAllowFileAccess(true);
mWebView.setWebChromeClient(new WebChromeClient());
String data_root = Environment.getExternalStorageDirectory()+"/"
+this.getPackageName();
mWebView.loadUrl("file:///android_asset/www/index.html?system=android&" +
"data_root="+data_root);
}
我在eclipse虚拟平板设备和真实的华硕平板上遇到了性能问题。同时,动画效果(如jquery show('slow'))有时会非常缓慢或出现故障。例如(这是一款卡牌游戏),玩家通过show('slow')动画获得9张牌,但每第二次或第三次我只能获得3张或8张牌,而不是9张^^。所有这些问题在像Chromium或Firefox这样的浏览器中运行良好。我正在使用Android 4.1。启用硬件加速也没有帮助。有关此渲染问题的各种帖子都提到了硬件加速解决方案,但这并没有起到帮助作用。