近期工作比較忙,所以更新的慢了一点,今天的主要内容是关于Android版2048的逻辑推断,经过本篇的解说,基本上完毕了这个游戏的主体部分。

首先还是看一下,我在实现2048时用到的一些存储的数据结构。

我在实现时,为了省事存储游戏过程中的变量主要用到的是List。

比方说:List<Integer> spaceList = new ArrayList<Integer>();这个spaceList主要用于保存。全部空白格的位置,也就是空白格在GridLayout中的位置(从0到15)

对于数字格,以及格子相应的数据。我写了一个类例如以下:

  1. package com.example.t2048;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import android.util.Log;
  7.  
  8. /**
  9. * 用于保存数字格,已经数字格相应的数字
  10. * @author Mr.Wang
  11. *
  12. */
  13. public class NumberList {
  14.  
  15. //这个list用于保存全部不为空的格子的坐标(在GridLayout中的位置从0到15)
  16. private List<Integer> stuffList = new ArrayList<Integer>();
  17. //这个list用于保存全部不为空的格子相应的数字(以2为底数的指数)
  18. private List<Integer> numberList = new ArrayList<Integer>();
  19.  
  20. /**
  21. * 新增加的数字格
  22. * @param index 数字格相应的位置
  23. * @param number 相应数字的指数(以2为底数)
  24. */
  25. public void add(int index, int number){
  26. stuffList.add(index);
  27. numberList.add(number);
  28. }
  29.  
  30. /**
  31. * 用于推断当前位置是否为数字格
  32. * @param index 当前位置
  33. * @return true表示是
  34. */
  35. public boolean contains(int index){
  36. return stuffList.contains(index);
  37. }
  38.  
  39. /**
  40. * 将当前的格子从数字列表中去掉
  41. * @param index
  42. */
  43. public void remove(int index){
  44. int order = stuffList.indexOf(index);
  45. numberList.remove(order);
  46. stuffList.remove(order);
  47. }
  48.  
  49. /**
  50. * 将当前格子相应的数字升级,指数加1
  51. * @param index
  52. */
  53. public void levelup(int index){
  54. int order = stuffList.indexOf(index);
  55. numberList.set(order, numberList.get(order)+1);
  56. }
  57.  
  58. /**
  59. * 将当前格子相应的位置置换为新的位置
  60. * @param index 当前位置
  61. * @param newIndex 新的位置
  62. */
  63. public void changeIndex(int index, int newIndex){
  64. stuffList.set(stuffList.indexOf(index), newIndex);
  65. }
  66.  
  67. /**
  68. * 通过格子相应的位置获取其相应的数字
  69. * @param index 当前位置
  70. * @return 格子相应数字的指数
  71. */
  72. public int getNumberByIndex(int index){
  73. int order = stuffList.indexOf(index);
  74. return numberList.get(order) ;
  75. }
  76.  
  77. public String toString(){
  78. return stuffList.toString()+numberList.toString();
  79. }
  80.  
  81. public void printLog(){
  82. Log.i("stuffList", stuffList.toString());
  83. Log.i("numberList", numberList.toString());
  84. }
  85. }

这个类主要是我对数字格、数字格相应数字的保存,和增删改等操作。

事实上就是两个list,我为了操作起来方便,所以把他们写在一个类里。

然后,我们来讲一下这个游戏的逻辑。

比方,我们在游戏过程中运行了一次向右滑动的操作,在这个操作中,我们要对全部能够移动和合并的格子进行推断和对应的操作:

1、数字格的右边假设是空白格。则数字格与空白格交换

2、数字格右边假设有多个空白格,则数字格与连续的最后一个空白格做交换

3、数字格的右边假设存在与之同样的数字格,则本格置空。右边的数字格升级(指数加一)

4、假设滑动方向连续存在多个同样的数字格,右的格子优先升级

5、在一次滑动中,每一个格子最多升级一次

当一个格子存在上述前四种中的随意一种时,则完毕了对它的操作

我们试着把上面的推断规则翻译成代码,首先,明白在GridLayout中的坐标位置。我在GridLayout中採用的是水平布局,所以每一个格子相应的位置例如以下

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhcGlubm9uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

在这个基础上,我建立例如以下的坐标轴,以左上角为原点,x轴为横轴,y轴为竖轴:

