├── README.md ├── ic_launcher-web.png ├── res ├── drawable │ ├── bc.png │ ├── bg.png │ ├── cp.png │ ├── me.png │ ├── icon.png │ ├── left.png │ ├── lord.png │ ├── poker.png │ ├── right.png │ ├── ic_launcher.png │ └── poker_mini.png ├── drawable-hdpi │ └── ic_launcher.png ├── drawable-ldpi │ └── ic_launcher.png ├── drawable-mdpi │ └── ic_launcher.png ├── drawable-xhdpi │ └── ic_launcher.png ├── values │ ├── strings.xml │ └── styles.xml ├── menu │ └── activity_main.xml ├── values-v11 │ └── styles.xml ├── values-v14 │ └── styles.xml └── layout │ └── activity_main.xml ├── libs └── android-support-v4.jar ├── .gitignore ├── project.properties ├── src └── com │ └── dongluh │ └── ddz │ ├── MainActivity.java │ ├── model │ ├── Card.java │ └── Player.java │ └── view │ ├── BaseView.java │ └── GameView.java ├── proguard-project.txt ├── AndroidManifest.xml └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | doudizhu 2 | ======== 3 | 4 | Android 斗地主游戏 5 | -------------------------------------------------------------------------------- /ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/ic_launcher-web.png -------------------------------------------------------------------------------- /res/drawable/bc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable/bc.png -------------------------------------------------------------------------------- /res/drawable/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable/bg.png -------------------------------------------------------------------------------- /res/drawable/cp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable/cp.png -------------------------------------------------------------------------------- /res/drawable/me.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable/me.png -------------------------------------------------------------------------------- /res/drawable/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable/icon.png -------------------------------------------------------------------------------- /res/drawable/left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable/left.png -------------------------------------------------------------------------------- /res/drawable/lord.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable/lord.png -------------------------------------------------------------------------------- /res/drawable/poker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable/poker.png -------------------------------------------------------------------------------- /res/drawable/right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable/right.png -------------------------------------------------------------------------------- /libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/libs/android-support-v4.jar -------------------------------------------------------------------------------- /res/drawable/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable/poker_mini.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable/poker_mini.png -------------------------------------------------------------------------------- /res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-ldpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable-ldpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/donglua/doudizhu/HEAD/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | DDZ 5 | Hello world! 6 | Settings 7 | 8 | -------------------------------------------------------------------------------- /res/menu/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | -------------------------------------------------------------------------------- /res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | -------------------------------------------------------------------------------- /res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | 18 | # Eclipse project files 19 | .classpath 20 | .project 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Intellij project files 26 | *.iml 27 | *.ipr 28 | *.iws 29 | .idea/ 30 | -------------------------------------------------------------------------------- /project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-19 15 | -------------------------------------------------------------------------------- /src/com/dongluh/ddz/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.dongluh.ddz; 2 | 3 | import android.os.Bundle; 4 | import android.app.Activity; 5 | import android.view.Menu; 6 | 7 | public class MainActivity extends Activity { 8 | 9 | @Override 10 | protected void onCreate(Bundle savedInstanceState) { 11 | super.onCreate(savedInstanceState); 12 | setContentView(R.layout.activity_main); 13 | } 14 | 15 | @Override 16 | public boolean onCreateOptionsMenu(Menu menu) { 17 | // Inflate the menu; this adds items to the action bar if it is present. 18 | getMenuInflater().inflate(R.menu.activity_main, menu); 19 | return true; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | -------------------------------------------------------------------------------- /proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /src/com/dongluh/ddz/model/Card.java: -------------------------------------------------------------------------------- 1 | package com.dongluh.ddz.model; 2 | 3 | import android.graphics.Bitmap; 4 | 5 | /** 6 | * 牌类 7 | */ 8 | public class Card { 9 | private int color; 10 | 11 | private int value; 12 | 13 | private Bitmap bigImg; 14 | 15 | private Bitmap smallImg; 16 | 17 | public int getColor() { 18 | return color; 19 | } 20 | 21 | public void setColor(int color) { 22 | this.color = color; 23 | } 24 | 25 | public int getValue() { 26 | return value; 27 | } 28 | 29 | public void setValue(int value) { 30 | this.value = value; 31 | } 32 | 33 | public Bitmap getBigImg() { 34 | return bigImg; 35 | } 36 | 37 | public void setBigImg(Bitmap bigImg) { 38 | this.bigImg = bigImg; 39 | } 40 | 41 | public Bitmap getSmallImg() { 42 | return smallImg; 43 | } 44 | 45 | public void setSmallImg(Bitmap smallImg) { 46 | this.smallImg = smallImg; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 16 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/com/dongluh/ddz/view/BaseView.java: -------------------------------------------------------------------------------- 1 | package com.dongluh.ddz.view; 2 | 3 | import java.util.Comparator; 4 | 5 | import com.dongluh.ddz.model.Card; 6 | 7 | import android.app.Activity; 8 | import android.content.Context; 9 | import android.graphics.Canvas; 10 | import android.graphics.Color; 11 | import android.graphics.Paint; 12 | import android.util.AttributeSet; 13 | import android.view.SurfaceHolder; 14 | import android.view.SurfaceHolder.Callback; 15 | import android.view.SurfaceView; 16 | 17 | public abstract class BaseView extends SurfaceView implements Callback, 18 | Runnable, Comparator { 19 | 20 | protected Paint paint; // 默认的画笔 21 | public static final int TEXT_SIZE = 20; 22 | 23 | private SurfaceHolder holder; // Surface的大管家 24 | private Thread pThread; // 绘图线程 25 | private boolean isRunning; // 是否在绘图 26 | private static final int SPAN = 100; 27 | 28 | /** 29 | * 屏幕尺寸 30 | */ 31 | public static int winWidth, winHeight; 32 | 33 | public BaseView(Context context, AttributeSet attrs) { 34 | super(context, attrs); 35 | 36 | holder = getHolder(); 37 | holder.addCallback(this); 38 | paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 设置抗拒出 39 | paint.setTextSize(TEXT_SIZE); 40 | paint.setColor(Color.WHITE); // 白色 41 | 42 | // 获得屏幕尺寸 43 | winWidth = ((Activity) context).getWindowManager().getDefaultDisplay() 44 | .getWidth(); 45 | winHeight = ((Activity) context).getWindowManager().getDefaultDisplay() 46 | .getHeight(); 47 | } 48 | 49 | @Override 50 | public void run() { 51 | while (isRunning) { 52 | Canvas canvas = null; 53 | try { 54 | // ���� 55 | synchronized (holder) { 56 | canvas = holder.lockCanvas(); 57 | } 58 | // ��ͼ�߼� 59 | render(canvas); 60 | } finally { 61 | // �������ص����̣߳���Ⱦ����Ļ�� 62 | if (canvas != null) 63 | holder.unlockCanvasAndPost(canvas); 64 | } 65 | 66 | try { 67 | Thread.sleep(SPAN); 68 | } catch (InterruptedException e) { 69 | e.printStackTrace(); 70 | } 71 | } 72 | } 73 | 74 | @Override 75 | public void surfaceCreated(SurfaceHolder holder) { 76 | // 开启绘图线程 77 | pThread = new Thread(this); 78 | pThread.start(); 79 | isRunning = true; 80 | } 81 | 82 | @Override 83 | public void surfaceChanged(SurfaceHolder holder, int format, int width, 84 | int height) { 85 | } 86 | 87 | @Override 88 | public void surfaceDestroyed(SurfaceHolder holder) { 89 | // 销毁绘图线程 90 | isRunning = false; 91 | if (pThread != null && pThread.isAlive()) { 92 | try { 93 | pThread.join(); 94 | } catch (InterruptedException e) { 95 | e.printStackTrace(); 96 | } 97 | } 98 | } 99 | 100 | // 比较牌大小 101 | @Override 102 | public int compare(Card card1, Card card2) { 103 | //先比较 值 104 | int result = card1.getValue() - card2.getValue(); 105 | if (result == 0) { 106 | // 如果值一样, 就比较花色 107 | result = card1.getColor() - card2.getColor(); 108 | } 109 | return result; 110 | } 111 | 112 | protected abstract void render(Canvas canvas); 113 | } 114 | -------------------------------------------------------------------------------- /src/com/dongluh/ddz/model/Player.java: -------------------------------------------------------------------------------- 1 | package com.dongluh.ddz.model; 2 | 3 | import static com.dongluh.ddz.view.BaseView.TEXT_SIZE; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | import com.dongluh.ddz.view.BaseView; 9 | import com.dongluh.ddz.view.GameView; 10 | 11 | import android.graphics.Bitmap; 12 | import android.graphics.Canvas; 13 | import android.graphics.Paint; 14 | import android.graphics.Rect; 15 | 16 | /** 17 | * 玩家类 18 | * 19 | * @author donglu 20 | */ 21 | public class Player { 22 | 23 | private String name; 24 | 25 | private String score; 26 | 27 | private Bitmap head; 28 | 29 | private int x; 30 | 31 | private int y; 32 | 33 | private boolean isLandlord; 34 | 35 | private boolean isComputer; 36 | 37 | private Player next; 38 | 39 | /** 40 | * 手里的牌 41 | */ 42 | private List handCards = new ArrayList(); 43 | /** 44 | * 即将打出去的牌 45 | */ 46 | private List willSendCards = new ArrayList(); 47 | /** 48 | * 打出去的牌 49 | */ 50 | private List sentCards = new ArrayList(); 51 | 52 | public List getHandCards() { 53 | return handCards; 54 | } 55 | 56 | public void setHandCards(List handCards) { 57 | this.handCards = handCards; 58 | } 59 | 60 | public List getWillSendCards() { 61 | return willSendCards; 62 | } 63 | 64 | public void setWillSendCards(List willSendCards) { 65 | this.willSendCards = willSendCards; 66 | } 67 | 68 | public List getSentCards() { 69 | return sentCards; 70 | } 71 | 72 | public void setSentCards(List sentCards) { 73 | this.sentCards = sentCards; 74 | } 75 | 76 | public String getName() { 77 | return name; 78 | } 79 | 80 | public void setName(String name) { 81 | this.name = name; 82 | } 83 | 84 | public String getScore() { 85 | return score; 86 | } 87 | 88 | public void setScore(String score) { 89 | this.score = score; 90 | } 91 | 92 | public Bitmap getHead() { 93 | return head; 94 | } 95 | 96 | public void setHead(Bitmap head) { 97 | this.head = head; 98 | } 99 | 100 | public int getX() { 101 | return x; 102 | } 103 | 104 | public void setX(int x) { 105 | this.x = x; 106 | } 107 | 108 | public int getY() { 109 | return y; 110 | } 111 | 112 | public void setY(int y) { 113 | this.y = y; 114 | } 115 | 116 | public boolean isLandlord() { 117 | return isLandlord; 118 | } 119 | 120 | public void setLandlord(boolean isLandlord) { 121 | this.isLandlord = isLandlord; 122 | } 123 | 124 | public boolean isComputer() { 125 | return isComputer; 126 | } 127 | 128 | public void setComputer(boolean isComputer) { 129 | this.isComputer = isComputer; 130 | } 131 | 132 | public Player getNext() { 133 | return next; 134 | } 135 | 136 | public void setNext(Player next) { 137 | this.next = next; 138 | } 139 | 140 | public void draw(Canvas canvas, Paint paint) { 141 | String scoreText = "积分(" + score + ")"; 142 | int sorceX = x; 143 | // 画头像 144 | canvas.drawBitmap(head, x, y, paint); 145 | 146 | Rect tempRect = new Rect(); 147 | paint.getTextBounds(name, 0, name.length(), tempRect); 148 | int nameWidth = tempRect.right; // name的宽度 149 | if (x + nameWidth > BaseView.winWidth) { // name超出屏幕 150 | x = BaseView.winWidth - nameWidth - GameView.PLAYER_MARGIN; 151 | } 152 | // 画名字 153 | canvas.drawText(name, x, y + head.getHeight() + TEXT_SIZE, paint); 154 | 155 | paint.getTextBounds(scoreText, 0, scoreText.length(), tempRect); 156 | int scoreWidth = tempRect.right; // score的宽度 157 | if (x + scoreWidth > BaseView.winWidth) { // score超出屏幕 158 | sorceX = BaseView.winWidth - scoreWidth - GameView.PLAYER_MARGIN; 159 | } 160 | // 画积分 161 | canvas.drawText(scoreText, sorceX, 162 | y + head.getHeight() + TEXT_SIZE * 2, paint); 163 | 164 | 165 | // 画扑克 166 | if(isComputer) { //如果是电脑 167 | 168 | /*********************画手牌**********************/ 169 | // 卡片背面的Y 170 | int backCardY = y + head.getHeight() + TEXT_SIZE * 2 + GameView.PLAYER_MARGIN; 171 | // 画牌的背面 172 | canvas.drawBitmap(GameView.backCard, x, backCardY, paint); 173 | // 手牌数量的Y 174 | int sizeY = backCardY + GameView.miniCardHeight + GameView.TEXT_SIZE; 175 | //画卡片剩余数 176 | canvas.drawText(""+handCards.size(), x, sizeY, paint); 177 | 178 | 179 | 180 | } else { //如果是玩家自己 181 | 182 | /*********************画手牌**********************/ 183 | //计算手牌宽度 184 | int handCardsWidth = (handCards.size()-1) * GameView.cardMargin + GameView.cardWidth; 185 | //计算手牌开始的X 186 | int handCardStartX = (GameView.winWidth - handCardsWidth) / 2; 187 | //开始画手牌 188 | for (int i = 0; i < handCards.size(); i++) { 189 | int cardX = handCardStartX + i * GameView.cardMargin; //卡片的x 190 | int cardY = GameView.winHeight - GameView.cardHeight - GameView.PLAYER_MARGIN; // 卡片的Y 191 | 192 | Card card = handCards.get(i); 193 | if(willSendCards.contains(card)) { //代表这张卡片被提起来了 194 | cardY -= GameView.PLAYER_MARGIN; //减小Y值 195 | } 196 | Bitmap bigIcon = handCards.get(i).getBigImg(); //卡片 197 | canvas.drawBitmap(bigIcon, cardX, cardY, paint); 198 | } 199 | } 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /src/com/dongluh/ddz/view/GameView.java: -------------------------------------------------------------------------------- 1 | package com.dongluh.ddz.view; 2 | 3 | import java.io.File; 4 | import java.io.FileInputStream; 5 | import java.io.FileNotFoundException; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.util.ArrayList; 10 | import java.util.Collections; 11 | import java.util.List; 12 | 13 | import android.app.Activity; 14 | import android.content.BroadcastReceiver; 15 | import android.content.ComponentName; 16 | import android.content.ContentResolver; 17 | import android.content.Context; 18 | import android.content.Intent; 19 | import android.content.IntentFilter; 20 | import android.content.IntentSender; 21 | import android.content.ServiceConnection; 22 | import android.content.SharedPreferences; 23 | import android.content.IntentSender.SendIntentException; 24 | import android.content.pm.ApplicationInfo; 25 | import android.content.pm.PackageManager; 26 | import android.content.pm.PackageManager.NameNotFoundException; 27 | import android.content.res.AssetManager; 28 | import android.content.res.Resources; 29 | import android.content.res.Resources.Theme; 30 | import android.database.sqlite.SQLiteDatabase; 31 | import android.database.sqlite.SQLiteDatabase.CursorFactory; 32 | import android.graphics.Bitmap; 33 | import android.graphics.Bitmap.Config; 34 | import android.graphics.BitmapFactory; 35 | import android.graphics.Canvas; 36 | import android.graphics.drawable.Drawable; 37 | import android.net.Uri; 38 | import android.os.Bundle; 39 | import android.os.Handler; 40 | import android.os.Looper; 41 | import android.util.AttributeSet; 42 | import android.view.GestureDetector; 43 | import android.view.GestureDetector.OnGestureListener; 44 | import android.view.MotionEvent; 45 | import android.view.GestureDetector.OnDoubleTapListener; 46 | 47 | import com.dongluh.ddz.R; 48 | import com.dongluh.ddz.model.Card; 49 | import com.dongluh.ddz.model.Player; 50 | 51 | /** 52 | * @author Administrator 53 | * 54 | */ 55 | /** 56 | * @author Administrator 57 | * 58 | */ 59 | public class GameView extends BaseView { 60 | 61 | public static Bitmap backCard; // 背景 62 | private Bitmap background; // 牌的背面 63 | 64 | public static final int PLAYER_MARGIN = 10; // 玩家和屏幕边框的距离 65 | 66 | private List players = new ArrayList(); // 3个玩家 67 | private List cards = new ArrayList(); // 一副牌 68 | private List bottomCards = new ArrayList(); // 3张底牌 69 | // private Rect bgRect; 70 | 71 | public static int cardWidth; // 一张牌的宽度 72 | public static int cardHeight; // 一张牌的宽度 73 | public static int pokerWidth; // 整副牌的宽度 74 | public static int pokerHeight; // 整副牌的宽度 75 | 76 | public static int miniCardWidth; // 一张小牌的宽度 77 | public static int miniCardHeight; // 一张小牌的宽度 78 | public static int miniPokerWidth; // 整副小牌的宽度 79 | public static int miniPokerHeight; // 整副小牌的宽度 80 | 81 | public static int cardMargin; // 牌之间的间距 82 | 83 | private Player me; 84 | 85 | public GameView(Context context, AttributeSet attrs) { 86 | super(context, attrs); 87 | 88 | initRes((Activity) context); 89 | 90 | initPlayer(); 91 | 92 | intiCards(); 93 | 94 | startNewGame(); 95 | } 96 | 97 | 98 | GestureDetector detector; 99 | private void initRes(Activity activity) { 100 | background = getBitmap(R.drawable.bg, winWidth, winHeight); 101 | // bgRect = new Rect(0, 0, width, height); 102 | OnGestureListener gestureListener = new OnGestureListener() { 103 | @Override 104 | public boolean onSingleTapUp(MotionEvent e) { 105 | // TODO Auto-generated method stub 106 | return false; 107 | } 108 | 109 | @Override 110 | public void onShowPress(MotionEvent e) { 111 | // TODO Auto-generated method stub 112 | 113 | } 114 | 115 | @Override 116 | public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 117 | float distanceY) { 118 | // TODO Auto-generated method stub 119 | return false; 120 | } 121 | 122 | @Override 123 | public void onLongPress(MotionEvent e) { 124 | // TODO Auto-generated method stub 125 | } 126 | 127 | @Override 128 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 129 | float velocityY) { 130 | // TODO Auto-generated method stub 131 | return false; 132 | } 133 | 134 | @Override 135 | public boolean onDown(MotionEvent e) { 136 | // TODO Auto-generated method stub 137 | return false; 138 | } 139 | }; 140 | detector = new GestureDetector(gestureListener); 141 | detector.setOnDoubleTapListener(this); 142 | } 143 | 144 | private void startNewGame() { 145 | cleanCards(); 146 | distributeCards(); 147 | } 148 | 149 | private void initPlayer() { 150 | // 自己 151 | me = new Player(); 152 | me.setComputer(false); 153 | me.setName("donglu"); 154 | me.setLandlord(true); 155 | me.setScore("0"); 156 | Bitmap myHead = BitmapFactory.decodeResource(getResources(), R.drawable.me); 157 | me.setHead(myHead); 158 | me.setX(PLAYER_MARGIN); 159 | me.setY(winHeight- myHead.getHeight() - TEXT_SIZE - 100); 160 | 161 | // 左边 162 | Player left = new Player(); 163 | left.setComputer(true); 164 | left.setName("马化騰"); 165 | left.setLandlord(false); 166 | left.setScore("0"); 167 | Bitmap leftHead = BitmapFactory.decodeResource(getResources(), R.drawable.left); 168 | left.setHead(leftHead); 169 | left.setX(PLAYER_MARGIN); 170 | left.setY(PLAYER_MARGIN); 171 | 172 | // 右边 173 | Player right = new Player(); 174 | right.setComputer(true); 175 | right.setName("乔布斯"); 176 | right.setLandlord(false); 177 | right.setScore("543210"); 178 | Bitmap rightHead = BitmapFactory.decodeResource(getResources(), R.drawable.right); 179 | right.setHead(rightHead); 180 | right.setX(winWidth - rightHead.getWidth() - PLAYER_MARGIN); 181 | right.setY(PLAYER_MARGIN); 182 | 183 | /** 184 | * 设置下家 185 | */ 186 | me.setNext(right); 187 | right.setNext(left); 188 | left.setNext(me); 189 | 190 | players.add(me); 191 | players.add(left); 192 | players.add(right); 193 | } 194 | 195 | 196 | private void intiCards() { 197 | // 初始化牌的尺寸 198 | cardWidth = winWidth / 13; 199 | cardHeight = winHeight / 6; 200 | pokerWidth = cardWidth * 13; 201 | pokerHeight = cardHeight * 5; 202 | 203 | // 初始化小牌尺寸 204 | miniCardHeight = cardHeight * 3 / 5; 205 | miniCardWidth = cardWidth * 3 / 5; 206 | miniPokerWidth = miniCardWidth * 13; 207 | miniPokerHeight = miniCardHeight * 5; 208 | 209 | cardMargin = cardWidth * 3 / 5; 210 | 211 | // 整副牌 212 | Bitmap poker = getBitmap(R.drawable.poker, pokerWidth, pokerHeight); 213 | // 整副小牌 214 | Bitmap pokerMini = getBitmap(R.drawable.poker_mini, miniPokerWidth, 215 | miniPokerHeight); 216 | // 卡片背面 217 | backCard = Bitmap.createBitmap(pokerMini, 2 * miniCardWidth, 218 | 4 * miniCardHeight, miniCardWidth, miniCardHeight); 219 | 220 | // 把牌切割出来 221 | for (int row = 0; row < 5; row++) { 222 | for (int col = 0; col < 13; col++) { 223 | if (row == 4 && col > 1) { 224 | break; 225 | } 226 | 227 | Bitmap bigCard = Bitmap.createBitmap(poker, col * cardWidth, 228 | row * cardHeight, cardWidth, cardHeight); 229 | 230 | Bitmap miniCard = Bitmap.createBitmap(pokerMini, col 231 | * miniCardWidth, row * miniCardHeight, miniCardWidth, 232 | miniCardHeight); 233 | 234 | Card card = new Card(); 235 | card.setBigImg(bigCard); 236 | card.setSmallImg(miniCard); 237 | card.setColor(row); 238 | card.setValue(col + 1); 239 | 240 | if (col < 2) { 241 | card.setValue(card.getValue() + 13); // 14表示A,15表示2 242 | if (row == 4) { // 大小王 243 | card.setValue(col == 0 ? 17 : 16); // 16表示小王,17表示大王 244 | } 245 | } 246 | cards.add(card); 247 | } 248 | } 249 | } 250 | 251 | /** 252 | * 洗牌 253 | */ 254 | public void cleanCards() { // 随机调换卡片位置 255 | for (int i = 0; i < 100; i++) { 256 | int rdIndex = (int) (Math.random() * cards.size()); 257 | if (rdIndex != 0) { 258 | // 分别取出两张卡片 259 | Card aCard = cards.get(rdIndex); 260 | Card bCard = cards.get(0); 261 | 262 | cards.set(0, aCard); // 交换卡片位置 263 | cards.set(rdIndex, bCard); 264 | } 265 | } 266 | } 267 | 268 | /** 269 | * 发牌 270 | */ 271 | private void distributeCards() { 272 | // 先清理所有玩家的牌 273 | for (Player player : players) { 274 | player.getSentCards().clear(); 275 | player.getHandCards().clear(); 276 | player.getWillSendCards().clear(); 277 | } 278 | 279 | // 给每个玩家17张 280 | for (int i = 0; i < 17; i++) { 281 | for (int j = 0; j < players.size(); j++) { 282 | players.get(j).getHandCards().add(cards.get(3 * i + j)); 283 | } 284 | } 285 | 286 | // 剩下3张作为底牌 287 | bottomCards.clear(); 288 | bottomCards.add(cards.get(51)); 289 | bottomCards.add(cards.get(52)); 290 | bottomCards.add(cards.get(53)); 291 | sortCards(bottomCards); // 对底牌排序 292 | 293 | // 手牌排序 294 | for (Player player : players) { 295 | if (player.isLandlord()) { 296 | // 给底牌给地主 297 | player.getHandCards().addAll(bottomCards); 298 | } 299 | sortCards(player.getHandCards()); 300 | } 301 | } 302 | 303 | private void sortCards(List cards) { 304 | Collections.sort(cards, this); 305 | } 306 | 307 | @Override 308 | protected void render(Canvas canvas) { 309 | // 画背景 310 | canvas.drawBitmap(background, 0, 0, paint); 311 | 312 | // 画玩家 313 | synchronized (players) { 314 | for (Player p : players) { 315 | p.draw(canvas, paint); 316 | } 317 | } 318 | 319 | // 底牌的宽度 320 | int bottomCardWidth = miniCardWidth * bottomCards.size(); 321 | // 底牌开始的X坐标 322 | int bottomCardStartX = (winWidth - bottomCardWidth) / 2; 323 | 324 | for (int i = 0; i < bottomCards.size(); i++) { 325 | //取出小牌 326 | Bitmap miniIcon = bottomCards.get(i).getSmallImg(); 327 | //画小牌 328 | canvas.drawBitmap(miniIcon, bottomCardStartX + i*miniCardWidth, 0, paint); 329 | } 330 | } 331 | 332 | @Override 333 | public boolean onTouchEvent(MotionEvent event) { 334 | float y = event.getY(); 335 | float x = event.getX(); 336 | 337 | if(event.getAction() == MotionEvent.ACTION_DOWN) { 338 | //计算手牌宽度 339 | int handCardsWidth = (me.getHandCards().size()-1) * GameView.cardMargin + GameView.cardWidth; 340 | //计算手牌开始的X 341 | int handCardStartX = (GameView.winWidth - handCardsWidth) / 2; 342 | //看是否在手牌范围内 343 | if ((y >= winHeight - cardHeight - GameView.PLAYER_MARGIN) 344 | && (x >= handCardStartX) 345 | && (x <= winWidth - handCardStartX)) { 346 | 347 | int index = ((int)x - handCardStartX)/cardMargin; 348 | if(index > me.getHandCards().size()-1) { //判断是否越界 349 | index = me.getHandCards().size()-1; 350 | } 351 | 352 | Card card = me.getHandCards().get(index); 353 | 354 | if(me.getWillSendCards().contains(card)) { //说明已经被提起来,现在要放下去 355 | me.getWillSendCards().remove(card); 356 | } else {//说明要提起来 357 | me.getWillSendCards().add(card); 358 | } 359 | } 360 | } 361 | return detector.onTouchEvent(event); 362 | } 363 | 364 | public Bitmap getBitmap(int resId, int w, int h) { 365 | Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); 366 | Canvas canvas = new Canvas(bitmap); 367 | Drawable drawable = getResources().getDrawable(resId); 368 | drawable.setBounds(0, 0, w, h); 369 | drawable.draw(canvas); 370 | return bitmap; 371 | } 372 | 373 | @Override 374 | public boolean onDoubleTap(MotionEvent e) { 375 | System.out.println("onDoubleTap"); 376 | return false; 377 | } 378 | 379 | @Override 380 | public boolean onDoubleTapEvent(MotionEvent e) { 381 | System.out.println("onDoubleTapEvent"); 382 | return false; 383 | } 384 | 385 | @Override 386 | public boolean onSingleTapConfirmed(MotionEvent e) { 387 | System.out.println("onSingleTapConfirmed"); 388 | return false; 389 | } 390 | } 391 | --------------------------------------------------------------------------------