Implementation of android games and Application of photo browsers

Source: Internet
Author: User
Tags android games

Next, let's talk about a game. When we pass the level, there may be scene switching in the game, which is not familiar to the game developers. Here I will give a brief introduction to it, I will consolidate my learning knowledge and share my learning experience so that everyone can make common progress. implementation principle: first, we perform clipRect on the screen to obtain the corresponding rectangular module. then fill in the image. implementation of rectangle (horizontal) intersection: [java]/***** rectangular scene (horizontal) */public void doRect_H () {/** intersection of rectangles **/int ractHeight = (mScreenHeight/ractCount); // obtain the width of each rectangle. // start on the left (even numbers 0, 2, 4 ...) for (int I = 0; I <ractCount; I + = 2) {drawFillRect (mCanvas, Color. BLACK, 0, I * ractHeight, rectSte P, ractHeight);} // start on the right (odd 1, 3, 5 ...) for (int I = 1; I <ractCount; I + = 2) {drawFillRect (mCanvas, Color. BLACK, mScreenWidth-rectStep, I * ractHeight, rectStep, ractHeight);} I will briefly explain the process: We split the screen into 10 rectangles, then we fill the 10 rectangles one by one. the filling is the next scenario. the same is true for rectangular (vertical) intersection. here, you need to understand the save and restore methods of canvas. save: used to save the Canvas status. After saving, you can call operations such as pan, zoom, rotate, miscut, and crop of the Canvas. Restore: used to restore the State saved before the Canvas. This prevents operations performed on the Canvas after the save operation from affecting subsequent painting. Save and restore must be used together (restore can be less than save, but not more). If the restore calls more than save, an Error is thrown. The save and restore operations on Canvas are often mixed. Implementation of shutter scenarios: [java]/***** shutter scenarios */public void doSQUARE () {/** shutter effect use a double for loop to modify the width of each rectangle **/for (int I = 0; I <= (mScreenWidth/squareRange); I ++) {// calculate the number of squares in the width, for (int j = 0; j <= (mScreenHeight/squareRange); j ++) {// calculate the number of square contained in the height. drawFillRect (mCanvas, Color. BLACK, I * squareRange, j * squareRange, rectStep, rectStep) ;}} Simple Description: cut several squares Based on the width and height of the View and fill them one by one. scroll (to the right) watermark scenario: [java]/***** scroll watermark scenario (move right) */Public void doSHADOW_RINGHT () {/***** here we need to calculate the width of each rectangle and the starting X coordinate ** to understand its meaning. */int X1 = rectStep + rectX1; // The x coordinate int rectWidth1 of rectangle 1 = rectW + 12; // The width of rectangle 1 int X2 = X1 + rectWidth1 + rectX2; int rectwid2= rectW + 5; int X3 = X2 + rectwid2+ rectX3; int rectWidth3 = rectW; /** the watermark effect actually draws four rectangles and leaves some gaps between them **/drawFillRect (mCanvas, Color. BLACK, 0, 0, rectStep, mScreenHeight); drawFillRect (mCanvas, Color. BLACK, X1, 0, rectWidth1, mScreenHeight); drawFillRect (mCanvas, Color. BLACK, X2, 0, rectwid2, mScreenHeight); // draw the second rectangle drawFillRect (mCanvas, Color. BLACK, X3, 0, rectWidth3, mScreenHeight); // draw the third rectangle} Simple Description: First, we split the screen into four rectangles. the first one will always increase and move to the right, and the other three rectangles will also move to the right, so it feels like rolling ripple. the same applies to scrolling (to the left) Waterlines. horizontal Split field: [java]/***** horizontal split image */public void doHalf_V () {int Y = mScreenHeight/2; // obtain the intermediate split line int Y1 = Y-rectStep ;// Upper Part X coordinate drawFillRect (mCanvas, Color. BLACK, 0, Y1, mScreenWidth, rectStep); // draw the top part of drawFillRect (mCanvas, Color. BLACK, 0, Y, mScreenWidth, rectStep); // draw the following part.} I will not explain this, because this is the simplest one. finally, let's take a look at the drawFillRect method. [java]/*** draw a rectangle ** @ param canvas * @ param color * @ param x * @ param y * @ param w * @ param h */public void drawFillRect (Canvas canvas, int color, int x, int y, int w, int h) {int backColor = MPaint. getColor (); mPaint. setColor (color); canvas. save (); canvas. clipRect (x, y, x + w, y + h); int id = currentId + 1; if (id = bitmap. length) id = 0; // drawing canvas. drawBitmap (bitmap [id], null, rectF, null); canvas. restore (); mPaint. setColor (backColor);} This method is to fill the image with the cropped rectangle. the code is relatively simple and I believe everyone can understand it. we may have discovered that our scenarios are actually produced by rectangles. This also benefits from the clipRect method provided by the API, but the API only provides the clipping of rectangles, for example, there is no circular or arc shape, so I can't do this, but it is good to provide rectangular tailoring, as long as you If you have good implementation ideas, you can build them one by one. The scenario is described here. below we will expand: in fact, it is not called expansion, because I am an application, so the first thing that comes to mind is what effect this stuff can achieve in the application. for example, the personalized album player. or image browsers. in fact, if you apply the effects of the game to applications, the results will be very good. the following describes the implementation process: first, we need to customize the SurfaceView control and inject the collection of retrieved image arrays into the custom control. then there is logic processing. We need to perform a switchover every five minutes, that is, the scenario switching effect. however, the best result of scenario switching is random. that's all simple. as long as you understand the principle, it is not difficult to implement it. What is difficult is how our logic should be handled. This is also the difficulty of game development. Different from applications, application controls are all ready-made, at most, rewrite its controls to add additional auxiliary functions. so the most important thing is to be patient. okay. Let's take a look at the effect. (look pretty good. the screenshot capture tool is not good, so it looks like there are some cards and there is no problem with the mobile phone simulator running .)

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.