当向右滑动的时候,从上面逻辑来看,为了方便,我们应当从右向左遍历格子

  1. for(int y=0;y<4;y++){
  2. for(int x=2;x>=0;x--){
  3. int thisIdx = 4*y +x;
  4. Change(thisIdx,direction);
  5. }
  6. }

每遍历到一个新的格子,运行一次change()方法。事实上应该是每遍历到一个非空的格子。运行一次change()可是我为了省事,把非空推断加到了change的代码里。我们看一下change()这种方法的实现。这种方法主要是用来推断,一个格子是须要移动、合并,还是什么都不操作。:

  1. /**
  2. * 该方法,为每一个符合条件的格子运行变动的操作。如置换,升级等
  3. * @param thisIdx 当前格子的坐标
  4. * @param direction 滑动方向
  5. */
  6. public void Change(int thisIdx,int direction){
  7. if(numberList.contains(thisIdx)){
  8.  
  9. int nextIdx = getLast(thisIdx, direction);
  10. if(nextIdx == thisIdx){
  11. //不能移动
  12. return;
  13. }else if(spaceList.contains(nextIdx)){
  14. //存在能够置换的空白格
  15. replace(thisIdx,nextIdx);
  16. }else{
  17. if(numberList.getNumberByIndex(thisIdx) == numberList.getNumberByIndex(nextIdx)){
  18. //能够合并
  19. levelup(thisIdx, nextIdx);
  20. }else{
  21. int before = getBefore(nextIdx, direction);
  22. if(before != thisIdx){
  23. //存在能够置换的空白格
  24. replace(thisIdx,before);
  25. }
  26. }
  27. }
  28. }
  29. }

当中getLast()方法,用于获取当前格子在移动方向的能够移动或者合并的最后一个格子,假设返回值还是当前的格子,则表示不能移动。当中调用的getNext()方法是为了获取当前格子在移动方向的下个格子的位置。

  1. /**
  2. * 用于获取移动方向上最后一个空白格之后的位置
  3. * @param index 当前格子的坐标
  4. * @param direction 移动方向
  5. * @return
  6. */
  7. public int getLast(int thisIdx, int direction){
  8. int nextIdx = getNext(thisIdx, direction);
  9. if(nextIdx < 0)
  10. return thisIdx;
  11. else{
  12. if(spaceList.contains(nextIdx))
  13. return getLast(nextIdx, direction);
  14. else
  15. return nextIdx;
  16. }
  17. }

然后是replace(int thisIdx, int nextIdx),这种方法是运行两个格子互换位置。内容主要是对两个格子中的view更换背景图片,然后操作空白格的list和数字格的list:

  1. /**
  2. * 该方法用来交换当前格与目标空白格的位置
  3. * @param thisIdx 当前格子的坐标
  4. * @param nextIdx 目标空白格的坐标
  5. */
  6. public void replace(int thisIdx, int nextIdx){
  7. moved = true;
  8. //获取当前格子的view,并将其置成空白格
  9. View thisView = gridLayout.getChildAt(thisIdx);
  10. ImageView image = (ImageView) thisView.findViewById(R.id.image);
  11. image.setBackgroundResource(icons[0]);
  12.  
  13. //获取空白格的view,并将其背景置成当前格的背景
  14. View nextView = gridLayout.getChildAt(nextIdx);
  15. ImageView nextImage = (ImageView) nextView.findViewById(R.id.image);
  16. nextImage.setBackgroundResource(icons[numberList.getNumberByIndex(thisIdx)]);
  17.  
  18. //在空白格列表中。去掉目标格。加上当前格
  19. spaceList.remove(spaceList.indexOf(nextIdx));
  20. spaceList.add(thisIdx);
  21.  
  22. //在数字格列表中,当前格的坐标置换成目标格的坐标
  23. numberList.changeIndex(thisIdx, nextIdx);
  24. }

