[Android] a bounce ball-surfaceview

Source: Internet
Author: User
Brief Introduction:

On a black full screen with Android, the red ball jumps randomly and the acceleration is getting faster and faster.

Use surfaceview, set it to full screen in the activity, and set the surfaceview of ball bounce to the display view. The main code is implemented in surfaceview.


Set the initial position of the ball to the center of the screen, set the initial direction to downward (expressed by angle, 0 ° in the right direction, and increased in the clockwise direction), and set the acceleration to 1. Then enable the thread to re-paint surfaceview, and re-paint the thread after thread. Sleep (10), and increase the acceleration by 0.2, otherwise it will be too fast. During each re-painting, the system checks whether the image hits the wall and writes a static final class to record the wall on which the image hits the wall. Then, the bounce direction is within 180 ° (because the screen cannot pop up in the direction) random, the route will be fixed, so random.


Main Code:

Public class gameview extends surfaceview implements callback {private float X; private float y; private double angle; // the right direction is positive, the angle is zero, and the clockwise private int radius; private surfaceholder; private animthread thread; private double accelerate; private int lastdir = direction. mid; Public gameview (context) {super (context); // todo auto-generated constructor stubsurfaceholder = This. getholder (); surf Aceholder. addcallback (this); this. setfocusable (true); thread = new animthread (surfaceholder, getcontext ();} @ overridepublic void surfacechanged (surfaceholder arg0, int arg1, int arg2, int arg3) {// todo auto-generated method stub} @ overridepublic void surfacecreated (surfaceholder arg0) {// todo auto-generated method stubx = getwidth ()/2; y = getheight () /2; radius = 30; angle = math. pI * 0.5; accelerate = 1; Thread. isrunning = true; thread. start () ;}@ overridepublic void surfacedestroyed (surfaceholder holder) {// todo auto-generated method stubthread. isrunning = false; try {thread. join ();} catch (interruptedexception e) {e. printstacktrace () ;}} public void drawcircle (canvas, paint) {canvas. drawcolor (color. black); // clear the canvas. drawcircle (X, Y, radius, paint); y + = math. sin (angle) * accelerate; x + = Math. cos (angle) * accelerate; Switch (getdirection (x, y) {Case direction. up: angle = math. random () * Math. pi; break; Case direction. down: angle = (1 + math. random () * Math. pi; break; Case direction. left: angle = (math. random ()-0.5) * Math. pi; break; Case direction. right: angle = (0.5 + math. random () * Math. pi; break ;}} public int getdirection (float X, float y) {If (Y <= radius & lastdir! = Direction. up) {lastdir = direction. up; y = radius; log. I ("dir", "up"); Return direction. up;} else if (Y >=( getheight ()-radius) & lastdir! = Direction. down) {lastdir = direction. down; y = getheight ()-radius; log. I ("dir", "down"); Return direction. down;} else if (x <= radius & lastdir! = Direction. left) {lastdir = direction. left; X = radius; log. I ("dir", "Left"); Return direction. left;} else if (x> = getwidth ()-radius & lastdir! = Direction. right) {lastdir = direction. right; X = getwidth ()-radius; log. I ("dir", "right"); Return direction. right;} return direction. mid;} static final class direction {public static final int left = 0; public static final int right = 1; public static final int up = 2; public static final int down = 3; public static final int mid = 4;} class animthread extends thread implements runnable {private Boolean isrunning; private surfaceholder holder; private context; private paint; Public animthread (surfaceholder holder, context context) {This. holder = holder; this. context = context; isrunning = false; paint = new paint (); paint. setcolor (color. red); paint. setstyle (paint. style. fill);} public void run () {// calculates the coordinate canvas = NULL for drawing an animation; while (isrunning) {accelerate + = 0.2; canvas = holder. lockcanvas (null); drawcircle (canvas, paint); try {thread. sleep (10);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {holder. unlockcanvasandpost (canvas );}}}}}


Problems:

1. I had no idea about acceleration before. Later I used acceleration and direction to draw it based on my needs, because when I knew the angle, I knew the distance, and the distance between the X and Y directions, after the angle definition, sin and cos values are both positive and negative, and you do not need to consider them yourself. The implementation is very simple. It should be noted that, in fact, the constant acceleration used above is the distance moving in the current direction.

2. when it is detected that the left wall is hit, it is found that 0 to π/2 and 3/2 π to 2 π. It is troublesome to add a judgment, later we found that it can be expressed as-π/2 to π/2.

After hitting the wall, the ball changes the direction just once. It is strange that the next draw after hitting the wall may be judged as hitting the wall through log output, so it will randomly change the direction, so the variable is added to record the last hit Wall, because it is impossible to hit the same wall twice in a row, so the judgment is added, if the wall is not the same as the last one, you can change the direction.

Project address

Http://download.csdn.net/detail/felicitia/5568083

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.