你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开发教程:斗地主 [牌桌实现源码]

[日期:2012-04-06] 来源:Linux社区  作者:Linux [字体:大 中 小]

2、牌桌页面

  1. package com.bison;  
  2.   
  3. import Android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Rect;  
  8. import android.view.MotionEvent;  
  9. import android.view.SurfaceHolder;  
  10. import android.view.SurfaceView;  
  11.   
  12. import com.bison.utils.Person;  
  13.   
  14. /** 
  15.  * 牌桌,会被老婆骂,最好不要上去,你懂的 
  16.  *  
  17.  * 扑克图片来源,和牌桌背景在文章的下面。 扑克背面图等我没上传,玩家自行百度 
  18.  *  
  19.  * @author Bison 
  20.  *  
  21.  */  
  22. public class GameView extends SurfaceView implements SurfaceHolder.Callback {  
  23.     private FlushThread thread = null;// 刷帧线程   
  24.     private Bitmap sourceBitmap = null;// 扑克图片来源   
  25.     private Bitmap backgroundDesk = null;// 牌桌背景   
  26.     private Bitmap backgroundPuke = null;// 扑克背面   
  27.   
  28.     private final Person person;  
  29.     private int pukeWidth = 0;// 扑克的宽   
  30.     private int pukeHeight = 0;// 扑克的高   
  31.     private int deskWidth = 0;// 牌桌的宽   
  32.     private int deskHeight = 0;// 牌桌的高   
  33.     private int left = 0;// 我自己首张牌左距离   
  34.   
  35.     public GameView(Context context) {  
  36.         super(context);  
  37.         getHolder().addCallback(this);  
  38.         this.thread = new FlushThread(getHolder(), this);// 实例化线程   
  39.         initBitmap();// 实例化图片   
  40.         this.person = new Person();// 实例化Person类   
  41.         this.left = deskWidth / 2 - (16 * 25 + pukeWidth) / 2;// 左距开始时赋值   
  42.     }  
  43.   
  44.     private void initBitmap() {// 初始化图片   
  45.         sourceBitmap = BitmapFactory.decodeResource(getResources(),  
  46.                 R.drawable.smallcard);  
  47.         pukeWidth = sourceBitmap.getWidth() / 14;// 每张扑克的宽高   
  48.         pukeHeight = sourceBitmap.getHeight() / 4;  
  49.   
  50.         backgroundDesk = BitmapFactory.decodeResource(getResources(),  
  51.                 R.drawable.gameback2);  
  52.   
  53.         deskWidth = backgroundDesk.getWidth();// 牌桌的宽高   
  54.         deskHeight = backgroundDesk.getHeight();  
  55.   
  56.         backgroundPuke = BitmapFactory.decodeResource(getResources(),  
  57.                 R.drawable.cardback);  
  58.     }  
  59.   
  60.     @Override  
  61.     protected void onDraw(Canvas canvas) {  
  62.         // 绘制牌桌   
  63.         canvas.drawBitmap(backgroundDesk, 0, 0, null);  
  64.         personPaint(canvas, pukeWidth, pukeHeight);  
  65.         deskthreePukes(canvas, pukeWidth, pukeHeight);  
  66.     }  
  67.   
  68.     /** 绘制每个玩家手里的牌 */  
  69.     public void personPaint(Canvas c, int pukeWidth, int pukeHeight) {  
  70.         Rect src = new Rect();  
  71.         Rect dst = new Rect();  
  72.   
  73.         // 遍历数组   
  74.         for (int i = 0; i < 3; i++) {  
  75.             for (int j = 0; j < 17; j++) {  
  76.                 if (i == 0) {// 左手边玩家,不用绘出正面   
  77.                     // src = person.cardRect(person.person1[j], pukeWidth,   
  78.                     // pukeHeight);   
  79.                     // dst.set(10, j * 20, 10 + pukeWidth, j * 20 + pukeHeight);   
  80.                     c.drawBitmap(backgroundPuke, 35, 85, null);  
  81.                 }  
  82.                 if (i == 1) {// 自己   
  83.                     src = person.cardRect(person.person2[j], pukeWidth,  
  84.                             pukeHeight);  
  85.                     dst.set(left + j * 25, this.deskHeight - 20 - pukeHeight,  
  86.                             left + j * 25 + pukeWidth, deskHeight - 20);  
  87.                     c.drawBitmap(sourceBitmap, src, dst, null);  
  88.                 }  
  89.                 if (i == 2) {// 右手边玩家,同样不用绘出正面   
  90.                     // src = person.cardRect(person.person3[j], pukeWidth,   
  91.                     // pukeHeight);   
  92.                     // dst.set(this.screenWidth - 10 - pukeWidth, j * 20,   
  93.                     // this.screenWidth - 10, j * 20 + pukeHeight);   
  94.                     c.drawBitmap(backgroundPuke, deskWidth - 35 - pukeWidth,  
  95.                             85, null);  
  96.                 }  
  97.             }  
  98.         }  
  99.     }  
  100.   
  101.     /** 绘制三张底牌 */  
  102.     private void deskthreePukes(Canvas c, int pukeWidth, int pukeHeight) {  
  103.         Rect src = new Rect();  
  104.         Rect dst = new Rect();  
  105.         for (int i = 0; i < 3; i++) {  
  106.             src = person.cardRect(person.threePukes[i], pukeWidth, pukeHeight);  
  107.             dst.set(280 + i * pukeWidth, 12, 280 + (i + 1) * pukeWidth,  
  108.                     12 + pukeHeight);  
  109.             c.drawBitmap(sourceBitmap, src, dst, null);  
  110.         }  
  111.     }  
  112.   
  113.     @Override  
  114.     public boolean onTouchEvent(MotionEvent event) {  
  115.         // 正在研究点击弹出相应的扑克   
  116.         return super.onTouchEvent(event);  
  117.     }  
  118.   
  119.     @Override  
  120.     public void surfaceChanged(SurfaceHolder holder, int format, int width,  
  121.             int height) {  
  122.     }  
  123.   
  124.     @Override  
  125.     public void surfaceCreated(SurfaceHolder holder) {  
  126.         this.thread.setFlag(true);  
  127.         this.thread.start();  
  128.     }  
  129.   
  130.     @Override  
  131.     public void surfaceDestroyed(SurfaceHolder holder) {  
  132.         boolean retry = true;  
  133.         this.thread.setFlag(false);  
  134.         while (retry) {  
  135.             try {  
  136.                 thread.join();  
  137.                 retry = false;  
  138.             } catch (InterruptedException e) {  
  139.                 e.printStackTrace();  
  140.             }  
  141.         }  
  142.   
  143.     }  
  144.   
  145.     // 刷帧线程,这个不解释,实在看不懂,M我:289302487@qq.com   
  146.     class FlushThread extends Thread {  
  147.         private boolean flag = false;  
  148.         private final int span = 500;  
  149.         private final GameView gameView;  
  150.         private final SurfaceHolder holder;  
  151.   
  152.         public FlushThread(SurfaceHolder holder, GameView gameView) {  
  153.             this.gameView = gameView;  
  154.             this.holder = holder;  
  155.         }  
  156.   
  157.         @Override  
  158.         public void run() {  
  159.             Canvas canvas;  
  160.             while (this.flag) {  
  161.                 canvas = null;  
  162.                 try {  
  163.                     canvas = this.holder.lockCanvas(null);  
  164.                     synchronized (this.holder) {  
  165.                         this.gameView.onDraw(canvas);  
  166.                     }  
  167.                 } finally {  
  168.                     if (canvas != null) {  
  169.                         this.holder.unlockCanvasAndPost(canvas);  
  170.                     }  
  171.                 }  
  172.   
  173.                 try {  
  174.                     Thread.sleep(span);  
  175.                 } catch (InterruptedException e) {  
  176.                     e.printStackTrace();  
  177.                 }  
  178.             }  
  179.         }  
  180.   
  181.         public boolean isFlag() {  
  182.             return flag;  
  183.         }  
  184.   
  185.         public void setFlag(boolean flag) {  
  186.             this.flag = flag;  
  187.         }  
  188.   
  189.     }  
  190.   
  191. }  
linux
相关资讯       Android开发基础教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款