今天我們主要實(shí)現(xiàn)我們的主界面:國際慣例:
界面我們已經(jīng)在第一天做好了,今天我們就要實(shí)現(xiàn)這個界面的功能,
分析一下,這個界面包含以下幾個功能:
1、顯示游戲的難度:使用popupwindow,選擇后改變顯示的數(shù)字
2、顯示默認(rèn)的待拼圖圖片,包含一張選擇自定義的圖片:這個比較簡單,只是GridView的最簡單應(yīng)用而已
3、自定義按鈕功能:調(diào)用系統(tǒng)圖冊和相機(jī)
4、查看記錄和了解更多:這個也是一般的應(yīng)用需求,比較簡單
- package com.xys.xpuzzle.activity;
-
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.graphics.drawable.ColorDrawable;
- import android.graphics.drawable.Drawable;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.GridView;
- import android.widget.PopupWindow;
- import android.widget.TextView;
- import com.xys.xpuzzle.R;
- import com.xys.xpuzzle.adapter.GridPicListAdapter;
-
- /**
- * 程序主界面:顯示默認(rèn)圖片列表、自選圖片按鈕
- *
- * @author xys
- *
- */
- public class MainActivity extends Activity implements OnClickListener {
-
- // 返回碼:系統(tǒng)圖庫
- private static final int RESULT_IMAGE = 100;
- // 返回碼:相機(jī)
- private static final int RESULT_CAMERA = 200;
- // Temp照片路徑
- public static String TEMP_IMAGE_PATH;
- // IMAGE TYPE
- private static final String IMAGE_TYPE = "image/*";
- // GridView 顯示圖片
- private GridView gv_Pic_List;
- private List<Bitmap> picList;
- // 主頁圖片資源ID
- private int[] resPicId;
- // 顯示Type
- private TextView tv_puzzle_main_type_selected;
- private LayoutInflater layoutInflater;
- private PopupWindow popupWindow;
- private View popupView;
- private TextView tvType2;
- private TextView tvType3;
- private TextView tvType4;
- // 游戲類型N*N
- private int type = 2;
- // 本地圖冊、相機(jī)選擇
- private String[] customItems = new String[] { "本地圖冊", "相機(jī)拍照" };
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.xpuzzle_main);
-
- TEMP_IMAGE_PATH = Environment.getExternalStorageDirectory().getPath() + "/ttt.jpg";
- picList = new ArrayList<Bitmap>();
- // 初始化Views
- initViews();
- // 數(shù)據(jù)適配器
- gv_Pic_List.setAdapter(new GridPicListAdapter(MainActivity.this, picList));
- // Item點(diǎn)擊監(jiān)聽
- gv_Pic_List.setOnItemClickListener(new OnItemClickListener() {
-
- @Override
- public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
- if (position == resPicId.length - 1) {
- // 選擇本地圖庫 相機(jī)
- showDialogCustom();
- } else {
- // 選擇默認(rèn)圖片
- Intent intent = new Intent(MainActivity.this, PuzzleMain.class);
- intent.putExtra("picSelectedID", resPicId[position]);
- intent.putExtra("type", type);
- startActivity(intent);
- }
- }
- });
-
- /**
- * 顯示難度Type
- */
- tv_puzzle_main_type_selected.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // 彈出popup window
- popupShow(v);
- }
- });
- }
-
- // 顯示選擇系統(tǒng)圖庫 相機(jī)對話框
- private void showDialogCustom() {
- AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
- builder.setTitle("選擇:");
- builder.setItems(customItems, new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- if (0 == which) {
- // 本地相冊
- Intent intent = new Intent(Intent.ACTION_PICK, null);
- intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_TYPE);
- startActivityForResult(intent, RESULT_IMAGE);
- } else if (1 == which) {
- // 系統(tǒng)相機(jī)
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- Uri photoUri = Uri.fromFile(new File(TEMP_IMAGE_PATH));
- intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
- startActivityForResult(intent, RESULT_CAMERA);
- }
- }
- });
- builder.create().show();
- }
-
- /**
- * 調(diào)用圖庫相機(jī)回調(diào)方法
- */
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (resultCode == RESULT_OK) {
- if (requestCode == RESULT_IMAGE && data != null) {
- // 相冊
- Cursor cursor = this.getContentResolver().query(data.getData(), null, null, null, null);
- cursor.moveToFirst();
- String imagePath = cursor.getString(cursor.getColumnIndex("_data"));
- Intent intent = new Intent(MainActivity.this, PuzzleMain.class);
- intent.putExtra("picPath", imagePath);
- intent.putExtra("type", type);
- startActivity(intent);
- } else if (requestCode == RESULT_CAMERA) {
- // 相機(jī)
- Intent intent = new Intent(MainActivity.this, PuzzleMain.class);
- intent.putExtra("picPath", TEMP_IMAGE_PATH);
- intent.putExtra("type", type);
- startActivity(intent);
- }
- }
- }
-
- /**
- * 顯示popup window
- *
- * @param view
- */
- private void popupShow(View view) {
- // 顯示popup window
- popupWindow = new PopupWindow(popupView, 200, 50);
- popupWindow.setFocusable(true);
- popupWindow.setOutsideTouchable(true);
- // 透明背景
- Drawable transpent = new ColorDrawable(Color.TRANSPARENT);
- popupWindow.setBackgroundDrawable(transpent);
- // 獲取位置
- int[] location = new int[2];
- view.getLocationOnScreen(location);
- popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0] - 40, location[1] + 50);
- }
-
- /**
- * 初始化Views
- */
- private void initViews() {
- gv_Pic_List = (GridView) findViewById(R.id.gv_xpuzzle_main_pic_list);
- // 初始化Bitmap數(shù)據(jù)
- resPicId = new int[] { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7,
- R.drawable.pic8, R.drawable.pic9, R.drawable.pic10, R.drawable.pic11, R.drawable.pic12, R.drawable.pic13, R.drawable.pic14,
- R.drawable.pic15, R.drawable.plus };
- Bitmap[] bitmaps = new Bitmap[resPicId.length];
- for (int i = 0; i < bitmaps.length; i++) {
- bitmaps[i] = BitmapFactory.decodeResource(getResources(), resPicId[i]);
- picList.add(bitmaps[i]);
- }
- // 顯示type
- tv_puzzle_main_type_selected = (TextView) findViewById(R.id.tv_puzzle_main_type_selected);
- layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
- // type view
- popupView = layoutInflater.inflate(R.layout.xpuzzle_main_type_selected, null);
- tvType2 = (TextView) popupView.findViewById(R.id.tv_main_type_2);
- tvType3 = (TextView) popupView.findViewById(R.id.tv_main_type_3);
- tvType4 = (TextView) popupView.findViewById(R.id.tv_main_type_4);
-
- // 監(jiān)聽事件
- tvType2.setOnClickListener(this);
- tvType3.setOnClickListener(this);
- tvType4.setOnClickListener(this);
- }
-
- /**
- * popup window item點(diǎn)擊事件
- */
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- // Type
- case R.id.tv_main_type_2:
- type = 2;
- tv_puzzle_main_type_selected.setText("2 X 2");
- break;
- case R.id.tv_main_type_3:
- type = 3;
- tv_puzzle_main_type_selected.setText("3 X 3");
- break;
- case R.id.tv_main_type_4:
- type = 4;
- tv_puzzle_main_type_selected.setText("4 X 4");
- break;
- default:
- break;
- }
- popupWindow.dismiss();
- }
- }
注釋還是比較清晰的,相信大家都看得懂。
下面還有送一個數(shù)據(jù)適配器:
- package com.xys.xpuzzle.adapter;
-
- import java.util.List;
-
- import android.R.color;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
-
- /**
- * 程序主界面數(shù)據(jù)適配器
- *
- * @author xys
- *
- */
- public class GridPicListAdapter extends BaseAdapter {
-
- // 映射List
- private List<Bitmap> picList;
- private Context context;
-
- public GridPicListAdapter(Context context, List<Bitmap> picList) {
- this.context = context;
- this.picList = picList;
- }
-
- @Override
- public int getCount() {
- return picList.size();
- }
-
- @Override
- public Object getItem(int position) {
- return picList.get(position);
- }
-
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup arg2) {
- ImageView iv_pic_item = null;
- if (convertView == null) {
- iv_pic_item = new ImageView(context);
- // 設(shè)置布局 圖片
- iv_pic_item.setLayoutParams(new GridView.LayoutParams(80, 100));
- // 設(shè)置顯示比例類型
- iv_pic_item.setScaleType(ImageView.ScaleType.FIT_XY);
- } else {
- iv_pic_item = (ImageView) convertView;
- }
- iv_pic_item.setBackgroundColor(color.black);
- iv_pic_item.setImageBitmap(picList.get(position));
- return iv_pic_item;
- }
- }
今天的難點(diǎn)在于調(diào)用系統(tǒng)圖冊和相機(jī):
1、調(diào)用的Intent在代碼中已經(jīng)寫清楚了
2、調(diào)用相機(jī)的時候,如果直接返回data,其實(shí)是返回的相機(jī)縮略圖,如果要獲得原始圖片,就需要先保存在SD卡上,再去獲取這張圖片,希望大家注意。
|