levelup(int thisIdx, int nextIdx)这种方法是为了实现同样数字格的合并操作。事实上就是将当前的格子置成空白格。将移动方向上下一个格子相应的背景置成下一个背景:

  1. /**
  2. * 刚方法用于合并在移动方向上两个同样的格子
  3. * @param thisIdx 当前格子的坐标
  4. * @param nextIdx 目标格子的坐标
  5. */
  6. public void levelup(int thisIdx, int nextIdx){
  7.  
  8. //一次移动中,每一个格子最多仅仅能升级一次
  9. if(!changeList.contains(nextIdx)){
  10. moved = true;
  11. //获取当前格子的view,并将其置成空白格
  12. View thisView = gridLayout.getChildAt(thisIdx);
  13. ImageView image = (ImageView) thisView.findViewById(R.id.image);
  14. image.setBackgroundResource(icons[0]);
  15.  
  16. //获取目标格的view,并将其背景置成当前格升级后的背景
  17. View nextView = gridLayout.getChildAt(nextIdx);
  18. ImageView nextImage = (ImageView) nextView.findViewById(R.id.image);
  19. nextImage.setBackgroundResource(icons[numberList.getNumberByIndex(nextIdx)+1]);
  20.  
  21. //在空白格列表中增加当前格
  22. spaceList.add(thisIdx);
  23. //在数字列中删掉第一个格子
  24. numberList.remove(thisIdx);
  25. //将数字列表相应的内容升级
  26. numberList.levelup(nextIdx);
  27.  
  28. changeList.add(nextIdx);
  29. }
  30.  
  31. }

写完这些,基本完毕了基本的推断,可是还有两个问题:1是假设每次滑动没有格子移动(合并),那么就不应该新随机生成格子;2每一个格子仅仅能合并一次。

为解决这两个问题,我又加了两个变量

  1. //用于保存每次操作时。已经升级过的格子
  2. List<Integer> changeList = new ArrayList<Integer>();
  3.  
  4. //用于表示本次滑动是否有格子移动过
  5. boolean moved = false;

当中changeList在每次滑动前清空,然后增加本次移动中发生过合并的格子,在每次合并的推断时首先看看要合并的格子是不是在这个list中。假设在的话,说明已经合并过。那么就不运行合并的操作了。

还有个波尔型的moved变量,这个也是在每次滑动前置为false,假设在本次滑动中,有格子移动或者合并,就置为ture,在滑动的最后。通过这个变量推断是否要随机生产新的格子。

