实例介绍
【实例简介】
Android开发的2048小游戏
【实例截图】
【核心代码】
package com.uberspot.a2048;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.widget.Toast;
import java.util.Locale;
import de.cketti.changelog.dialog.DialogChangeLog;
public class MainActivity extends Activity {
private static final String MAIN_ACTIVITY_TAG = "2048_MainActivity";
private WebView mWebView;
private long mLastBackPress;
private static final long mBackPressThreshold = 3500;
private static final String IS_FULLSCREEN_PREF = "is_fullscreen_pref";
private long mLastTouch;
private static final long mTouchThreshold = 2000;
private Toast pressBackToast;
@SuppressLint({"SetJavaScriptEnabled", "ShowToast", "ClickableViewAccessibility"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Don't show an action bar or title
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Enable hardware acceleration
getWindow().setFlags(LayoutParams.FLAG_HARDWARE_ACCELERATED,
LayoutParams.FLAG_HARDWARE_ACCELERATED);
// Apply previous setting about showing status bar or not
applyFullScreen(isFullScreen());
// Check if screen rotation is locked in settings
boolean isOrientationEnabled = false;
try {
isOrientationEnabled = Settings.System.getInt(getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION) == 1;
} catch (SettingNotFoundException e) {
Log.d(MAIN_ACTIVITY_TAG, "Settings could not be loaded");
}
// If rotation isn't locked and it's a LARGE screen then add orientation changes based on sensor
int screenLayout = getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK;
if (((screenLayout == Configuration.SCREENLAYOUT_SIZE_LARGE)
|| (screenLayout == Configuration.SCREENLAYOUT_SIZE_XLARGE))
&& isOrientationEnabled) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
setContentView(R.layout.activity_main);
DialogChangeLog changeLog = DialogChangeLog.newInstance(this);
if (changeLog.isFirstRun()) {
changeLog.getLogDialog().show();
}
// Load webview with game
mWebView = findViewById(R.id.mainWebView);
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setRenderPriority(RenderPriority.HIGH);
settings.setDatabasePath(getFilesDir().getParentFile().getPath() "/databases");
// If there is a previous instance restore it in the webview
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
} else {
// Load webview with current Locale language
mWebView.loadUrl("file:///android_asset/2048/index.html?lang=" Locale.getDefault().getLanguage());
}
Toast.makeText(getApplication(), R.string.toggle_fullscreen, Toast.LENGTH_SHORT).show();
// Set fullscreen toggle on webview LongClick
mWebView.setOnTouchListener((v, event) -> {
// Implement a long touch action by comparing
// time between action up and action down
long currentTime = System.currentTimeMillis();
if ((event.getAction() == MotionEvent.ACTION_UP)
&& (Math.abs(currentTime - mLastTouch) > mTouchThreshold)) {
boolean toggledFullScreen = !isFullScreen();
saveFullScreen(toggledFullScreen);
applyFullScreen(toggledFullScreen);
} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
mLastTouch = currentTime;
}
// return so that the event isn't consumed but used
// by the webview as well
return false;
});
pressBackToast = Toast.makeText(getApplicationContext(), R.string.press_back_again_to_exit,
Toast.LENGTH_SHORT);
}
@Override
protected void onResume() {
super.onResume();
mWebView.loadUrl("file:///android_asset/2048/index.html?lang=" Locale.getDefault().getLanguage());
}
@Override
protected void onSaveInstanceState(Bundle outState) {
mWebView.saveState(outState);
}
/**
* Saves the full screen setting in the SharedPreferences
*
* @param isFullScreen boolean value
*/
private void saveFullScreen(boolean isFullScreen) {
// save in preferences
Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putBoolean(IS_FULLSCREEN_PREF, isFullScreen);
editor.apply();
}
private boolean isFullScreen() {
return PreferenceManager.getDefaultSharedPreferences(this).getBoolean(IS_FULLSCREEN_PREF,
true);
}
/**
* Toggles the activity's fullscreen mode by setting the corresponding window flag
*
* @param isFullScreen boolean value
*/
private void applyFullScreen(boolean isFullScreen) {
if (isFullScreen) {
getWindow().clearFlags(LayoutParams.FLAG_FULLSCREEN);
} else {
getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN,
LayoutParams.FLAG_FULLSCREEN);
}
}
/**
* Prevents app from closing on pressing back button accidentally.
* mBackPressThreshold specifies the maximum delay (ms) between two consecutive backpress to
* quit the app.
*/
@Override
public void onBackPressed() {
long currentTime = System.currentTimeMillis();
if (Math.abs(currentTime - mLastBackPress) > mBackPressThreshold) {
pressBackToast.show();
mLastBackPress = currentTime;
} else {
pressBackToast.cancel();
super.onBackPressed();
}
}
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论