2、牌桌页面
- 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;
- /**
- * 牌桌,会被老婆骂,最好不要上去,你懂的
- *
- * 扑克图片来源,和牌桌背景在文章的下面。 扑克背面图等我没上传,玩家自行百度
- *
- * @author Bison
- *
- */
- 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();// 实例化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) {// 左手边玩家,不用绘出正面
- // src = person.cardRect(person.person1[j], pukeWidth,
- // pukeHeight);
- // dst.set(10, j * 20, 10 + pukeWidth, j * 20 + pukeHeight);
- 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) {// 右手边玩家,同样不用绘出正面
- // src = person.cardRect(person.person3[j], pukeWidth,
- // pukeHeight);
- // dst.set(this.screenWidth - 10 - pukeWidth, j * 20,
- // this.screenWidth - 10, j * 20 + pukeHeight);
- 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();
- }
- }
- }
- // 刷帧线程,这个不解释,实在看不懂,M我:289302487@qq.com
- 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;
- }
- }
- }
