前面跟大家聊了拼图的滑动图片和过关判定等功能,接下来给大家聊一聊后期功能的完善
首先,咱们从Menu菜单开始
菜单的XML文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_more"
android:orderInCategory="6"
android:showAsAction="collapseActionView"
android:title="@string/action_more"/>
<item
android:id="@+id/action_show"
android:orderInCategory="1"
android:showAsAction="always"
android:title="@string/action_show"/>
<item
android:id="@+id/action_showimage"
android:orderInCategory="2"
android:showAsAction="always"
android:title="@string/action_showimage"/>
<item
android:id="@+id/action_reset"
android:orderInCategory="9"
android:showAsAction="never"
android:title="@string/action_reset"/>
<item
android:id="@+id/action_set"
android:orderInCategory="11"
android:showAsAction="never"
android:title="@string/action_set"/>
<item
android:id="@+id/action_about"
android:orderInCategory="12"
android:showAsAction="never"
android:title="@string/action_about"/>
<item
android:id="@+id/action_reback"
android:orderInCategory="13"
android:showAsAction="never"
android:title="@string/action_reback"/>
</menu>
获取菜单,将菜单布局到界面
/**
* 获取菜单,将菜单布局到
*/
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
// 获取菜单
getMenuInflater().inflate(R.menu.main, menu);
// 设置预览缩略图片
items = menu.findItem(R.id.action_showimage);
items.setIcon(gameover[10]);
return true;
}
其中ietm是小的预览图
当点击的时候可以跳转到预览界面,并将预览界面需要预览的图片的地址传过去
接下来便是所有菜单的点击事件
/**
* 菜单点击事件
*
*
*/
@SuppressLint("NewApi")
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
// 点击预览缩略图
case R.id.action_showimage:
Intent intent = new Intent(EasyActivity.this, YuLanActivity.class);
intent.putExtra("id", gameover[11]);
startActivity(intent);
break;
// 点击更多图片
case R.id.action_more:
action_more();
break;
// 重置,步数清零,图片随机,分数清零
case R.id.action_reset:
action_reset();
break;
// 背景图片的选择
case R.id.action_set:
action_setShow();
break;
// 游戏介绍
case R.id.action_about:
aboutshow();
break;
// 反馈
case R.id.action_reback:
// 直接跳转到网页
Uri uri = Uri.parse("http://weibo.cn/evanjhon");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
break;
default:
break;
}
return super.onMenuItemSelected(featureId, item);
}
接下来分别简单介绍下各个菜单,首先,预览界面,XML文件中设置的它是always,还有预览两个字,当点击小图片的时候,会跳转到预览界面
接着是更多图片
这个菜单点击之后,会弹出一个列表对话框,显示供选择的各个图片,当点击某一行列表的时候,便会回到游戏界面,但是拼图中需要完成的图片已经改变并且初始化
当然,这些都是在子线程中完成的,还记得之前我们提到过关判定的时候提到的handler当时传走的what=8么,这里也需要继续传what的值
/**
* 点击更多图片
*/
private void action_more() {
// TODO Auto-generated method stub
// 得到AlertDialog.Builder 对象
AlertDialog.Builder builder = new AlertDialog.Builder(EasyActivity.this);
// 设置标题
builder.setTitle("请选择图片");
// 设置图标
builder.setIcon(R.drawable.lufe);
list = new ArrayList<Map<String, Object>>();
int[] imageIds = { R.drawable.baoluo_small, R.drawable.chenyanxi_small,
R.drawable.car_small, R.drawable.light_small,
R.drawable.blank_small, R.drawable.mengbao_small,
R.drawable.hourse_small };
final String[] name = { "保罗", "美女", "豪车", "光线", "静物", "萌宝", "马" };
for (int i = 0; i < imageIds.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("images", imageIds[i]);
map.put("items", name[i]);
list.add(map);
}
// 使用simpleAdapter将布局添加到ListView
SimpleAdapter adapter = new SimpleAdapter(EasyActivity.this, list,
R.layout.select_item, new String[] { "images", "items" },
new int[] { R.id.select_item_iv, R.id.select_item_tv });
// 设置listView的Item的点击事件 自定义内部类
builder.setAdapter(adapter, new OnClickListener() {
/**
* 从帮助类中获取图片的地址 点击不同的item选择不同的图片\]]
*/
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch (which) {
case 0:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number1[i];
number[i] = DataHelper_easy.number1[i];
}
// 利用handler发送标记,更新UI
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 1;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 1:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number2[i];
number[i] = DataHelper_easy.number2[i];
}
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 2;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 2:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number3[i];
number[i] = DataHelper_easy.number3[i];
}
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 3;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 3:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number4[i];
number[i] = DataHelper_easy.number4[i];
}
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 4;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 4:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number5[i];
number[i] = DataHelper_easy.number5[i];
}
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 5;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 5:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number6[i];
number[i] = DataHelper_easy.number6[i];
}
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 6;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 6:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number7[i];
number[i] = DataHelper_easy.number7[i];
}
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 7;// 标记
handler.sendMessage(msg);
};
}.start();
break;
default:
break;
}
}
});
// 将builder创建
builder.create();
// 将builder显示
builder.show();
}
接着就是重置,这里的重置跟重新开始并不一样,这里的充值并不是仅仅的将游戏的界面初始化,还会把sharepreferences里的数据初始化
代码如下:
/**
* 重置
*/
private void action_reset() {
// TODO Auto-generated method stub
// 获取SharePreference
SharedPreferences preferences = getSharedPreferences("info_easy",
Context.MODE_PRIVATE);
// 获取编辑者对象
Editor editor = preferences.edit();
// 将数据置零
editor.putInt("socer_db", 0);
// 提交
editor.commit();
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 9;// 标记
handler.sendMessage(msg);
};
}.start();
}
然后就是更换背景,这里也准备了14张背景图片,
代码如下:
/**
* 背景图片选择
*/
private void action_setShow() {
// TODO Auto-generated method stub
// 得到AlertDialog.Builder 对象
AlertDialog.Builder builder1 = new AlertDialog.Builder(
EasyActivity.this);
// 设置标题
builder1.setTitle("请选择背景");
// 设置图标
builder1.setIcon(R.drawable.lufe);
list1 = new ArrayList<Map<String, Object>>();
// 背景图片的ID
int[] backgroundIds = { R.drawable.background1, R.drawable.background2,
R.drawable.background3, R.drawable.background4,
R.drawable.background5, R.drawable.background6,
R.drawable.background7, R.drawable.background8,
R.drawable.background9, R.drawable.background10,
R.drawable.background11, R.drawable.background12,
R.drawable.background13, R.drawable.background14, };
for (int i = 0; i < backgroundIds.length; i++) {
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("images", backgroundIds[i]);
list1.add(map1);
}
SimpleAdapter adapter1 = new SimpleAdapter(EasyActivity.this, list1,
R.layout.background_item, new String[] { "images" },
new int[] { R.id.background_item_iv });
builder1.setAdapter(adapter1, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch (which) {
case 0:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 10;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 1:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 11;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 2:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 12;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 3:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 13;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 4:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 14;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 5:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 15;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 6:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 16;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 7:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 17;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 8:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 18;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 9:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 19;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 10:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 20;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 11:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 21;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 12:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 22;// 标记
handler.sendMessage(msg);
};
}.start();
break;
case 13:
new Thread() {
public void run() {
Message msg = Message.obtain();
msg.what = 23;// 标记
handler.sendMessage(msg);
};
}.start();
break;
default:
break;
}
}
});
// 将builder1创建
builder1.create();
// 将builder1显示
builder1.show();
}
至于后面的关于,还有反馈,都很简单,不做介绍了
子线程handler根据收到的参数处理事件
代码:
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
int a = msg.what;
switch (a) {
case 1:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number1[i];
number[i] = DataHelper_easy.number1[i];
items.setIcon(gameover[10]);
}
initView();
// initGame();
break;
case 2:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number2[i];
number[i] = DataHelper_easy.number2[i];
items.setIcon(gameover[10]);
}
initView();
// initGame();
break;
case 3:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number3[i];
number[i] = DataHelper_easy.number3[i];
items.setIcon(gameover[10]);
}
initView();
// initGame();
break;
case 4:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number4[i];
number[i] = DataHelper_easy.number4[i];
items.setIcon(gameover[10]);
}
initView();
// initGame();
break;
case 5:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number5[i];
number[i] = DataHelper_easy.number5[i];
items.setIcon(gameover[10]);
}
initView();
// initGame();
break;
case 6:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number6[i];
number[i] = DataHelper_easy.number6[i];
items.setIcon(gameover[10]);
}
initView();
// initGame();
break;
case 7:
for (int i = 0; i < 12; i++) {
gameover[i] = DataHelper_easy.number7[i];
number[i] = DataHelper_easy.number7[i];
items.setIcon(gameover[10]);
}
initView();
// initGame();
break;
case 8:
iv_group[2][2].setImageResource(gameover[9]);
break;
case 9:
initView();
// initGame();
tv1.setText("");
break;
case 10:
ll.setBackgroundResource(R.drawable.background1);
tv1.setTextColor(Color.rgb(50, 50, 50));
tv2.setTextColor(Color.rgb(50, 50, 50));
break;
case 11:
ll.setBackgroundResource(R.drawable.background2);
tv1.setTextColor(Color.rgb(50, 50, 50));
tv2.setTextColor(Color.rgb(50, 50, 50));
break;
case 12:
ll.setBackgroundResource(R.drawable.background3);
tv1.setTextColor(Color.rgb(50, 50, 50));
tv2.setTextColor(Color.rgb(50, 50, 50));
break;
case 13:
ll.setBackgroundResource(R.drawable.background4);
tv1.setTextColor(Color.rgb(50, 50, 50));
tv2.setTextColor(Color.rgb(50, 50, 50));
break;
case 14:
ll.setBackgroundResource(R.drawable.background5);
tv1.setTextColor(Color.rgb(50, 50, 50));
tv2.setTextColor(Color.rgb(50, 50, 50));
break;
case 15:
ll.setBackgroundResource(R.drawable.background6);
tv1.setTextColor(Color.rgb(50, 50, 50));
tv2.setTextColor(Color.rgb(50, 50, 50));
break;
case 16:
ll.setBackgroundResource(R.drawable.background7);
tv1.setTextColor(Color.rgb(50, 50, 50));
tv2.setTextColor(Color.rgb(50, 50, 50));
break;
case 17:
ll.setBackgroundResource(R.drawable.background8);
tv1.setTextColor(Color.rgb(50, 50, 50));
tv2.setTextColor(Color.rgb(50, 50, 50));
break;
case 18:
ll.setBackgroundResource(R.drawable.background9);
tv1.setTextColor(Color.rgb(50, 50, 50));
tv2.setTextColor(Color.rgb(50, 50, 50));
break;
case 19:
ll.setBackgroundResource(R.drawable.background10);
tv1.setTextColor(Color.rgb(50, 50, 50));
tv2.setTextColor(Color.rgb(50, 50, 50));
break;
case 20:
ll.setBackgroundResource(R.drawable.background11);
tv1.setTextColor(Color.rgb(50, 50, 50));
tv2.setTextColor(Color.rgb(50, 50, 50));
break;
case 21:
ll.setBackgroundResource(R.drawable.background12);
tv1.setTextColor(Color.rgb(200, 200, 200));
tv2.setTextColor(Color.rgb(200, 200, 200));
break;
case 22:
ll.setBackgroundResource(R.drawable.background13);
tv1.setTextColor(Color.rgb(50, 50, 50));
tv2.setTextColor(Color.rgb(50, 50, 50));
break;
case 23:
ll.setBackgroundResource(R.drawable.background14);
tv1.setTextColor(Color.rgb(200, 200, 200));
tv2.setTextColor(Color.rgb(200, 200, 200));
break;
case 24:
initView();
break;
default:
break;
}
};
};
还有就是退出本关卡游戏的时候会弹出对话框询问是否退出,使界面更加友好话,还有一些按钮图标的圆角设计,各Activity之间的跳转动画等,这里都不做详细介绍了
好了,简单关卡到这儿就是完全介绍完了,因为三个关卡的思路都差不多,所以这里就不对另外两个关卡进行介绍了,有需要源码的小伙伴们可以留言或者私我,大家一起努力,共同学习