Android jigsaw puzzle game development full record 5

Source: Internet
Author: User

Today, we can finally end this project. We have prepared for it a few days ago. I believe that the last day is easy. International Practice:


The last thing we need to accomplish is our main function-the jigsaw puzzle interface.

The layout is relatively simple and has been completed a few days ago. Now we have to do the following:

1. Timing steps: this is a basic function of the game. In fact, it is relatively simple. Just record the number of successfully moved steps and display a timer.

2. Adjust the image size to a proper proportion: This method has been implemented a few days ago.

3. Click the GridView icon and move the image. The method for moving the image has been implemented a few days ago.

4. Determine whether the puzzle is completed: Alas, it has already been implemented.

5. Click "show source image". Only one ImageView is displayed.

6. Click "reset" to reprocess the uploaded image.

After writing the basic idea, it seems very complicated. It is very easy to sort it out. A lot of preparation work is done, which will make our thinking clearer.


Package com. xys. xpuzzle. activity; import java. io. file; import java. util. arrayList; import java. util. list; import java. util. timer; import java. util. timerTask; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onClickListener; import Droid. view. animation. animation; import android. view. animation. animationUtils; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. button; import android. widget. gridView; import android. widget. imageView; import android. widget. relativeLayout; import android. widget. relativeLayout. layoutParams; import android. widget. textView; import android. widget. toas T; import com. xys. xpuzzle. r; import com. xys. xpuzzle. adapter. gridItemsAdapter; import com. xys. xpuzzle. bean. itemBean; import com. xys. xpuzzle. util. gameUtil; import com. xys. xpuzzle. util. imagesUtil; import com. xys. xpuzzle. util. screenUtil;/*** main interface of the puzzle logic: the panel displays ** @ author xys **/public class PuzzleMain extends Activity implements OnClickListener {// private Bitmap picSelected; // public st of the last image displayed when the puzzle is complete Atic Bitmap lastBitmap; // PuzzlePanel private GridView gv_puzzle_main_detail; private int resId; private String picPath; private ImageView imageView; // Button private Button btnBack; private Button btnImage; private Button btnRestart; // display the number of steps private TextView TV _puzzle_main_counts; // timer private TextView TV _Timer; // private List of images after cropping <Bitmap> bitmapItemLists = new ArrayList <Bitmap> (); // Grid View adapter private GridItemsAdapter adapter; // set to N * N to show public static int type = 2; // Flag to show whether the source image private boolean isShowImg is displayed; // public static int countIndex = 0 for the number of steps; // public static int timerIndex = 0 for the Timer type; // private timer Timer for the timer type; /*** UI update Handler */private Handler handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {switch (msg. what) {case 1: // update the timer timerIndex ++; TV _ Timer. setText ("" + timerIndex); break; default: break ;}};/*** timer thread */private TimerTask timerTask; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. xpuzzle_puzzle_detail_main); // obtain the selected image Bitmap picSelectedTemp; // select the default image or custom image resId = getIntent (). getExtras (). getInt ("picSelectedID"); picPath = getIntent (). getExtras (). getStrin G ("picPath"); if (resId! = 0) {picSelectedTemp = BitmapFactory. decodeResource (getResources (), resId);} else {picSelectedTemp = BitmapFactory. decodeFile (picPath);} type = getIntent (). getExtras (). getInt ("type", 2); // handlerImage for image processing (picSelectedTemp); // initialize ViewsinitViews (); // generate game data generateGame (); // GridView Click Event gv_puzzle_main_detail.setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (Adapte RView <?> Arg0, View view, int position, long arg3) {// you can determine whether to move if (GameUtil. isMoveable (position) {// switch the position where the Item and space are clicked. swapItems (GameUtil. itemBeans. get (position), GameUtil. blankItemBean); // obtain the image recreateData () again; // notify the GridView to change the UI adapter. notifyDataSetChanged (); // The number of update steps countIndex ++; TV _puzzle_main_counts.setText ("" + countIndex); // you can determine if (GameUtil. isSuccess () {// display the complete recreateData (); bit in the last graph MapItemLists. remove (type * type-1); bitmapItemLists. add (lastBitmap); // notifies the GridView to change the UIadapter. notifyDataSetChanged (); Toast. makeText (PuzzleMain. this, "the puzzle is successful! ", Toast. LENGTH_LONG ). show (); gv_puzzle_main_detail.setEnabled (false); timer. cancel (); timerTask. cancel () ;}}}); // click the event btnBack button to return. setOnClickListener (this); // display the source image button and click event btnImage. setOnClickListener (this); // reset the button and click event btnRestart. setOnClickListener (this);}/*** Button click event */@ Override public void onClick (View v) {switch (v. getId () {// return button click event case R. id. btn_puzzle_main_back: PuzzleMain. this. finish (); B Reak; // display the source image button and click event case R. id. btn_puzzle_main_img: Animation animShow = AnimationUtils. loadAnimation (PuzzleMain. this, R. anim. image_show_anim); Animation animHide = AnimationUtils. loadAnimation (PuzzleMain. this, R. anim. image_hide_anim); if (isShowImg) {imageView. startAnimation (animHide); imageView. setVisibility (View. GONE); isShowImg = false;} else {imageView. startAnimation (animShow); imageView. setVisibi Lity (View. VISIBLE); isShowImg = true;} break; // click the reset button to event case R. id. btn_puzzle_main_restart: cleanConfig (); generateGame (); recreateData (); // notifies the GridView to change UI TV _puzzle_main_counts.setText ("" + countIndex); adapter. notifyDataSetChanged (); gv_puzzle_main_detail.setEnabled (true); break; default: break ;}/ *** generate game data */private void generateGame () {// obtain the normal sequence of the initial tile data by cropping the graph. new ImagesUtil (). createInitBitmaps (Type, picSelected, PuzzleMain. this); // generate random data GameUtil. getPuzzleGenerator (); // gets the Bitmap set for (ItemBean temp: GameUtil. itemBeans) {bitmapItemLists. add (temp. getBitmap ();} // data adapter = new GridItemsAdapter (this, bitmapItemLists); gv_puzzle_main_detail.setAdapter (adapter); // enable timer Timer = new timer (true ); // timer thread timerTask = new TimerTask () {@ Override public void run () {Message msg = new Message (); Msg. what = 1; handler. sendMessage (msg) ;}}; // The Execution Delay of 0stimer per Ms. schedule (timerTask, 0, 1000);}/*** add View */private void addImgView () {RelativeLayout relativeLayout = (RelativeLayout) findViewById (R. id. rl_puzzle_main_main_layout); imageView = new ImageView (PuzzleMain. this); imageView. setImageBitmap (picSelected); int x = (int) (picSelected. getWidth () * 0.9F); int y = (int) (picSelected. getHe Ight () * 0.9F); LayoutParams params = new LayoutParams (x, y); params. addRule (RelativeLayout. CENTER_IN_PARENT); imageView. setLayoutParams (params); relativeLayout. addView (imageView); imageView. setVisibility (View. GONE);}/*** call when returning */@ Override protected void onStop () {super. onStop (); // clear related parameter settings cleanConfig (); this. finish ();}/*** clear related parameter settings */private void cleanConfig () {// clear related parameter settings GameUtil. itemBeans. cle Ar (); // stop timer. cancel (); timerTask. cancel (); countIndex = 0; timerIndex = 0; // clear the photo if (picPath! = Null) {// Delete photo File file = new File (MainActivity. TEMP_IMAGE_PATH); if (file. exists () {file. delete () ;}}/*** retrieve image again */private void recreateData () {bitmapItemLists. clear (); for (ItemBean temp: GameUtil. itemBeans) {bitmapItemLists. add (temp. getBitmap () ;}}/*** Adaptive Image Processing size ** @ param bitmap */private void handlerImage (Bitmap bitmap) {// enlarge the image to a fixed int screenWidth = ScreenUtil. getScreenSize (this ). widthPixels; int screenheimpaired = ScreenUtil. getScreenSize (this ). heightPixels; picSelected = new ImagesUtil (). resizeBitmap (screenWidth * 0.8f, screenheimpaired * 0.6f, bitmap);}/*** initialize Views */private void initViews () {// ButtonbtnBack = (Button) findViewById (R. id. btn_puzzle_main_back); btnImage = (Button) findViewById (R. id. btn_puzzle_main_img); btnRestart = (Button) findViewById (R. id. btn_puzzle_main_restart); // whether the source image isShowImg = false is displayed in the Flag; // GVgv_puzzle_main_detail = (GridView) findViewById (R. id. gv_puzzle_main_detail); // set it to N * N to display gv_puzzle_main_detail.setNumColumns (type); LayoutParams gridParams = new RelativeLayout. layoutParams (picSelected. getWidth (), picSelected. getHeight (); // horizontally centered gridParams. addRule (RelativeLayout. CENTER_HORIZONTAL); // other format attributes gridParams. addRule (RelativeLayout. BELOW, R. id. ll_puzzle_main_spinner); // Grid display metrics (gridParams); counts (0); counts (0); // TV step count TV _puzzle_main_counts = (TextView) findViewById (R. id. TV _puzzle_main_counts); TV _puzzle_main_counts.setText ("" + countIndex); // TV timer TV _Timer = (TextView) findViewById (R. id. TV _puzzle_main_time); TV _Timer.setText ("0 seconds"); // Add ViewaddImgView ();}}

I think the annotation and code style are good. I hope you will like it.

PS: If you need the source code, please leave a message. In fact, you can implement it by yourself based on this idea, which greatly improves yourself. If you can optimize my code, it will be better, you can learn and communicate with each other.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.