1、主Activity
- <span style="font-size:18px;color:#3333ff;">package com.bison;
-
- import android.app.Activity;
- import android.content.pm.ActivityInfo;
- import android.os.Bundle;
- import android.view.Window;
- import android.view.WindowManager;
-
- /**
- * </span>
- <span style="font-size:18px;color:#3333ff;"> * @author Bison
- *
- */
- public class PukeActivity extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- requestWindowFeature(Window.FEATURE_NO_TITLE);
-
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
-
-
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
-
- setContentView(new GameView(this));
- }
- }</span>
2、牌桌页面
- <span style="color:#3333ff;">package com.bison;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Rect;
- import android.view.MotionEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
-
- import com.bison.utils.Person;
-
- public class GameView extends SurfaceView implements SurfaceHolder.Callback {
- private FlushThread thread = null;
- private Bitmap sourceBitmap = null;
- private Bitmap backgroundDesk = null;
- private Bitmap backgroundPuke = null;
-
- private final Person person;
- private int pukeWidth = 0;
- private int pukeHeight = 0;
- private int deskWidth = 0;
- private int deskHeight = 0;
- private int left = 0;
-
- public GameView(Context context) {
- super(context);
- getHolder().addCallback(this);
- this.thread = new FlushThread(getHolder(), this);
- initBitmap();
- this.person = new Person();
- this.left = deskWidth / 2 - (16 * 25 + pukeWidth) / 2;
- }
-
- private void initBitmap() {
- sourceBitmap = BitmapFactory.decodeResource(getResources(),
- R.drawable.smallcard);
- pukeWidth = sourceBitmap.getWidth() / 14;
- pukeHeight = sourceBitmap.getHeight() / 4;
-
- backgroundDesk = BitmapFactory.decodeResource(getResources(),
- R.drawable.gameback2);
-
- deskWidth = backgroundDesk.getWidth();
- deskHeight = backgroundDesk.getHeight();
-
- backgroundPuke = BitmapFactory.decodeResource(getResources(),
- R.drawable.cardback);
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
-
- canvas.drawBitmap(backgroundDesk, 0, 0, null);
- personPaint(canvas, pukeWidth, pukeHeight);
- deskthreePukes(canvas, pukeWidth, pukeHeight);
- }
-
-
- public void personPaint(Canvas c, int pukeWidth, int pukeHeight) {
- Rect src = new Rect();
- Rect dst = new Rect();
-
-
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 17; j++) {
- if (i == 0) {
-
-
-
- c.drawBitmap(backgroundPuke, 35, 85, null);
- }
- if (i == 1) {
- src = person.cardRect(person.person2[j], pukeWidth,
- pukeHeight);
- dst.set(left + j * 25, this.deskHeight - 20 - pukeHeight,
- left + j * 25 + pukeWidth, deskHeight - 20);
- c.drawBitmap(sourceBitmap, src, dst, null);
- }
- if (i == 2) {
-
-
-
-
- c.drawBitmap(backgroundPuke, deskWidth - 35 - pukeWidth,
- 85, null);
- }
- }
- }
- }
-
-
- private void deskthreePukes(Canvas c, int pukeWidth, int pukeHeight) {
- Rect src = new Rect();
- Rect dst = new Rect();
- for (int i = 0; i < 3; i++) {
- src = person.cardRect(person.threePukes[i], pukeWidth, pukeHeight);
- dst.set(280 + i * pukeWidth, 12, 280 + (i + 1) * pukeWidth,
- 12 + pukeHeight);
- c.drawBitmap(sourceBitmap, src, dst, null);
- }
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
-
- return super.onTouchEvent(event);
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- }
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- this.thread.setFlag(true);
- this.thread.start();
- }
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- boolean retry = true;
- this.thread.setFlag(false);
- while (retry) {
- try {
- thread.join();
- retry = false;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- }
-
-
- <span style="color:#3333ff;"> class FlushThread extends Thread {
- private boolean flag = false;
- private final int span = 500;
- private final GameView gameView;
- private final SurfaceHolder holder;
-
- public FlushThread(SurfaceHolder holder, GameView gameView) {
- this.gameView = gameView;
- this.holder = holder;
- }
-
- @Override
- public void run() {
- Canvas canvas;
- while (this.flag) {
- canvas = null;
- try {
- canvas = this.holder.lockCanvas(null);
- synchronized (this.holder) {
- this.gameView.onDraw(canvas);
- }
- } finally {
- if (canvas != null) {
- this.holder.unlockCanvasAndPost(canvas);
- }
- }
-
- try {
- Thread.sleep(span);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- public boolean isFlag() {
- return flag;
- }
-
- public void setFlag(boolean flag) {
- this.flag = flag;
- }
-
- }
-
- }
- </span>
3、相关实体类
扑克牌类:
- <span style="font-size:18px;color:#3333ff;">package com.bison.utils;
-
- import java.util.Random;
-
- public class Cards {
-
- public int[] pukes = new int[54];
-
- private static Cards cardsInstance = null;
-
- private Cards() {
- setPuke();
- shuffle();
- }
-
- public static Cards getInstance() {
- if (cardsInstance == null) {
- cardsInstance = new Cards();
- }
- return cardsInstance;
- }
-
-
- private void setPuke() {
- for (int i = 0; i < 54; i++) {
- pukes[i] = i + 1;
- }
- }
-
-
- private void shuffle() {
- Random rdm = new Random();
- for (int i = 0; i < 54; i++) {
-
- int rdmNo = rdm.nextInt(54);
- int temp = pukes[i];
- pukes[i] = pukes[rdmNo];
- pukes[rdmNo] = temp;
- }
- }
- }
- </span>
玩家类:
- <span style="font-size:18px;color:#3333ff;">package com.bison.utils;
-
- import android.graphics.Rect;
-
- public class Person {
- private final Cards mCards = Cards.getInstance();
-
- public int[] person1 = new int[17];
- public int[] person2 = new int[17];
- public int[] person3 = new int[17];
-
-
- public int[] threePukes = new int[3];
-
- public Person() {
- personHold(mCards.pukes);
- }
-
-
- private void personHold(int[] pukes) {
- int k = 0;
- for (int i = 0; i < 3; i++) {
- if (i == 0) {
- for (int j = 0; j < 17; j++) {
- person1[j] = pukes[k++];
- }
-
- sort(person1);
- }
- if (i == 1) {
- for (int j = 0; j < 17; j++) {
- person2[j] = pukes[k++];
- }
-
- sort(person2);
- }
- if (i == 2) {
- for (int j = 0; j < 17; j++) {
- person3[j] = pukes[k++];
- }
-
- sort(person3);
- }
- }
-
- threePukes[0] = pukes[51];
- threePukes[1] = pukes[52];
- threePukes[2] = pukes[53];
- }
-
-
- private void sort(int[] ary) {
- for (int i = 0; i < ary.length; i++) {
- for (int j = 0; j < ary.length - i - 1; j++) {
- if (ary[j] > ary[j + 1]) {
- int temp = ary[j];
- ary[j] = ary[j + 1];
- ary[j + 1] = temp;
- }
- }
- }
- }
-
-
- public Rect cardRect(int cardValue, int width, int height) {
- int x = 0, y = 0;
- if (cardValue % 4 == 0) {
- x = cardValue / 4 - 1;
- y = 4;
- } else {
- x = cardValue / 4;
- y = cardValue % 4;
- }
-
- int left = x * width;
- int top = (y - 1) * height;
- int right = (x + 1) * width;
- int bottom = (y) * height;
- return new Rect(left, top, right, bottom);
- }
- }
- </span>
PS:斗地主还是可以做成很复杂的。相关图片

Android --- 斗地主 [牌桌实现源码]
原文:http://www.cnblogs.com/endv/p/5816099.html