Recently, WeChat is a very popular game [one second] for android-development sharing, android

Source: Internet
Author: User

Recently popular mini games [one second] android version-development sharing, android

Recently, in the circle of friends, a friend turned to an html game [one second]. The rule of the game is: the user presses the button and releases it to see who can keep the time for one second accurately. ^_^ The server crashed when I debugged the program on the linux server just now, and made up some time to share this little game I made the day before yesterday.

Not to mention, directly:


As a little friend who loves android, It is also a pleasure to spare a little time to make a small game. The entire game is relatively simple, mainly divided into uidesign and development and time computing logic. Of course, the UI of this mini-game should be brilliant, so I will focus on uidesign and development.


Uidesign and development

The RelativeLayout layout is used to control the entire interface flexibly. The basic layout is easy for you, and it is simple layout. The main reason is how to extend the layout after you press and hold the button. My idea is to show that the second layout contains all the required space at the beginning. This layout uses a height that increases with the content, and then writes a view with a height of 10 DP to hold the height up, then, hide other items directly, and then directly display them based on the logic.

Below is my xml layout code

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical" android: background = "# ffffff"> <! -- Android: background = "@ drawable/bg" --> <! -- <TextView android: layout_width = "match_parent" android: layout_height = "50dp" android: text = "one second game, ready GO! "Android: textSize =" 22sp "android: gravity =" center "android: textColor =" # ffffff "android: background =" #000000 "/> --> <LinearLayout android: id = "@ + id/linearLayout" android: layout_marginTop = "30dp" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <TextView android: id = "@ + id/tip_l" android: layout_width = "0dp" android: layout_height = "wrap_content" android: Layout_weight = "1" android: text = "your" android: textSize = "50sp" android: gravity = "right" android: textColor = "#666666" android: textStyle = "bold"/> <TextView android: id = "@ + id/tip_r" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1.3" android: text = "one second" android: textSize = "50sp" android: textColor = "# FF3333" android: gravity = "left" android: textStyle = "bold"/> </LinearLayo Ut> <TextView android: layout_marginTop = "3dp" android: id = "@ + id/tip_ B" android: paddingLeft = "8dp" android: layout_below = "@ + id/linearLayout" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "How long is it? "Android: textSize =" 40sp "android: textColor =" #666666 "android: textStyle =" bold "android: gravity =" center "/> <TextView android: layout_marginTop = "15dp" android: id = "@ + id/tip_e" android: layout_below = "@ + id/tip_ B" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "How long is one second? "Android: textSize =" 15sp "android: textColor =" #666666 "android: textStyle =" bold "android: gravity =" center "/> <LinearLayout android: layout_below = "@ + id/tip_e" android: id = "@ + id/show_linearlayout" android: layout_marginLeft = "50dp" android: layout_marginRight = "50dp" android: layout_marginTop = "3dp" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: background = "@ drawable/show" android: orientation = "vertical"> <TextView android: layout_width = "fill_parent" android: layout_height = "10dp"/> <LinearLayout android: layout_marginTop = "10dp" android: id = "@ + id/show_result" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "horizontal" android: visibility = "gone"> <TextView android: id = "@ + id/show_result_l" android: layout_width = "0dp" android: layout_weight = "1.7" android: layout_height = "wrap_content" android: gravity = "right" android: textColor = "# CC9933" android: textSize = "40sp"/> <TextView android: layout_marginLeft = "3dp" android: id = "@ + id/show_result_r" android: text = "second" android: gravity = "left" android: layout_width = "0dp" android: layout_weight = "1" android: layout_height = "wrap_content" android: textColor = "# ffffff" android: textSize = "25sp"/> </LinearLayout> <TextView android: paddingLeft = "30dp" android: paddingRight = "30dp" android: layout_marginTop = "5dp" android: gravity = "center" android: id = "@ + id/show_remark" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: visibility = "gone" android: textColor = "# ffffff" android: textSize = "15sp" android: layout_marginBottom = "20dp"/> </LinearLayout> <Button android: layout_below = "@ + id/show_linearlayout" android: layout_centerHorizontal = "true" android: background = "@ drawable/button" android: layout_marginTop = "20dp" android: id = "@ + id/cul_bt" android: layout_height = "120dp" android: layout_width = "120dp" android: layout_gravity = "center_horizontal" android: gravity = "center" android: text = "hold down" android: textColor = "# ffffff" android: textSize = "30sp"/> </RelativeLayout>


A special font is added to the UI for better beautification. I added the "cube cartoon" and "文" fonts. The method for displaying special fonts in android is as follows: if your IDE is eclipse, you can directly put it in assets (the method is not described here). My IDE is android studio, if your IDE is the same as mine, you can use the following methods:

Create an assets package in the app → src → main directory, and add

<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />


And then reference it in your own code.

Private Typeface fangtikatong, huawencaiyun; // cube cartoon, 文
// Save the font file to the assets Directory and create the Typeface object fangtikatong = Typeface at www.linuxidc.com. createFromAsset (getAssets (), "fangtikatong. ttf "); huawencaiyun = Typeface. createFromAsset (getAssets (), "huawencaiyun. TTF "); result_r.setTypeface (fangtikatong); tip_l.setTypeface (huawencaiyun );


Time computing Logic

The time computing logic is relatively simple. First, you can monitor the touch of the button and set the system time for the two variables to record the moment and the moment (the same touch process, the latter minus the former and then converts the unit to calculate the duration of the touch.

The Code is as follows:

Click_cl.setOnTouchListener (new View. onTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {if (event. getAction () = MotionEvent. ACTION_DOWN) {x1 = (long) System. currentTimeMillis (); // show = new StringBuffer ("first press" + '\ n' + x1 +' \ n');} if (event. getAction () = MotionEvent. ACTION_UP) {x2 = (long) System. currentTimeMillis (); if (result_l.getVisibility () = View. GONE | result_l.getVisibility () = View. GONE | remark. getVisibility () = View. GONE) {result. setVisibility (View. VISIBLE); result_l.setVisibility (View. VISIBLE); result_r.setVisibility (View. VISIBLE); remark. setVisibility (View. VISIBLE);} num = (x2-x1) * 1.000/1000; result_l.setText (num + ""); // if the value is less than or equal to 1, multiply by 1000 and then divide it by 200 if (num <= 1) {remark. setText (remarks [(int) (num * 1000)/200)]); // values greater than 1 are first multiplied by 1000 and then divided by 200} else {remark. setText (remarks [(int) (num * 1000 + 200)/200)]);} return true ;}});
At this point, the entire game is basically developed, isn't it quite simple?

If you love android, can you develop your own games in your free time?


Download CSDN game code: one-second game source code

The github game code is at: one-second game source code








Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.