以下是完整的Activity中的代码:

  1. package com.example.t2048;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import android.app.Activity;
  8. import android.os.Bundle;
  9. import android.view.GestureDetector;
  10. import android.view.GestureDetector.OnGestureListener;
  11. import android.view.Menu;
  12. import android.view.MotionEvent;
  13. import android.view.View;
  14. import android.view.View.OnTouchListener;
  15. import android.widget.GridLayout;
  16. import android.widget.ImageView;
  17.  
  18. public class MainActivity extends Activity {
  19.  
  20. final static int LEFT = -1;
  21. final static int RIGHT = 1;
  22. final static int UP = -4;
  23. final static int DOWN = 4;
  24.  
  25. GridLayout gridLayout = null;
  26.  
  27. //用于保存空格的位置
  28. List<Integer> spaceList = new ArrayList<Integer>();
  29.  
  30. //全部非空的格子
  31. NumberList numberList = new NumberList();
  32.  
  33. //用于保存每次操作时,已经升级过的格子
  34. List<Integer> changeList = new ArrayList<Integer>();
  35.  
  36. //用于表示本次滑动是否有格子移动过
  37. boolean moved = false;
  38.  
  39. GestureDetector gd = null;
  40.  
  41. /**
  42. * 图标数组
  43. */
  44. private final int[] icons = { R.drawable.but_empty, R.drawable.but2,
  45. R.drawable.but4, R.drawable.but8, R.drawable.but16,
  46. R.drawable.but32, R.drawable.but64, R.drawable.but128,
  47. R.drawable.but256, R.drawable.but512, R.drawable.but1024,
  48. R.drawable.but2048, R.drawable.but4096 };
  49.  
  50. protected void onCreate(Bundle savedInstanceState) {
  51. System.out.println("程序启动");
  52. super.onCreate(savedInstanceState);
  53. setContentView(R.layout.activity_main);
  54.  
  55. gridLayout = (GridLayout) findViewById(R.id.GridLayout1);
  56.  
  57. init();
  58.  
  59. MygestureDetector mg = new MygestureDetector();
  60.  
  61. gd = new GestureDetector(mg);
  62. gridLayout.setOnTouchListener(mg);
  63. gridLayout.setLongClickable(true);
  64. }
  65.  
  66. //初始化界面
  67. public void init(){
  68. System.out.println("初始化");
  69.  
  70. //首先在16个各种都填上空白的图片
  71. for(int i=0;i<16;i++){
  72. View view = View.inflate(this, R.layout.item, null);
  73. ImageView image = (ImageView) view.findViewById(R.id.image);
  74.  
  75. image.setBackgroundResource(icons[0]);
  76. spaceList.add(i);
  77. gridLayout.addView(view);
  78. }
  79.  
  80. //在界面中随机增加两个2或者4
  81. addRandomItem();
  82. addRandomItem();
  83. }
  84.  
  85. //从空格列表中随机获取位置
  86. public int getRandomIndex(){
  87. Random random = new Random();
  88. if(spaceList.size()>0)
  89. return random.nextInt(spaceList.size());
  90. else
  91. return -1;
  92. }
  93.  
  94. //在空白格中随机增加数字2或4
  95. public void addRandomItem(){
  96. int index = getRandomIndex();
  97. if(index!=-1){
  98. System.out.println("随机生成数字 位置"+spaceList.get(index));
  99. //获取相应坐标所相应的View
  100. View view = gridLayout.getChildAt(spaceList.get(index));
  101. ImageView image = (ImageView) view.findViewById(R.id.image);
  102. //随机生成数字1或2
  103. int i = (int) Math.round(Math.random()+1);
  104. //将当前格子的图片置换为2或者4
  105. image.setBackgroundResource(icons[i]);
  106.  
  107. //在numList中增加该格子的信息
  108. numberList.add(spaceList.get(index), i);
  109.  
  110. //在空白列表中去掉这个格子
  111. spaceList.remove(index);
  112.  
  113. }
  114. }
  115.  
  116. public class MygestureDetector implements OnGestureListener,OnTouchListener{
  117.  
  118. @Override
  119. public boolean onTouch(View v, MotionEvent event) {
  120. // TODO Auto-generated method stub
  121. return gd.onTouchEvent(event);
  122. }
  123.  
  124. @Override
  125. public boolean onDown(MotionEvent e) {
  126. // TODO Auto-generated method stub
  127. return false;
  128. }
  129.  
  130. @Override
  131. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  132. float velocityY) {
  133.  
  134. // 參数解释:
  135. // e1:第1个ACTION_DOWN MotionEvent
  136. // e2:最后一个ACTION_MOVE MotionEvent
  137. // velocityX:X轴上的移动速度,像素/秒
  138. // velocityY:Y轴上的移动速度。像素/秒
  139.  
  140. // 触发条件 :
  141. // X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒
  142.  
  143. if(e1.getX()-e2.getX()>100){
  144. System.out.println("向左");
  145. move(LEFT);
  146. return true;
  147. }else if(e1.getX()-e2.getX()<-100){
  148. System.out.println("向右");
  149. move(RIGHT);
  150. return true;
  151. }else if(e1.getY()-e2.getY()>100){
  152. System.out.println("向上");
  153. move(UP);
  154. return true;
  155. }else if(e1.getY()-e2.getY()<-00){
  156. System.out.println("向下");
  157. move(DOWN);
  158. return true;
  159. }
  160. return false;
  161. }
  162.  
  163. @Override
  164. public void onLongPress(MotionEvent e) {
  165. // TODO Auto-generated method stub
  166.  
  167. }
  168.  
  169. @Override
  170. public boolean onScroll(MotionEvent e1, MotionEvent e2,
  171. float distanceX, float distanceY) {
  172. // TODO Auto-generated method stub
  173. return false;
  174. }
  175.  
  176. @Override
  177. public void onShowPress(MotionEvent e) {
  178. // TODO Auto-generated method stub
  179.  
  180. }
  181.  
  182. @Override
  183. public boolean onSingleTapUp(MotionEvent e) {
  184. // TODO Auto-generated method stub
  185. return false;
  186. }
  187.  
  188. }
  189.  
  190. /**
  191. * 用于获取移动方向上下一个格子的位置
  192. * @param index 当前格子的位置
  193. * @param direction 滑动方向
  194. * @return 假设在边界在返回-1
  195. */
  196. public int getNext(int index,int direction){
  197.  
  198. int y = index/4;
  199. int x = index%4;
  200.  
  201. if(x==3 && direction==RIGHT)
  202. return -1;
  203. if(x==0 && direction==LEFT)
  204. return -1;
  205. if(y==0 && direction==UP)
  206. return -1;
  207. if(y==3 && direction==DOWN)
  208. return -1;
  209. return index+direction;
  210. }
  211.  
  212. /**
  213. * 用于获取移动方向上前一个格子的位置
  214. * @param index 当前格子的位置
  215. * @param direction 滑动方向
  216. * @return 假设在边界在返回-1
  217. */
  218. public int getBefore(int index,int direction){
  219.  
  220. int y = index/4;
  221. int x = index%4;
  222.  
  223. if(x==0 && direction==RIGHT)
  224. return -1;
  225. if(x==3 && direction==LEFT)
  226. return -1;
  227. if(y==3 && direction==UP)
  228. return -1;
  229. if(y==0 && direction==DOWN)
  230. return -1;
  231. return index-direction;
  232. }
  233.  
  234. /**
  235. * 该方法用来交换当前格与目标空白格的位置
  236. * @param thisIdx 当前格子的坐标
  237. * @param nextIdx 目标空白格的坐标
  238. */
  239. public void replace(int thisIdx, int nextIdx){
  240. moved = true;
  241. //获取当前格子的view,并将其置成空白格
  242. View thisView = gridLayout.getChildAt(thisIdx);
  243. ImageView image = (ImageView) thisView.findViewById(R.id.image);
  244. image.setBackgroundResource(icons[0]);
  245.  
  246. //获取空白格的view,并将其背景置成当前格的背景
  247. View nextView = gridLayout.getChildAt(nextIdx);
  248. ImageView nextImage = (ImageView) nextView.findViewById(R.id.image);
  249. nextImage.setBackgroundResource(icons[numberList.getNumberByIndex(thisIdx)]);
  250.  
  251. //在空白格列表中,去掉目标格。加上当前格
  252. spaceList.remove(spaceList.indexOf(nextIdx));
  253. spaceList.add(thisIdx);
  254.  
  255. //在数字格列表中,当前格的坐标置换成目标格的坐标
  256. numberList.changeIndex(thisIdx, nextIdx);
  257. }
  258.  
  259. /**
  260. * 刚方法用于合并在移动方向上两个同样的格子
  261. * @param thisIdx 当前格子的坐标
  262. * @param nextIdx 目标格子的坐标
  263. */
  264. public void levelup(int thisIdx, int nextIdx){
  265.  
  266. //一次移动中。每一个格子最多仅仅能升级一次
  267. if(!changeList.contains(nextIdx)){
  268. moved = true;
  269. //获取当前格子的view,并将其置成空白格
  270. View thisView = gridLayout.getChildAt(thisIdx);
  271. ImageView image = (ImageView) thisView.findViewById(R.id.image);
  272. image.setBackgroundResource(icons[0]);
  273.  
  274. //获取目标格的view,并将其背景置成当前格升级后的背景
  275. View nextView = gridLayout.getChildAt(nextIdx);
  276. ImageView nextImage = (ImageView) nextView.findViewById(R.id.image);
  277. nextImage.setBackgroundResource(icons[numberList.getNumberByIndex(nextIdx)+1]);
  278.  
  279. //在空白格列表中增加当前格
  280. spaceList.add(thisIdx);
  281. //在数字列中删掉第一个格子
  282. numberList.remove(thisIdx);
  283. //将数字列表相应的内容升级
  284. numberList.levelup(nextIdx);
  285.  
  286. changeList.add(nextIdx);
  287. }
  288.  
  289. }
  290.  
  291. /**
  292. * 该方法为不同的滑动方向。运行不同的遍历顺序
  293. * @param direction 滑动方向
  294. */
  295. public void move(int direction){
  296.  
  297. moved = false;
  298.  
  299. changeList.clear();
  300.  
  301. numberList.printLog();
  302.  
  303. switch(direction){
  304. case RIGHT:
  305. for(int y=0;y<4;y++){
  306. for(int x=2;x>=0;x--){
  307. int thisIdx = 4*y +x;
  308. Change(thisIdx,direction);
  309. }
  310. }
  311. break;
  312. case LEFT:
  313. for(int y=0;y<4;y++){
  314. for(int x=1;x<=3;x++){
  315. int thisIdx = 4*y +x;
  316. Change(thisIdx,direction);
  317. }
  318. }
  319. break;
  320. case UP:
  321. for(int x=0;x<4;x++){
  322. for(int y=1;y<=3;y++){
  323. int thisIdx = 4*y +x;
  324. Change(thisIdx,direction);
  325. }
  326. }
  327. break;
  328. case DOWN:
  329. for(int x=0;x<4;x++){
  330. for(int y=2;y>=0;y--){
  331. int thisIdx = 4*y +x;
  332. Change(thisIdx,direction);
  333. }
  334. }
  335. break;
  336. }
  337.  
  338. //假设本次滑动有格子移动过,则随机填充新的格子
  339. if(moved)
  340. addRandomItem();
  341.  
  342. }
  343.  
  344. /**
  345. * 该方法。为每一个符合条件的格子运行变动的操作,如置换,升级等
  346. * @param thisIdx 当前格子的坐标
  347. * @param direction 滑动方向
  348. */
  349. public void Change(int thisIdx,int direction){
  350. if(numberList.contains(thisIdx)){
  351.  
  352. int nextIdx = getLast(thisIdx, direction);
  353. if(nextIdx == thisIdx){
  354. //不能移动
  355. return;
  356. }else if(spaceList.contains(nextIdx)){
  357. //存在能够置换的空白格
  358. replace(thisIdx,nextIdx);
  359. }else{
  360. if(numberList.getNumberByIndex(thisIdx) == numberList.getNumberByIndex(nextIdx)){
  361. //能够合并
  362. levelup(thisIdx, nextIdx);
  363. }else{
  364. int before = getBefore(nextIdx, direction);
  365. if(before != thisIdx){
  366. //存在能够置换的空白格
  367. replace(thisIdx,before);
  368. }
  369. }
  370. }
  371. }
  372. }
  373.  
  374. /**
  375. * 用于获取移动方向上最后一个空白格之后的位置
  376. * @param index 当前格子的坐标
  377. * @param direction 移动方向
  378. * @return
  379. */
  380. public int getLast(int thisIdx, int direction){
  381. int nextIdx = getNext(thisIdx, direction);
  382. if(nextIdx < 0)
  383. return thisIdx;
  384. else{
  385. if(spaceList.contains(nextIdx))
  386. return getLast(nextIdx, direction);
  387. else
  388. return nextIdx;
  389. }
  390. }
  391.  
  392. public boolean onCreateOptionsMenu(Menu menu) {
  393. // Inflate the menu; this adds items to the action bar if it is present.
  394. getMenuInflater().inflate(R.menu.main, menu);
  395. return true;
  396. }
  397.  
  398. }

