Today, we mainly implement our main interface: international practices:
We have already completed the interface on the first day. Today we will implement the interface function,
This interface provides the following functions:
1. display the difficulty of the game: Use popupwindow and select and change the displayed number.
2. display the default picture to be jigsaw puzzle, including a custom image: This is relatively simple, just the simplest application of the GridView.
3. Custom button function: Call the system atlas and camera
4. view records and learn more: this is also a common application requirement and is relatively simple.
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. colorDra Wable; 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;/*** Main Interface: displays the default image list and the optional image button ** @ author xys **/public class MainActivity extends Activity implements OnClickListener {// return code: system Image Library private static final int RESULT_IMAGE = 100; // return code: Camera private static final int RESULT_CAMERA = 200; // Temp photo path public Static String TEMP_IMAGE_PATH; // image type private static final String IMAGE_TYPE = "image/*"; // display the IMAGE private GridView gv_Pic_List; private List <Bitmap> picList; // homepage image resource ID private int [] resPicId; // display Type private TextView identifier; private LayoutInflater layoutInflater; private PopupWindow popupWindow; private View popupView; private TextView tvType2; private Te XtView tvType3; private TextView tvType4; // game type N * N private int type = 2; // select private String [] customItems = new String [] {"Local album", "Camera photo"} for the local album and camera; @ 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> (); // Initialize ViewsinitViews (); // data adapter gv_Pic_List.setAdapter (new GridPicListAdapter (MainActivity. this, picList); // Item click to listen to gv_Pic_List.setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> Arg0, View view, int position, long arg3) {if (position = resPicId. length-1) {// select the local image library camera showDialogCustom ();} else {// select the default image Intent intent = new Intent (MainActivity. this, PuzzleMain. class); intent. putExtra ("picSelectedID", resPicId [position]); intent. putExtra ("type", type); startActivity (intent) ;}});/*** display difficulty Type */TV _puzzle_main_type_selected.setOnClickListener (new OnClickListener (){ @ Override public void onClick (View v) {// popup windowpopupShow (v) ;}) ;}// display the private void showDialogCustom () {AlertDialog in the select System Image Library camera dialog box. builder builder = new AlertDialog. builder (MainActivity. this); builder. setTitle ("select:"); builder. setItems (customItems, new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {if (0 = which) {// Intent local album = New Intent (Intent. ACTION_PICK, null); intent. setDataAndType (MediaStore. images. media. EXTERNAL_CONTENT_URI, IMAGE_TYPE); startActivityForResult (intent, RESULT_IMAGE);} else if (1 = which) {// System camera 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 ();}/*** call the callback method of the Image Library Camera */@ 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) {// album Cursor cursor = this. getContentResolver (). query (data. getData (), 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) {// camera Intent intent = new Intent (MainActivity. this, PuzzleMain. class); intent. putExtra ("picPath", TEMP_IMAGE_PATH); intent. putExtra ("type", type); startActivity (intent) ;}}/ *** display popup window ** @ param view */private void popupShow (View view) {// display popup windowpopupWindow = new PopupWindow (popupView, 200, 50); popupWindow. setFocusable (true); popupWindow. setOutsideTouchable (true); // transparent background Drawable transpent = new ColorDrawable (Color. TRANSPARENT); popupWindow. setBackgroundDrawable (transpent); // obtain the position int [] location = new int [2]; view. getLocationOnScreen (location); popupWindow. showAtLocation (view, Gravity. NO_GRAVITY, location [0]-40, location [1] + 50);}/*** initialize Views */private void initViews () {gv_Pic_List = (GridView) findViewById (R. id. gv_xpuzzle_main_pic_list); // initialize Bitmap data 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]);} // display typetv_puzzle_main_type_selected = (TextView) findViewById (R. id. TV _puzzle_main_type_selected); layoutInflater = (LayoutInflater) getSystemService (LAYOUT_INFLATER_SERVICE); // type viewpopupView = 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); // listener event tvType2.setOnClickListener (this); tvType3.setOnClickListener (this); tvType4.setOnClickListener (this );} /*** popup window item Click Event */@ Override public void onClick (View v) {switch (v. getId () {// Typecase R. id. TV _main_type_2: type = 2; TV _puzzle_main_type_selected.setText ("2X2"); break; case R. id. TV _main_type_3: type = 3; TV _puzzle_main_type_selected.setText ("3X3"); break; case R. id. TV _main_type_4: type = 4; TV _puzzle_main_type_selected.setText ("4X4"); break; default: break;} popupWindow. dismiss ();}}
The comments are clear. I believe everyone can understand them.
A data adapter is also provided below:
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;/*** main interface data adapter ** @ author xys **/public class GridPicListAdapter extends BaseAdapter {// ing 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); // set the layout image iv_pic_item.setLayoutParams (new GridView. layoutParams (80,100); // sets the display ratio type 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 ;}}
The difficulty today is to call the system atlas and camera:
1. The called Intent has been clearly written in the code.
2. When calling the camera, if data is directly returned, it is actually a thumbnail of the returned camera. To get the original image, you need to first save it to the SD card and then get the image. I hope you will pay attention to it.