Surface instance-Small ball bounce case

Source: Internet
Author: User
Tags getcolor

Ball.java

Package Com.example.sufacedemo;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.util.DisplayMetrics; /** * Balls * @author ZCL * */public class Ball {/** * balls high */public static FINA         l int HEIGHT = 93;         /** * Ball Width */public static final int width = 93;         private static final int steplength = 10;//interval for each motion private static final float Reducepercentage = 0.35f;//decrement factor         private int stepreduce;//The shortened distance of each inverse movement private float runX;//Ball position private float runy;//Ball position         Private Ballsurfaceview BSV; Private Boolean updirection = false;//if true,up direction,or is down direction private float maxheight;//Current highest movement                 Highly private paint paint;         Bitmap ballbitmap;//Picture of the ball sportactivity sa; Public Ball (Float INITX, float inity, Ballsurfaceview bsv) {this.runx = INITX;                   This.runy = inity;                   MaxHeight = inity;                   THIS.BSV = BSV; Ballbitmap = Bitmapfactory.decoderesource (Bsv.getresources (), r.drawable.ball);//Load picture paint = new Paint (                   );         SA = bsv.sportactivity;                   public void OnDraw (canvas canvas) {int c = Paint.getcolor ();//Save color, then revert to previous color                   Boundarytest ();                   if (canvas! = null) Canvas.drawbitmap (ballbitmap,runx,runy,paint);                   Paint.setcolor (c);         Move (); }/** * Motion * * private void Move () {if (MaxHeight >= (Sa.screenheigh                   T-height)) {return;                   } if (updirection) {//Up Runy = Runy + steplength;     }else{                       Runy = Runy-steplength; }}/** * Bounds detection so that the ball does not fly out of bounds */private void Boundarytest () {if (R                            UnY > Sa.screenheight-height) {//down motion head updirection =!updirection;//direction Reverse                            Runy = Sa.screenheight-height;                            Stepreduce = (int) (maxheight * reducepercentage);  MaxHeight = MaxHeight + stepreduce;//maximum height decrement} if (Runy <  MaxHeight) {//up motion head updirection =!updirection;//direction reverse if (maxheight                            >= (Sa.screenheight-height)) return;                                              Runy = MaxHeight; }         }}

  

Ballsurfaceview

Package Com.example.sufacedemo;import Android.annotation.suppresslint;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.view.surfaceholder;import Android.view.surfaceview;public class Ballsurfaceview extends SurfaceView Implementssurfaceholder.callback {sportactivity sportactivity;//invokes the context reference of this surfaceview private ball ball;// Small ball Surfaceholder Holder;public Ballsurfaceview (context context) {super (context); this.sportactivity = (sportactivity) Context;ball = new Ball (+, this); holder = This.getholder (); Holder.addcallback (this);} @SuppressLint ("Wrongcall") @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); if (canvas = = null)  Canvas = Holder.lockcanvas ();//Lock canvas Paint p = new Paint (); int c = P.getcolor ();p. SetColor (Color.White);//Set Background white if (canvas ! = null) canvas.drawrect (0, 0, sportactivity.screenwidth,sportactivity.screenheight, p);p. SetColor (c); Ball.onDraw ( Canvas); Holder.unlockcanvasandPost (Canvas),//release lock} @Overridepublic void Surfacechanged (surfaceholder holder, int format, int width,int height) {}@ overridepublic void surfacecreated (Surfaceholder holder) {new Refreshthread (). Start (); @Overridepublic void surfacedestroyed (Surfaceholder holder) {}private Class Refreshthread extends Thread {@SuppressLint ("Wrongcall") @Overridepublic void Run () {while (true) {Canvas canvas = null;try {onDraw (canvas);} catch (Exception e) {E.P Rintstacktrace ();} try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();}}}}

  

Sportactivity.java

Package Com.example.sufacedemo;import Android.os.bundle;import Android.app.activity;import android.view.Menu; Import Android.app.activity;import Android.content.pm.activityinfo;import Android.os.bundle;import Android.util.displaymetrics;import Android.view.window;import Android.view.WindowManager;         public class Sportactivity extends Activity {public int screenwidth;         public int screenheight;    Ballsurfaceview BSV; /** called when the activity is first created.        */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        BSV = new Ballsurfaceview (this);        Get screen size displaymetrics dm = new Displaymetrics ();              DM = This.getapplicationcontext (). Getresources (). Getdisplaymetrics ();              ScreenWidth = Dm.widthpixels;            ScreenHeight = Dm.heightpixels;                   The next two sentences are set to full screen requestwindowfeature (window.feature_no_title); GetWindow (). SetFlags (WindOwManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);    Setcontentview (BSV); }}

  

Surface instance-Small ball bounce case

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.