乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Android拼圖游戲開發(fā)全紀(jì)錄5

       水與火604 2016-03-13

      今天我們終于可以把這個(gè)項(xiàng)目給結(jié)束掉啦,有了前幾天的準(zhǔn)備,相信最后一天還是比較輕松的,國際慣例:


      最后要完成的就是我們的主要功能--拼圖界面。

      布局比較簡單,在前幾天就已經(jīng)做好了,現(xiàn)在我們要做的是以下幾件事情:

      1、計(jì)時(shí)記步:這個(gè)是游戲基本都有的功能,其實(shí)也比較簡單,記錄成功移動的步數(shù)、顯示一個(gè)計(jì)時(shí)器就行了。

      2、處理圖片,調(diào)整為合適的大小比例:這個(gè)方法在前幾天已經(jīng)實(shí)現(xiàn)了

      3、點(diǎn)擊GridView后移動圖片:是否能移動的方法已經(jīng)在前幾天實(shí)現(xiàn)了

      4、判斷是否拼圖完成:唉,也已經(jīng)實(shí)現(xiàn)了

      5、點(diǎn)擊原圖按鈕:顯示原圖:只要顯示一個(gè)ImageView就可以了

      6、點(diǎn)擊重置按鈕:將傳進(jìn)來的這張圖片重新處理一遍流程

      基本思路寫完了,看來很復(fù)雜的事情,是不是梳理下就覺得很簡單了,很多準(zhǔn)備工作做好了,會讓我們的思路變的更清晰。


      1. package com.xys.xpuzzle.activity;  
      2.   
      3. import java.io.File;  
      4. import java.util.ArrayList;  
      5. import java.util.List;  
      6. import java.util.Timer;  
      7. import java.util.TimerTask;  
      8.   
      9. import android.app.Activity;  
      10. import android.graphics.Bitmap;  
      11. import android.graphics.BitmapFactory;  
      12. import android.os.Bundle;  
      13. import android.os.Handler;  
      14. import android.os.Message;  
      15. import android.view.View;  
      16. import android.view.View.OnClickListener;  
      17. import android.view.animation.Animation;  
      18. import android.view.animation.AnimationUtils;  
      19. import android.widget.AdapterView;  
      20. import android.widget.AdapterView.OnItemClickListener;  
      21. import android.widget.Button;  
      22. import android.widget.GridView;  
      23. import android.widget.ImageView;  
      24. import android.widget.RelativeLayout;  
      25. import android.widget.RelativeLayout.LayoutParams;  
      26. import android.widget.TextView;  
      27. import android.widget.Toast;  
      28.   
      29. import com.xys.xpuzzle.R;  
      30. import com.xys.xpuzzle.adapter.GridItemsAdapter;  
      31. import com.xys.xpuzzle.bean.ItemBean;  
      32. import com.xys.xpuzzle.util.GameUtil;  
      33. import com.xys.xpuzzle.util.ImagesUtil;  
      34. import com.xys.xpuzzle.util.ScreenUtil;  
      35.   
      36. /** 
      37.  * 拼圖邏輯主界面:面板顯示 
      38.  *  
      39.  * @author xys 
      40.  *  
      41.  */  
      42. public class PuzzleMain extends Activity implements OnClickListener {  
      43.   
      44.     // 選擇的圖片  
      45.     private Bitmap picSelected;  
      46.     // 拼圖完成時(shí)顯示的最后一個(gè)圖片  
      47.     public static Bitmap lastBitmap;  
      48.     // PuzzlePanel  
      49.     private GridView gv_puzzle_main_detail;  
      50.     private int resId;  
      51.     private String picPath;  
      52.     private ImageView imageView;  
      53.     // Button  
      54.     private Button btnBack;  
      55.     private Button btnImage;  
      56.     private Button btnRestart;  
      57.     // 顯示步數(shù)  
      58.     private TextView tv_puzzle_main_counts;  
      59.     // 計(jì)時(shí)器  
      60.     private TextView tv_Timer;  
      61.     // 切圖后的圖片  
      62.     private List<Bitmap> bitmapItemLists = new ArrayList<Bitmap>();  
      63.     // GridView適配器  
      64.     private GridItemsAdapter adapter;  
      65.     // 設(shè)置為N*N顯示  
      66.     public static int type = 2;  
      67.     // Flag 是否已顯示原圖  
      68.     private boolean isShowImg;  
      69.     // 步數(shù)顯示  
      70.     public static int countIndex = 0;  
      71.     // 計(jì)時(shí)顯示  
      72.     public static int timerIndex = 0;  
      73.     // 計(jì)時(shí)器類  
      74.     private Timer timer;  
      75.   
      76.     /** 
      77.      * UI更新Handler 
      78.      */  
      79.     private Handler handler = new Handler() {  
      80.   
      81.     @Override  
      82.     public void handleMessage(Message msg) {  
      83.         switch (msg.what) {  
      84.         case 1:  
      85.         // 更新計(jì)時(shí)器  
      86.         timerIndex++;  
      87.         tv_Timer.setText("" + timerIndex);  
      88.         break;  
      89.         default:  
      90.         break;  
      91.         }  
      92.     }  
      93.     };  
      94.   
      95.     /** 
      96.      * 計(jì)時(shí)器線程 
      97.      */  
      98.     private TimerTask timerTask;  
      99.   
      100.     @Override  
      101.     protected void onCreate(Bundle savedInstanceState) {  
      102.     super.onCreate(savedInstanceState);  
      103.     setContentView(R.layout.xpuzzle_puzzle_detail_main);  
      104.     // 獲取選擇的圖片  
      105.     Bitmap picSelectedTemp;  
      106.     // 選擇默認(rèn)圖片還是自定義圖片  
      107.     resId = getIntent().getExtras().getInt("picSelectedID");  
      108.     picPath = getIntent().getExtras().getString("picPath");  
      109.     if (resId != 0) {  
      110.         picSelectedTemp = BitmapFactory.decodeResource(getResources(), resId);  
      111.     } else {  
      112.         picSelectedTemp = BitmapFactory.decodeFile(picPath);  
      113.     }  
      114.     type = getIntent().getExtras().getInt("type", 2);  
      115.     // 對圖片處理  
      116.     handlerImage(picSelectedTemp);  
      117.     // 初始化Views  
      118.     initViews();  
      119.     // 生成游戲數(shù)據(jù)  
      120.     generateGame();  
      121.     // GridView點(diǎn)擊事件  
      122.     gv_puzzle_main_detail.setOnItemClickListener(new OnItemClickListener() {  
      123.   
      124.         @Override  
      125.         public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {  
      126.         // 判斷是否可移動  
      127.         if (GameUtil.isMoveable(position)) {  
      128.             // 交換點(diǎn)擊Item與空格的位置  
      129.             GameUtil.swapItems(GameUtil.itemBeans.get(position), GameUtil.blankItemBean);  
      130.             // 重新獲取圖片  
      131.             recreateData();  
      132.             // 通知GridView更改UI  
      133.             adapter.notifyDataSetChanged();  
      134.             // 更新步數(shù)  
      135.             countIndex++;  
      136.             tv_puzzle_main_counts.setText("" + countIndex);  
      137.             // 判斷是否成功  
      138.             if (GameUtil.isSuccess()) {  
      139.             // 將最后一張圖顯示完整  
      140.             recreateData();  
      141.             bitmapItemLists.remove(type * type - 1);  
      142.             bitmapItemLists.add(lastBitmap);  
      143.             // 通知GridView更改UI  
      144.             adapter.notifyDataSetChanged();  
      145.             Toast.makeText(PuzzleMain.this, "拼圖成功!", Toast.LENGTH_LONG).show();  
      146.             gv_puzzle_main_detail.setEnabled(false);  
      147.             timer.cancel();  
      148.             timerTask.cancel();  
      149.             }  
      150.         }  
      151.         }  
      152.     });  
      153.     // 返回按鈕點(diǎn)擊事件  
      154.     btnBack.setOnClickListener(this);  
      155.     // 顯示原圖按鈕點(diǎn)擊事件  
      156.     btnImage.setOnClickListener(this);  
      157.     // 重置按鈕點(diǎn)擊事件  
      158.     btnRestart.setOnClickListener(this);  
      159.     }  
      160.   
      161.     /** 
      162.      * Button點(diǎn)擊事件 
      163.      */  
      164.     @Override  
      165.     public void onClick(View v) {  
      166.     switch (v.getId()) {  
      167.     // 返回按鈕點(diǎn)擊事件  
      168.     case R.id.btn_puzzle_main_back:  
      169.         PuzzleMain.this.finish();  
      170.         break;  
      171.     // 顯示原圖按鈕點(diǎn)擊事件  
      172.     case R.id.btn_puzzle_main_img:  
      173.         Animation animShow = AnimationUtils.loadAnimation(PuzzleMain.this, R.anim.image_show_anim);  
      174.         Animation animHide = AnimationUtils.loadAnimation(PuzzleMain.this, R.anim.image_hide_anim);  
      175.         if (isShowImg) {  
      176.         imageView.startAnimation(animHide);  
      177.         imageView.setVisibility(View.GONE);  
      178.         isShowImg = false;  
      179.         } else {  
      180.         imageView.startAnimation(animShow);  
      181.         imageView.setVisibility(View.VISIBLE);  
      182.         isShowImg = true;  
      183.         }  
      184.         break;  
      185.     // 重置按鈕點(diǎn)擊事件  
      186.     case R.id.btn_puzzle_main_restart:  
      187.         cleanConfig();  
      188.         generateGame();  
      189.         recreateData();  
      190.         // 通知GridView更改UI  
      191.         tv_puzzle_main_counts.setText("" + countIndex);  
      192.         adapter.notifyDataSetChanged();  
      193.         gv_puzzle_main_detail.setEnabled(true);  
      194.         break;  
      195.     default:  
      196.         break;  
      197.     }  
      198.     }  
      199.   
      200.     /** 
      201.      * 生成游戲數(shù)據(jù) 
      202.      */  
      203.     private void generateGame() {  
      204.     // 切圖 獲取初始拼圖數(shù)據(jù) 正常順序  
      205.     new ImagesUtil().createInitBitmaps(type, picSelected, PuzzleMain.this);  
      206.     // 生成隨機(jī)數(shù)據(jù)  
      207.     GameUtil.getPuzzleGenerator();  
      208.     // 獲取Bitmap集合  
      209.     for (ItemBean temp : GameUtil.itemBeans) {  
      210.         bitmapItemLists.add(temp.getBitmap());  
      211.     }  
      212.   
      213.     // 數(shù)據(jù)適配器  
      214.     adapter = new GridItemsAdapter(this, bitmapItemLists);  
      215.     gv_puzzle_main_detail.setAdapter(adapter);  
      216.   
      217.     // 啟用計(jì)時(shí)器  
      218.     timer = new Timer(true);  
      219.     // 計(jì)時(shí)器線程  
      220.     timerTask = new TimerTask() {  
      221.   
      222.         @Override  
      223.         public void run() {  
      224.         Message msg = new Message();  
      225.         msg.what = 1;  
      226.         handler.sendMessage(msg);  
      227.         }  
      228.     };  
      229.     // 每1000ms執(zhí)行 延遲0s  
      230.     timer.schedule(timerTask, 0, 1000);  
      231.     }  
      232.   
      233.     /** 
      234.      * 添加顯示原圖的View 
      235.      */  
      236.     private void addImgView() {  
      237.     RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rl_puzzle_main_main_layout);  
      238.     imageView = new ImageView(PuzzleMain.this);  
      239.     imageView.setImageBitmap(picSelected);  
      240.     int x = (int) (picSelected.getWidth() * 0.9F);  
      241.     int y = (int) (picSelected.getHeight() * 0.9F);  
      242.     LayoutParams params = new LayoutParams(x, y);  
      243.     params.addRule(RelativeLayout.CENTER_IN_PARENT);  
      244.     imageView.setLayoutParams(params);  
      245.     relativeLayout.addView(imageView);  
      246.     imageView.setVisibility(View.GONE);  
      247.     }  
      248.   
      249.     /** 
      250.      * 返回時(shí)調(diào)用 
      251.      */  
      252.     @Override  
      253.     protected void onStop() {  
      254.     super.onStop();  
      255.     // 清空相關(guān)參數(shù)設(shè)置  
      256.     cleanConfig();  
      257.     this.finish();  
      258.     }  
      259.   
      260.     /** 
      261.      * 清空相關(guān)參數(shù)設(shè)置 
      262.      */  
      263.     private void cleanConfig() {  
      264.     // 清空相關(guān)參數(shù)設(shè)置  
      265.     GameUtil.itemBeans.clear();  
      266.     // 停止計(jì)時(shí)器  
      267.     timer.cancel();  
      268.     timerTask.cancel();  
      269.     countIndex = 0;  
      270.     timerIndex = 0;  
      271.     // 清除拍攝的照片  
      272.     if (picPath != null) {  
      273.         // 刪除照片  
      274.         File file = new File(MainActivity.TEMP_IMAGE_PATH);  
      275.         if (file.exists()) {  
      276.         file.delete();  
      277.         }  
      278.     }  
      279.     }  
      280.   
      281.     /** 
      282.      * 重新獲取圖片 
      283.      */  
      284.     private void recreateData() {  
      285.     bitmapItemLists.clear();  
      286.     for (ItemBean temp : GameUtil.itemBeans) {  
      287.         bitmapItemLists.add(temp.getBitmap());  
      288.     }  
      289.     }  
      290.   
      291.     /** 
      292.      * 對圖片處理 自適應(yīng)大小 
      293.      *  
      294.      * @param bitmap 
      295.      */  
      296.     private void handlerImage(Bitmap bitmap) {  
      297.     // 將圖片放大到固定尺寸  
      298.     int screenWidth = ScreenUtil.getScreenSize(this).widthPixels;  
      299.     int screenHeigt = ScreenUtil.getScreenSize(this).heightPixels;  
      300.     picSelected = new ImagesUtil().resizeBitmap(screenWidth * 0.8f, screenHeigt * 0.6f, bitmap);  
      301.     }  
      302.   
      303.     /** 
      304.      * 初始化Views 
      305.      */  
      306.     private void initViews() {  
      307.     // Button  
      308.     btnBack = (Button) findViewById(R.id.btn_puzzle_main_back);  
      309.     btnImage = (Button) findViewById(R.id.btn_puzzle_main_img);  
      310.     btnRestart = (Button) findViewById(R.id.btn_puzzle_main_restart);  
      311.     // Flag 是否已顯示原圖  
      312.     isShowImg = false;  
      313.   
      314.     // GV  
      315.     gv_puzzle_main_detail = (GridView) findViewById(R.id.gv_puzzle_main_detail);  
      316.     // 設(shè)置為N*N顯示  
      317.     gv_puzzle_main_detail.setNumColumns(type);  
      318.     LayoutParams gridParams = new RelativeLayout.LayoutParams(picSelected.getWidth(), picSelected.getHeight());  
      319.     // 水平居中  
      320.     gridParams.addRule(RelativeLayout.CENTER_HORIZONTAL);  
      321.     // 其他格式屬性  
      322.     gridParams.addRule(RelativeLayout.BELOW, R.id.ll_puzzle_main_spinner);  
      323.     // Grid顯示  
      324.     gv_puzzle_main_detail.setLayoutParams(gridParams);  
      325.     gv_puzzle_main_detail.setHorizontalSpacing(0);  
      326.     gv_puzzle_main_detail.setVerticalSpacing(0);  
      327.   
      328.     // TV步數(shù)  
      329.     tv_puzzle_main_counts = (TextView) findViewById(R.id.tv_puzzle_main_counts);  
      330.     tv_puzzle_main_counts.setText("" + countIndex);  
      331.     // TV計(jì)時(shí)器  
      332.     tv_Timer = (TextView) findViewById(R.id.tv_puzzle_main_time);  
      333.     tv_Timer.setText("0秒");  
      334.   
      335.     // 添加顯示原圖的View  
      336.     addImgView();  
      337.     }  
      338. }  

      自認(rèn)為注釋、代碼風(fēng)格還是不錯(cuò)的,希望大家喜歡。

      PS:需要源代碼的朋友請留言,其實(shí)根據(jù)這個(gè)思路自己去實(shí)現(xiàn)下,對自己是有很大提高的,如果能優(yōu)化我的代碼,那就更好了,可以一起學(xué)習(xí)交流下。


        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多