写到这里,做为我学习Android以来。第一个自己写的程序已经完毕一半了。

逻辑推断这部分写的时候,还是费了一点时间,由于总有一些情况没有考虑进来,到如今基本上已经实现了。可是也反应出来一个非常重要的问题。那就是自己在数据结构和算法方面还是非常薄弱。整个读一下自己写的代码,为了完毕对各种情况的推断。整个代码看起来十分冗余。并且效率之低就更不用说了。再看看别人写的代码。感觉自己在开发方面还是有非常长的路要走的。

接下来的时间,我会利用工作之余的时间不断去完好这个程序,并尽可能的去优化。

大家共勉吧!

代码写成这样,我也不藏拙了,我把代码打包上传了,须要的朋友能够下载,也希望大家多多指正

下载地址http://download.csdn.net/detail/johnsonwce/7269315

从零開始开发Android版2048 (三)逻辑推断的更多相关文章

  1. 从零開始开发Android版2048 (一)初始化界面

    自学Android一个月多了,一直在工作之余零零散散地看一些东西.感觉经常使用的东西都有些了解了,可是一開始写代码总会出各种奇葩的问题.感觉还是代码写得太少.这样继续杂乱地学习下去进度也太慢了,并且学 ...

  2. 从零開始开发Android版2048 (二)获取手势信息

    今天是尝试開始Android版2048小游戏的第二天.在今天,我主要学习了怎样获取用户在屏幕滑动的手势,以及对布局进行了一些小小的完好. 获取用户操作的手势(比方向左滑.向右滑等)主要用到了Gestu ...

  3. 从零開始开发Android版2048 (四) 分数、重置、结束

    这一篇的内容主要是在上一篇的基础上,增加分数计算(包含当前分数和最高分数).游戏结束的推断以及游戏界面的重置这三个部分的功能. 一.分数的计算和保存          首先,2048这个游戏的分数包含 ...

  4. 从零開始开发Android版2048 (五) 撤销的实现

    本篇的内容是,在前一篇的基础上添�了撤销的功能.撤销事实上就是将当前的用户界面恢复到这次滑动值前的样子.我实现撤销的主要原理是,将每次滑动后界面上的格子和相应的数字记录下来,当然还有分数,把这些数据写 ...

  5. Bmob移动后端云服务平台--Android从零開始--(二)android高速入门

    Bmob移动后端云服务平台--Android从零開始--(二)android高速入门 上一篇博文我们简介何为Bmob移动后端服务平台,以及其相关功能和优势. 本文将利用Bmob高速实现简单样例,进一步 ...

  6. 从零開始学android&lt;TabHost标签组件.二十九.&gt;

    TabHost主要特点是能够在一个窗体中显示多组标签栏的内容,在Android系统之中每一个标签栏就称为一个Tab.而包括这多个标签栏的容器就将其称为TabHost.TabHost类的继承结构例如以下 ...

  7. 第13章、布局Layouts之RelativeLayout相对布局(从零開始学Android)

    RelativeLayout相对布局 RelativeLayout是一种相对布局,控件的位置是依照相对位置来计算的,后一个控件在什么位置依赖于前一个控件的基本位置,是布局最经常使用,也是最灵活的一种布 ...

  8. 从零開始学android&lt;SeekBar滑动组件.二十二.&gt;

    拖动条能够由用户自己进行手工的调节,比如:当用户须要调整播放器音量或者是电影的播放进度时都会使用到拖动条,SeekBar类的定义结构例如以下所看到的: java.lang.Object    ↳ an ...

  9. 从零開始学android&lt;数据存储(1)SharedPreferences属性文件.三十五.&gt;

    在android中有五种保存数据的方法.各自是: Shared Preferences Store private primitive data in key-value pairs. 相应属性的键值 ...

