Android 2048 (1) initialization interface developed from scratch

Source: Internet
Author: User

I have learned Android for more than a month. I have been reading some things out of work, and I feel a little familiar with common things. But at the beginning, there will always be a variety of amazing problems when I write code, I still feel that the code is too little written, so it is too slow to continue learning in disorder, and it is too inefficient to learn a little bit. So from today on, I plan to do some small programs and keep learning in development.

Recently, there was a game in Android 2048, which is quite popular.

Because I do not have a deep understanding of Android, all the things I write are relatively basic, so I need to see some friends of advanced development who can bypass it, I also hope that some experts will give me some guidance and suggestions. Thank you first.

I also referred to the examples previously written by many netizens during the learning process (there are too many references to list them one by one), so the code ideas are not completely original, but they are all written by myself, there will certainly be many problems. I hope you can correct them.


I am trying to develop some records for Android 2048.

Today is the first day. I mainly implement the layout of the Game 4*4 interface and generate 2 or 4 at random positions during program initialization. The layout file mainly uses GridLayout. Using this layout can easily implement interfaces like tables in web development. For example, many examples, such as virtual keyboards and calendars, use GridLayout layout. In addition, GridLayout is provided after version 4.0. To run GridLayout in a previous version, you need to introduce other packages (qiniang ).

First, we have to prepare several images, which correspond to numbers 2 to 2048 respectively (if you want more images, you can make a few more images, such as 4096 or higher ). Because I am not good at artists, I have taken some pictures from others' programs for the time being.

Next, let's take a look at the code and write comments in detail (although not standardized ).

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; // List of locations used to save spaces <Integer> spaceList = new ArrayList <Integer> (); // Stores the List of locations with numeric cells <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}; protected void onCreate (Bundle savedInstanceState) {Super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); gridLayout = (GridLayout) findViewById (R. id. gridLayout1); init () ;}// initialize the public void init () {// fill in the blank image 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 );}/ /Add two 2 or 4 addRandomItem ();} // randomly retrieve the position public int getRandomIndex () from the Space list () {Random random = new Random (); if (spaceList. size ()> 0) return random. nextInt (spaceList. size (); else return-1;} // Add the number 2 public void addRandomItem () {int index = getRandomIndex (); if (index! =-1) {// obtain the ViewView view corresponding to the corresponding coordinate = gridLayout. getChildAt (spaceList. get (index); ImageView image = (ImageView) view. findViewById (R. id. image); // randomly generate numbers 1 or 2int I = (int) Math. round (Math. random () + 1); // Replace the current image with 2 or 4 images. setBackgroundResource (icons [I]); // remove this lattice from the blank list // spaceList. remove (spaceList. indexOf (index); this line of code is written incorrectly, leading to the spaceList in the appendix. remove (index); // corrected code} public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. 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:orientation="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>

There are so many codes, and the overall process is relatively simple. Of course, I only want to implement the Code first, without considering the efficiency.

Below is the running interface



The interface is relatively simple, but we have implemented the most basic layout. In the future, I will continue to supplement it and make it more complete.


Appendix:

At present, this code runs and occasionally reports an error. I checked it and found no problem at the moment. If you know where the problem is, leave a message to tell me. Of course, if I find the problem, it will be updated to this blog. The following is the log when an error is reported:



Solution:

I followed the code again last night and found that I was mistaken.

Row 81st of the problem in the Activity

SpaceList. remove (spaceList. indexOf (index ));

Index should be passed in remove. I accidentally wrote the corresponding value of index.

SpaceList. remove (index );

I tested it and it was okay. I have modified the above Code



Related Article

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.