How to Implement helloPe's android Project)

Source: Internet
Author: User

In the previous article "connecting to the practice of the helloPe android project-design", we designed a link-to-View Project for android, including the division of functional modules and the design of Core algorithms. This article introduces the implementation of the android platform's continuous access program. In this project, based on the above functional analysis, we will implement the following classes (below is the project file directory ):

In development, we follow the bottom-up approach. That is to say, we first develop the class at the bottom layer, which does not depend on other classes we need to implement. According to the above analysis, we first developed the interface display class in the presentation layer module, first the BoardView class. On the android platform, we inherited the self-View class to View such code, try to add detailed comments in the Code:

Package nate. llk. view;/* sort the imported packages again */
/*************************************** * ********** @ Author HelloPe ************************** * ********************/public class BoardView extends View {/***** Number of icons in the X axis direction + 2 */protected static final int xCount = 10; /*** Number of charts in the Y axis of yCount + 2 */protected static final int yCount = 12;/*** map connects to the game board, the int type added to map in the program means index, not the screen coordinate! */Protected int [] [] map = new int [xCount] [yCount];/*** iconSize: The Icon size. The icon is square, therefore, an int variable indicates */protected int iconSize;/*** Number of iconCounts icons */protected int iconCounts = 19; /*** all icons images */protected Bitmap [] icons = new Bitmap [iconCounts]; /*** path indicates the path of the connected vertex */private Point [] path = null; /*** selected icon */protected List <Point> selected = new ArrayList <Point> ();/*** constructor * @ param context * @ p Aram attrs */public BoardView (Context context, AttributeSet attrs) {super (context, attrs); calIconSize (); Resources r = getResources (); // load loadBitmaps (1, r. getDrawable (R. drawable. fruit_01); loadBitmaps (2, r. getDrawable (R. drawable. fruit_02); loadBitmaps (3, r. getDrawable (R. drawable. fruit_03); loadBitmaps (4, r. getDrawable (R. drawable. fruit_04); loadBitmaps (5, r. getDrawable (R. drawable. fruit_05 )); LoadBitmaps (6, r. getDrawable (R. drawable. fruit_06); loadBitmaps (7, r. getDrawable (R. drawable. fruit_07); loadBitmaps (8, r. getDrawable (R. drawable. fruit_08); loadBitmaps (9, r. getDrawable (R. drawable. fruit_09); loadBitmaps (10, r. getDrawable (R. drawable. fruit_10); loadBitmaps (11, r. getDrawable (R. drawable. fruit_11); loadBitmaps (12, r. getDrawable (R. drawable. fruit_12); loadBitmaps (13, r. getDrawable (R. drawabl E. fruit_13); loadBitmaps (14, r. getDrawable (R. drawable. fruit_14); loadBitmaps (15, r. getDrawable (R. drawable. fruit_15); loadBitmaps (16, r. getDrawable (R. drawable. fruit_17); loadBitmaps (17, r. getDrawable (R. drawable. fruit_18); loadBitmaps (18, r. getDrawable (R. drawable. fruit_19);}/*** calculate the Icon size */private void calIconSize () {// obtain the screen size DisplayMetrics dm = new DisplayMetrics (); (Activity) this. getContext ()). g EtWindowManager (). getdefadisplay display (). getMetrics (dm); iconSize = dm. widthPixels/(xCount);}/*** function is used to load the icon resource and assign a key (Specific integer ID) bind an icon * @ param key: the identifier of a specific icon * @ param d resource */public void loadBitmaps (int key, drawable d) {Bitmap bitmap = Bitmap. createBitmap (iconSize, iconSize, Bitmap. config. ARGB_8888); Canvas canvas = new Canvas (bitmap); d. setBounds (0, 0, iconSize, iconSize); d. draw (canvas); icon S [key] = bitmap; // No index 0}/*** View is used, but in this method, there is a draw path (delete two icons of China Unicom ), * draw all the icons of the Board (or refresh as long as the map position value is greater than 0) * enlarge the first selected icon (selected. size () = 1) */@ Overrideprotected void onDraw (Canvas canvas) {/*** draw the connection path, and then clear the path and two icons */if (path! = Null & path. length> = 2) {for (int I = 0; I <path. length-1; ++ I) {Paint paint = new Paint (); paint. setColor (Color. BLUE); paint. setStrokeWidth (3); paint. setStyle (Paint. style. STROKE); Point p1 = indexToScreen (path [I]. x, path [I]. y); Point p2 = indexToScreen (path [I + 1]. x, path [I + 1]. y); canvas. drawLine (p1.x + iconSize/2, p1.y + iconSize/2, p2.x + iconSize/2, p2.y + iconSize/2, paint);} map [path [0]. x] [path [0]. y] = 0; map [path [path. length-1]. x] [path [path. length-1]. y] = 0; selected. clear (); path = null;}/*** draw all the icons of the Board when the value in the coordinate is greater than 0 */for (int x = 1; x <xCount-1; ++ x) {for (int y = 1; y <yCount-1; ++ y) {if (map [x] [y]> 0) {Point p = indexToScreen (x, y); canvas. drawBitmap (icons [map [x] [y], p. x, p. y, null) ;}}/ *** draw the selected icon. When the selected icon is displayed, the icon is enlarged. * // for (Point position: selected) {if (selected. size ()> 0) {Point position = selected. get (0); Point p = indexToScreen (position. x, position. y); if (map [position. x] [position. y]> = 1) {canvas. drawBitmap (icons [map [position. x] [position. y], null, new Rect (p. x-5, p. y-5, p. x + iconSize + 5, p. y + iconSize + 5), null) ;}} super. onDraw (canvas );} /*** tool method ** @ horizontal coordinates in the param x array * @ ordinate y in the param y array * @ return convert the coordinates of the icon in the array to the actual coordinates on the screen */public Point indexToScreen (int x, int y) {return new Point (x * iconSize, y * iconSize );} /*** tool method ** @ param x y screenToIndex (int x, int y) {int xindex = x/iconSize; int yindex = y/iconSize; if (xindex <xCount & yindex <yCount) {return new Point (xindex, yindex );} else {return new Point (0, 0) ;}/ *** input path data update display, that is, remove * @ param path */public void drawLine (Point [] path) {this. path = path; this. invalidate ();}}

This class mainly loads the linked view ICON resources and binds them to a specific int type key, we can perform operations more conveniently. Of course

This class also requires some necessary tool functions, such as the screenToIndex method, because we use screen coordinates to customize the View to draw on the screen, but at the same time, we need to watch the game.

In, we also need to know the index of the icon (because the icon is always the same length and width, it is easy to convert between the screen coordinate and index) for easy operation. Of course, rewrite is the most important thing in this class.

OnDraw function; this function first checks whether the path is null and whether there are two or more elements. When we define the path variable, it is used as a tool to save the connected path. (Path

The value of the connection path will be added when the connection algorithm is implemented. Here we first draw a line in the onDraw function (if connected ), then, set the map value at the beginning and end of the path to 0.

In the descending order, the map values of rows 0th and the last row are always 0, the map values of columns 0th and the last are always 0, and the 0 value in the map is 0, which indicates that no icon is available, according to the binding value of the preceding icon resource and

The values in the map correspond to the values in the map. If the values in the map are a few, the corresponding icon is attached to the corresponding index. In the onDraw function, another function is to enlarge the first icon to remind players.

Finally, draw (textures). As mentioned above, the corresponding icon resource is attached to the corresponding position when the map value is as much as it is. When loading the resource above, we can see that there is no icon resource corresponding to 0, if the value is 0, no texture is created.

In order to prevent code confusion, the above BoardView class does not implement all functions, such as touch event listening, connection Algorithm Implementation, and no solution. So we set the BoardView

Class to inherit BoardView's GameView (this also makes the Code not too messy ). Due to space limitations, we can first implement the internal class used in the GameView to listen for the remaining time

(This class implements the Runnable interface ):

/*** The thread used to update the remaining time * @ author helloPe **/class RefreshTime implements Runnable {@ Overridepublic void run () {if (isContinue) {while (leftTime> 0 &&! IsStop) {timerListener. onTimer (leftTime); leftTime --; try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace () ;}} if (isStop & leftTime> 0) {if (win (); // setMode (WIN); elsesetMode (PAUSE );} // setMode (LOSE); else if (leftTime = 0) {setMode (LOSE) ;}}/ *** stop display time */public void stopTimer () {isStop = true; isContinue = false;}/*** set to continue */public void setContinue () {isContinue = true; isStop = false; refreshTime = new RefreshTime (); thread t = new Thread (refreshTime); // You must correctly start a Thread t that implements the Runnable interface. start ();}

As mentioned above, this thread is used to control the game time.

Here, we will introduce several custom interfaces,

public interface OnStateListener{public void OnStateChanged(int StateMode);}

There is only one method, mainly listening for game status changes, such as pause and stop.

public interface OnTimerListener{public void onTimer(int leftTime);}

Used to listen for the remaining time. Different from the preceding thread, This method uses the leftTime result of the preceding thread to update the time progress bar used to remind players in the game.

public interface OnToolsChangeListener{public void onRefreshChanged(int count);public void onTipChanged(int count);}

Tool is two tools provided to players in our game. One is to refresh the game interface and disrupt the existing Board (of course, the number of existing charts remains unchanged ), the other is the automatic help feature of the previously mentioned hint, which helps players find a set of accessible icons. Of course, both tools have limits on the number of times.

This article describes the development and introduction of the BoardView class and Time Thread class. We will complete the rendering and touch event processing of the game board, and the connection algorithm and hint in the game's Core algorithms automatically help the algorithm and determine whether there is no solution to the algorithm. These codes are processed in the GameView class inherited from the BoardView class.

The reason why I wrote this series of articles is to record the experiences of small android projects, increase practical capabilities, and make a summary. Not just to make a novel project, but also to learn from many netizens!


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.