随机推荐

  1. No module named '_Sqlite3' 解决方法

    今晚,在学习Python的时候,(学习链接:http://yidao620c.github.io/blog/20150420/simpleblog-01.html(搭载自己的博客案例)): 想为系统生 ...

  2. 每日一刷(2018多校水题+2016icpc水题)

    11.9 线段树 http://acm.hdu.edu.cn/showproblem.php?pid=6315 求逆序对个数 http://acm.hdu.edu.cn/showproblem.php ...

  3. Linux命令之rlogin

    rlogin [-8EKLdx] [-e char] [-l username] host rlogin在远程主机host上开始一个终端会话. (1).选项 -8 选项允许进行8位的输入数据传送:否则 ...

  4. cogs 1075. [省常中2011S4] 最短路径问题

    1075. [省常中2011S4] 最短路径问题 ★   输入文件:short.in   输出文件:short.out   简单对比 时间限制:1 s   内存限制:128 MB [问题描述] 平面上 ...

  5. 【动态规划技巧题】POJ2229-Sumsets

    [题目大意] 把一个数n分成2的指数幂相加的形式,问有几种情况. [思路] 如果当前i为奇数,则必定有至少一个1,可以看作i-1的情形再加上一个1.即f[i]=f[i-1]. 如果当前i为偶数,假设没 ...

  6. 关于Mysort实验的补发博客

    关于本次课后的一些话 关于这次课上的关于sort -nk 2 -t: sort.txt的实验没能在课上做出,有自身的知识不够,没能灵活运用所学知识,以及在当时课上走了会神,回过头来已经不知道该干些什么 ...

  7. 浙南联合训练赛 D - Broken Clock

    You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM fo ...

  8. React.js学习知识小结(一)

    学习React也有半个月了吧,这里对所学的基础知识做个简单的总结.自己先是跟着官方文档学,差不多学了四五天,也跟着入门教程做了一个简单的小栗子.然后跟着阮一峰老师的教程上手了几个小Demo,后来在网上 ...

  9. insert失败自动执行update(duplicate先insert)

    例如:有一张表 字段有  id主键自增,或者唯一索引:datetime时间  name名字 INSERT INTO TABLE (id,datetime) VALUES (1,1440000000), ...

  10. Hiho---欧拉图

    欧拉路·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最后的宝藏.现 ...