[Java] Android开发的斗地主牌桌实现代码 →→→→→进入此内容的聊天室

来自 , 2020-12-18, 写在 Java, 查看 125 次.
URL http://www.code666.cn/view/cdf6581c
  1.  
  2. package com.bison;  
  3.  
  4. import android.app.Activity;  
  5. import android.content.pm.ActivityInfo;  
  6. import android.os.Bundle;  
  7. import android.view.Window;  
  8. import android.view.WindowManager;  
  9.  
  10. /**
  11.  * 求某公司包养
  12.  *  
  13.  * @author Bison
  14.  *  
  15.  */  
  16. public class PukeActivity extends Activity {  
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         // 这个事隐藏标题栏,不解释  
  22.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  23.         // 隐藏状态栏,你懂的  
  24.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  25.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  26.         /*
  27.          * 开始有考虑使屏幕上扑克的排列随屏幕的分辨率变动 结果貌似不好做,注释掉了 Display display =
  28.          * getWindowManager().getDefaultDisplay(); int screenWidth =
  29.          * display.getWidth(); int screenHeight = display.getHeight();
  30.          */  
  31.  
  32.         // 使用代码锁定横屏  
  33.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
  34.         // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);这个是竖屏  
  35.         setContentView(new GameView(this));  
  36.     }  
  37. }
  38. 2、牌桌页面
  39.  
  40. package com.bison;  
  41.  
  42. import android.content.Context;  
  43. import android.graphics.Bitmap;  
  44. import android.graphics.BitmapFactory;  
  45. import android.graphics.Canvas;  
  46. import android.graphics.Rect;  
  47. import android.view.MotionEvent;  
  48. import android.view.SurfaceHolder;  
  49. import android.view.SurfaceView;  
  50.  
  51. import com.bison.utils.Person;  
  52.  
  53. /**
  54.  * 牌桌,会被老婆骂,最好不要上去,你懂的
  55.  *  
  56.  * 扑克图片来源,和牌桌背景在文章的下面。 扑克背面图等我没上传,玩家自行百度
  57.  *  
  58.  * @author Bison
  59.  *  
  60.  */  
  61. public class GameView extends SurfaceView implements SurfaceHolder.Callback {  
  62.     private FlushThread thread = null;// 刷帧线程  
  63.     private Bitmap sourceBitmap = null;// 扑克图片来源  
  64.     private Bitmap backgroundDesk = null;// 牌桌背景  
  65.     private Bitmap backgroundPuke = null;// 扑克背面  
  66.  
  67.     private final Person person;  
  68.     private int pukeWidth = 0;// 扑克的宽  
  69.     private int pukeHeight = 0;// 扑克的高  
  70.     private int deskWidth = 0;// 牌桌的宽  
  71.     private int deskHeight = 0;// 牌桌的高  
  72.     private int left = 0;// 我自己首张牌左距离  
  73.  
  74.     public GameView(Context context) {  
  75.         super(context);  
  76.         getHolder().addCallback(this);  
  77.         this.thread = new FlushThread(getHolder(), this);// 实例化线程  
  78.         initBitmap();// 实例化图片  
  79.         this.person = new Person();// 实例化Person类  
  80.         this.left = deskWidth / 2 - (16 * 25 + pukeWidth) / 2;// 左距开始时赋值  
  81.     }  
  82.  
  83.     private void initBitmap() {// 初始化图片  
  84.         sourceBitmap = BitmapFactory.decodeResource(getResources(),  
  85.                 R.drawable.smallcard);  
  86.         pukeWidth = sourceBitmap.getWidth() / 14;// 每张扑克的宽高  
  87.         pukeHeight = sourceBitmap.getHeight() / 4;  
  88.  
  89.         backgroundDesk = BitmapFactory.decodeResource(getResources(),  
  90.                 R.drawable.gameback2);  
  91.  
  92.         deskWidth = backgroundDesk.getWidth();// 牌桌的宽高  
  93.         deskHeight = backgroundDesk.getHeight();  
  94.  
  95.         backgroundPuke = BitmapFactory.decodeResource(getResources(),  
  96.                 R.drawable.cardback);  
  97.     }  
  98.  
  99.     @Override  
  100.     protected void onDraw(Canvas canvas) {  
  101.         // 绘制牌桌  
  102.         canvas.drawBitmap(backgroundDesk, 0, 0, null);  
  103.         personPaint(canvas, pukeWidth, pukeHeight);  
  104.         deskthreePukes(canvas, pukeWidth, pukeHeight);  
  105.     }  
  106.  
  107.     /** 绘制每个玩家手里的牌 */  
  108.     public void personPaint(Canvas c, int pukeWidth, int pukeHeight) {  
  109.         Rect src = new Rect();  
  110.         Rect dst = new Rect();  
  111.  
  112.         // 遍历数组  
  113.         for (int i = 0; i < 3; i++) {  
  114.             for (int j = 0; j < 17; j++) {  
  115.                 if (i == 0) {// 左手边玩家,不用绘出正面  
  116.                     // src = person.cardRect(person.person1[j], pukeWidth,  
  117.                     // pukeHeight);  
  118.                     // dst.set(10, j * 20, 10 + pukeWidth, j * 20 + pukeHeight);  
  119.                     c.drawBitmap(backgroundPuke, 35, 85, null);  
  120.                 }  
  121.                 if (i == 1) {// 自己  
  122.                     src = person.cardRect(person.person2[j], pukeWidth,  
  123.                             pukeHeight);  
  124.                     dst.set(left + j * 25, this.deskHeight - 20 - pukeHeight,  
  125.                             left + j * 25 + pukeWidth, deskHeight - 20);  
  126.                     c.drawBitmap(sourceBitmap, src, dst, null);  
  127.                 }  
  128.                 if (i == 2) {// 右手边玩家,同样不用绘出正面  
  129.                     // src = person.cardRect(person.person3[j], pukeWidth,  
  130.                     // pukeHeight);  
  131.                     // dst.set(this.screenWidth - 10 - pukeWidth, j * 20,  
  132.                     // this.screenWidth - 10, j * 20 + pukeHeight);  
  133.                     c.drawBitmap(backgroundPuke, deskWidth - 35 - pukeWidth,  
  134.                             85, null);  
  135.                 }  
  136.             }  
  137.         }  
  138.     }  
  139.  
  140.     /** 绘制三张底牌 */  
  141.     private void deskthreePukes(Canvas c, int pukeWidth, int pukeHeight) {  
  142.         Rect src = new Rect();  
  143.         Rect dst = new Rect();  
  144.         for (int i = 0; i < 3; i++) {  
  145.             src = person.cardRect(person.threePukes[i], pukeWidth, pukeHeight);  
  146.             dst.set(280 + i * pukeWidth, 12, 280 + (i + 1) * pukeWidth,  
  147.                     12 + pukeHeight);  
  148.             c.drawBitmap(sourceBitmap, src, dst, null);  
  149.         }  
  150.     }  
  151.  
  152.     @Override  
  153.     public boolean onTouchEvent(MotionEvent event) {  
  154.         // 正在研究点击弹出相应的扑克  
  155.         return super.onTouchEvent(event);  
  156.     }  
  157.  
  158.     @Override  
  159.     public void surfaceChanged(SurfaceHolder holder, int format, int width,  
  160.             int height) {  
  161.     }  
  162.  
  163.     @Override  
  164.     public void surfaceCreated(SurfaceHolder holder) {  
  165.         this.thread.setFlag(true);  
  166.         this.thread.start();  
  167.     }  
  168.  
  169.     @Override  
  170.     public void surfaceDestroyed(SurfaceHolder holder) {  
  171.         boolean retry = true;  
  172.         this.thread.setFlag(false);  
  173.         while (retry) {  
  174.             try {  
  175.                 thread.join();  
  176.                 retry = false;  
  177.             } catch (InterruptedException e) {  
  178.                 e.printStackTrace();  
  179.             }  
  180.         }  
  181.  
  182.     }  
  183.  
  184.     // 刷帧线程,这个不解释,实在看不懂,M我:289302487@qq.com  
  185.     class FlushThread extends Thread {  
  186.         private boolean flag = false;  
  187.         private final int span = 500;  
  188.         private final GameView gameView;  
  189.         private final SurfaceHolder holder;  
  190.  
  191.         public FlushThread(SurfaceHolder holder, GameView gameView) {  
  192.             this.gameView = gameView;  
  193.             this.holder = holder;  
  194.         }  
  195.  
  196.         @Override  
  197.         public void run() {  
  198.             Canvas canvas;  
  199.             while (this.flag) {  
  200.                 canvas = null;  
  201.                 try {  
  202.                     canvas = this.holder.lockCanvas(null);  
  203.                     synchronized (this.holder) {  
  204.                         this.gameView.onDraw(canvas);  
  205.                     }  
  206.                 } finally {  
  207.                     if (canvas != null) {  
  208.                         this.holder.unlockCanvasAndPost(canvas);  
  209.                     }  
  210.                 }  
  211.  
  212.                 try {  
  213.                     Thread.sleep(span);  
  214.                 } catch (InterruptedException e) {  
  215.                     e.printStackTrace();  
  216.                 }  
  217.             }  
  218.         }  
  219.  
  220.         public boolean isFlag() {  
  221.             return flag;  
  222.         }  
  223.  
  224.         public void setFlag(boolean flag) {  
  225.             this.flag = flag;  
  226.         }  
  227.  
  228.     }  
  229.  
  230. }  
  231.  
  232.  
  233. //java/6173

回复 "Android开发的斗地主牌桌实现代码"

这儿你可以回复上面这条便签

captcha