Android 模拟小球来回撞墙效果(游戏的开始)


Android 模拟小球来回撞墙效果代码:

  1. package com.ball;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Window;  
  6. import android.view.WindowManager;  
  7.   
  8. public class MainActivity extends Activity {  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  13.         getWindow().setFlags(               //设置为全屏模式   
  14.                 WindowManager.LayoutParams.FLAG_FULLSCREEN,   
  15.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  16.         setContentView(new BallView(this));  
  17.     }  
  18. }  
  1. package com.ball;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.Resources;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.graphics.Canvas;  
  8. import android.view.SurfaceHolder;  
  9. import android.view.SurfaceView;  
  10.   
  11.   
  12. public class BallView extends SurfaceView implements SurfaceHolder.Callback {  
  13.   
  14.     Bitmap bitmapBall = null;  
  15.     Movable movable = null;  
  16.     DrawThread dt;  
  17.     Bitmap bmpBack;  
  18.       
  19.     public BallView(Context context) {  
  20.         super(context);  
  21.         getHolder().addCallback(this);  
  22.         initBitmaps(getResources());  
  23.         initMovable();  
  24.         dt = new DrawThread(this,getHolder());  
  25.     }  
  26.   
  27.     public void initBitmaps(Resources r) {  
  28.         bitmapBall = BitmapFactory.decodeResource(r, R.drawable.icon);  
  29.     }  
  30.       
  31.     public void initMovable() {  
  32.         movable = new Movable(0,0,   
  33.                 0, bitmapBall);  
  34.     }  
  35.       
  36.     public void doDraw(Canvas canvas) {  
  37.         int color = getResources().getColor(android.R.color.white);  
  38.         canvas.drawColor(color);  
  39.         movable.drawSelf(canvas);  
  40.     }  
  41.       
  42.     @Override  
  43.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {  
  44.     }  
  45.   
  46.     @Override  
  47.     public void surfaceCreated(SurfaceHolder holder) {  
  48.         if(!dt.isAlive()) {  
  49.             dt.start();  
  50.         }  
  51.     }  
  52.   
  53.     @Override  
  54.     public void surfaceDestroyed(SurfaceHolder holder) {  
  55.         dt.flag = false;  
  56.         dt = null;  
  57.     }  
  58.   
  59. }  
  1. package com.ball;  
  2.   
  3. import android.graphics.Canvas;  
  4. import android.view.SurfaceHolder;  
  5.   
  6. public class DrawThread extends Thread {  
  7.     boolean flag = false;  
  8.     BallView ballView;  
  9.     SurfaceHolder surfaceHolder;  
  10.     int sleepSpan = 30;  
  11.       
  12.     public DrawThread(BallView ballView, SurfaceHolder surfaceHolder) {  
  13.         this.ballView = ballView;  
  14.         this.surfaceHolder = surfaceHolder;  
  15.         this.flag = true;  
  16.     }  
  17.       
  18.     public void run() {  
  19.         Canvas canvas = null;  
  20.         while(flag) {  
  21.             try {  
  22.                 canvas = surfaceHolder.lockCanvas(null);  
  23.                 synchronized(surfaceHolder) {  
  24.                     ballView.doDraw(canvas);  
  25.                 }  
  26.             } catch(Exception e) {  
  27.                 e.printStackTrace();  
  28.             } finally {  
  29.                 if(canvas != null) {  
  30.                     surfaceHolder.unlockCanvasAndPost(canvas);  
  31.                 }  
  32.             }  
  33.               
  34.             try{  
  35.                 Thread.sleep(sleepSpan);  
  36.             }  
  37.             catch(Exception e){                   
  38.                 e.printStackTrace();      
  39.             }  
  40.         }  
  41.     }  
  42. }  

相关内容