Development of Android 2048 (one) initialization interface from scratch

Source: Internet
Author: User

Self-study Android more than one months, has been working outside the fragmented to see something. Feel often use of things have some understanding, but a beginning to write code always out of various wonderful problems. It still feels like the code is too small to write. It is too slow to continue to study in a messy way, and to learn a little bit less, too inefficient. So start today. I'm going to actually do some small programs. In the development of continuous learning it.

Just recently there is a game on Android 2048 more fire, so the practiced hand it.

Because of Android has not too deep understanding, so I write things will be more basic, so need to see some high-level development of friends can bypass, but also hope to have a master to give me some guidance and advice, here first thank you.

Also in the course of learning to participate in a very many users before writing the sample (too much content can not be enumerated), so the code is not completely original thinking, but I wrote it, there will be a lot of problems, I hope you correct.


Needless to say, the next step is I'm trying to develop some records of Android version 2048.

Today is the first day, I mainly realize the layout of the game 4*4 interface. and generates 2 or 4 at random locations when the program is initialized.

Layout files primarily use GridLayout, which makes it very easy to implement interfaces like table in Web development. For example, I am in very diverse examples, such as virtual keyboards, calendars and so many are used GridLayout layout. Other than that. GridLayout is provided after the 4.0 version, assuming that the previous version number needs to be introduced into the other package (ask the mother to do so).

First, we have to prepare several images, corresponding to the number 21 until 2048 (assuming that many others can do a few more analogies 4096 or higher). Because I am not good at the art. So I took a few pictures from someone else's program on a temporary basis.

Next look at the code. The gaze is written more specifically (albeit not standardized). So I'm not going to say much.

Activity:

Package Com.example.t2048;import Java.util.arraylist;import Java.util.list;import java.util.random;import Android.app.activity;import Android.os.bundle;import Android.view.menu;import Android.view.View;import Android.widget.gridlayout;import Android.widget.imageview;public class Mainactivity extends Activity {GridLayout GridLayout = null;//The position used to hold the space list<integer> spacelist = new arraylist<integer> ();//used to hold a position with a number lattice list< integer> stufflist = new arraylist<integer> ()/** * icon Array */private final int[] icons = {r.drawable.but_empty, R. Drawable.but2,r.drawable.but4, R.drawable.but8, R.drawable.but16,r.drawable.but32, R.drawable.but64, r.drawable.but128,r.drawable.but256, r.drawable.but512, r.drawable.but1024,r.drawable.but2048, R.drawable.but4096 };p rotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); gridLayout = (gridLayout) Findviewbyid (R.ID.GRIDLAYOUT1); init ();} Initialize interface public void init () {//First in 16 individualTypes are filled with blank images for (int i=0;i<16;i++) {View view = View.inflate (this, r.layout.item, null); ImageView image = (ImageView) View . Findviewbyid (R.id.image); Image.setbackgroundresource (Icons[0]); Spacelist.add (i); Gridlayout.addview (view);} Randomly add two x 2 or 4addRandomItem () to the interface, Addrandomitem ();} Randomly get the location public int Getrandomindex () {Random random = new random () from the empty column table, and if (Spacelist.size () >0) return Random.nextint (Spacelist.size ()); else return-1;} Randomly increases the number 2public void Addrandomitem () {int index = Getrandomindex () in the blank lattice, if (index!=-1) {//To get the corresponding Viewview view = Gridlayout.getchildat (Spacelist.get (index)); ImageView image = (ImageView) View.findviewbyid (r.id.image);// Randomly generated numbers 1 or 2int i = (int) math.round (math.random () +1);//replace the current lattice picture with 2 or 4image.setbackgroundresource (Icons[i]);// Remove the lattice//spacelist.remove (Spacelist.indexof (index)) from the blank list;     The line of code is wrong, resulting in the appendix of the Problem Spacelist.remove (index); Corrected Code}}public Boolean oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar if it is P Resent.getmenuinflAter (). Inflate (R.menu.main, menu); return true;}} 



Activity_main.xml

<gridlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:id=" @+id/gridlayout1 "    android:background=" #708090 "    android:o rientation= "vertical"    android:layout_width= "match_parent"    android:layout_height= "Match_parent    " android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_horizontal_ Margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen/activity _vertical_margin "    android:rowcount=" 4 "    android:columncount=" 4 "    android:padding=" 20DP "       > </GridLayout>


Item.xml
<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/framelayout1 "    android:layout_width=" match_parent "    android:layout_height= "Match_parent" >    <imageview        android:id= "@+id/image"        android:layout_width= "Fill_parent        " android:layout_height= "Fill_parent"        android:padding= "3DP"/></framelayout>

The code is so much. Overall is relatively simple, of course, I am now only consider the implementation. No question of efficiency is taken into account.

The following is the execution interface



The interface is relatively simple, but the main layout we have achieved. In the future I will continue to add, to make this more complete.


Report:

The code now executes, occasionally with an error. I checked it out a bit. Temporarily did not find the problem where, if you know where there is a problem, please tell me a message. Of course, suppose I find a problem. will also be updated in this blog post.

The following is the error log when:



Solution to the problem:

I followed the code last night and found that it was a mistake.

The problem is in the 81st row of activity

Spacelist.remove (Spacelist.indexof (index));

Remove should pass in index. I accidentally wrote the value of index accordingly.

Spacelist.remove (index);

After changing to this, I tested it, no problem. I've changed it in the code above



Development of Android 2048 (one) initialization interface from scratch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.