主界面的编写(Login.java)
login.xml
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/menubg"
android:orientation="vertical" >
<Button
android:id="@ id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@ id/option"
android:layout_marginBottom="30dp"
android:background="@null"
android:text="@string/start"
android:textColor="#FF0000" />
<Button
android:id="@ id/option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@null"
android:text="@string/option"
android:textColor="#FF0000" />
<Button
android:id="@ id/rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@ id/option"
android:layout_marginTop="30dp"
android:background="@null"
android:text="@string/rank"
android:textColor="#FF0000" />
<ProgressBar
android:id="@ id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminateDrawable="@drawable/progressbar" />
<TextView
android:id="@ id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@ id/progressBar1"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="@string/hint"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF0000" />
</RelativeLayout>
注意,ProgressBar,TextView在一开始要设置为隐藏
Login.java
代码语言:javascript复制package com.example.gamedemo;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Login extends Activity {
private SharedPreferences sharedPreferences;
private Button btnStart, btnOption, rankBtn;
private TextView tv;
private ProgressBar pBar;
private int progress;
private MediaPlayer mp;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.login);
try {
AssetFileDescriptor afd = getAssets().openFd("menubg.mp3");
mp = new MediaPlayer();
mp.setLooping(true);
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getLength());
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
tv = (TextView) findViewById(R.id.textView1);
pBar = (ProgressBar) findViewById(R.id.progressBar1);
pBar.setVisibility(View.INVISIBLE);
tv.setVisibility(View.INVISIBLE);
btnStart = (Button) findViewById(R.id.start);
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pBar.setVisibility(View.VISIBLE);
tv.setVisibility(View.VISIBLE);
btnStart.setVisibility(View.INVISIBLE);
btnOption.setVisibility(View.INVISIBLE);
rankBtn.setVisibility(View.INVISIBLE);
progress = 0;
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
Message m = handler.obtainMessage();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (progress < 100) {
progress;
m.what = 0x11;
handler.sendMessage(m);
} else {
m.what = 0x12;
handler.sendMessage(m);
break;
}
}
}
}).start();
}
});
btnOption = (Button) findViewById(R.id.option);
btnOption.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(Login.this,
OptionActivity.class), 1);
}
});
rankBtn = (Button) findViewById(R.id.rank);
rankBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(Login.this,
RankListActivity.class), 2);
}
});
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x11) {
tv.setText("正在加载:" progress "%");
pBar.setProgress(progress);
} else if (msg.what == 0x12) {
tv.setText("加载完毕!正在进入游戏......");
pBar.setVisibility(View.INVISIBLE);
startActivityForResult(new Intent(Login.this,
MainActivity.class), 2);
}
}
};
// 生成默认设置
DefaultOption();
}
/**
* 生成默认设置
*/
private void DefaultOption() {
sharedPreferences = getSharedPreferences("option",
Activity.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString("bgMusic", "开");
editor.putString("gameMusic", "开");
editor.putString("planeModel", "机型一");
editor.apply();
}
@Override
protected void onStart() {
super.onStart();
sharedPreferences = getSharedPreferences("option",
Activity.MODE_PRIVATE);
String bgMusic = sharedPreferences.getString("bgMusic", "开");
if (bgMusic.equals("关")) {
isPause = false;
} else if (bgMusic.equals("开")) {
isPause = true;
}
}
private boolean isPause = false;
@Override
protected void onPause() {
super.onPause();
if (mp.isPlaying())
mp.pause();
isPause = true;
}
@Override
protected void onResume() {
super.onResume();
if (isPause) {
mp.start();
isPause = false;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mp != null) {
mp.stop();
mp.reset();
mp.release();
mp = null;
}
};
/**
* 先onActivityResult,后onResume
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 1) {
sharedPreferences = getSharedPreferences(
"option", Activity.MODE_PRIVATE);
String bgMusic = sharedPreferences.getString("bgMusic", "开");
if (bgMusic.equals("关")) {
isPause = false;
} else if (bgMusic.equals("开")) {
isPause = true;
}
} else if (resultCode == RESULT_OK && requestCode == 2) {
btnStart.setVisibility(View.VISIBLE);
btnOption.setVisibility(View.VISIBLE);
rankBtn.setVisibility(View.VISIBLE);
tv.setVisibility(View.INVISIBLE);
pBar.setVisibility(View.INVISIBLE);
}
}
}
构造函数里面默认menubg是正在播放的。 然后设置ProgressBar,TextView为隐藏 然后设置几个button的click事件 组后设置默认的设置。通过sharedPreferences
btnStart响应事件中,新建一个线程来控制progressbar。当progress不满100时,一直加一。当等于100时startActivityForResult。在最后的onActivityResult中requestCode == 2设置组件的是否可视
btnOption进入OptionActivity。requestCode == 1来控制bgmusic的开关(本界面播放的是menubg.mp3,只有在正式的游戏界面才播放gamebg.mp3)
rankBtn进入RankListActivity
当你从别的activity切回Login的话,会调用onStart。所以可以在onStart函数里面重新读取设置来确定是否要播放音乐。
这个界面不算难。就用到了sharedPreferences和onActivityResult和Activity的生命周期。 至于onActivityResult和Activity的生命周期被调用的先后顺序,自己可以Log查看。