I am also developing the 2048 square design.

Source: Internet
Author: User

This time, our task is to implement small blocks on the game panel. Our main panel is a GridLayout. We add small blocks one by one to GridLayout to form our current game panel.

The small square is relatively simple. The key is, how can we make it the most efficient? This is what we have been thinking about. It is also considered occupational obsessive-compulsive disorder. Our small square actually shows a number, so we can use TextView or ImageView, check whether custom images are required for future plans. For now, use TextView, which is simple.

What is the parent layout? In fact, the layout is the same because we have a sub-View. However, FrameLayout is the first choice for efficiency consideration. This is the simplest and most efficient layout.


OK. We also need to implement some methods for this small square:

1. Set the displayed number

2. Set the background color based on numbers.

3. A series of get and set methods. This can be done later during program design.

Package com. xys. game2048.bean; import android. content. context; import android. graphics. color; import android. view. gravity; import android. view. view; import android. widget. frameLayout; import android. widget. textView; public class GameItem extends FrameLayout {// Item: display the number private int cardShowNum; // Item: Display color private int colorShow; // Number Title private TextView tvNum; // Number Title LayoutParams private LayoutParams params; public GameItem (Context context, int cardShowNum) {super (context); this. cardShowNum = cardShowNum; // initialize IteminitCardItem ();}/*** initialize Item ** @ param context * @ param cardShowNum */private void initCardItem () {// set the background Color setBackgroundColor (Color. GRAY); tvNum = new TextView (getContext (); setNum (cardShowNum); tvNum. setTextSize (30); tvNum. setGravity (Gravity. CENTER); params = new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT); params. setMargins (5, 5, 5, 5); addView (tvNum, params);} public View getItemView () {return tvNum;} public int getNum () {return cardShowNum ;} public void setNum (int num) {this. cardShowNum = num; if (num = 0) {tvNum. setText ("");} else {tvNum. setText ("" + num);} // set the background color switch (num) {case 0: tvNum. setBackgroundColor (0x00000000); break; case 2: tvNum. setBackgroundColor (0xffeee4da); break; case 4: tvNum. setBackgroundColor (0xffede0c8); break; case 8: tvNum. setBackgroundColor (0xfff2b179); break; case 16: tvNum. setBackgroundColor (0xfff59563); break; case 32: tvNum. setBackgroundColor (0xfff67c5f); break; case 64: tvNum. setBackgroundColor (0xfff65e3b); break; case 128: tvNum. setBackgroundColor (0xffedcf72); break; case 256: tvNum. setBackgroundColor (0xffedcc61); break; case 512: tvNum. setBackgroundColor (0xffedc850); break; case 1024: tvNum. setBackgroundColor (0xffedc53f); break; case 2048: tvNum. setBackgroundColor (0xffedc22e); break; default: tvNum. setBackgroundColor (0xff3c3a32); break ;}}}

Now, you can change the background color or custom image.


PS requires the source code, please pay attention to it, and will be sent to you after improvement

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.