论坛首页 移动开发技术论坛

SurfaceView简单例子

浏览 9910 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-02-13  

SurfaceView简单例子

作者:Legend

QQ158067568

上一节讨论了SurfaceView的初步知识,这一节将通过一个简单的例子来进一步学习SurfaceView

本节将学习一个例子来对上一节内容做个总结,该例子讲演示一个篮球上下运动的动画。java eyeblog贴图还需要将图片上传到其他网站在转帖,我实在觉得有些麻烦,所以就不贴图了。大家下载之后再机子上运行一下就ok了。

实现

Activity中很简单,代码中需要注意的地方与知识点都已经给了注释,代码如下:

package cn.edu.heut.zcl;
 
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);
        //获得屏幕尺寸
        DisplayMetrics dm = new DisplayMetrics();
    dm = this.getApplicationContext().getResources().getDisplayMetrics();
              screenWidth = dm.widthPixels;
              screenHeight = dm.heightPixels;
            //下两句为设置全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
                   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , 
                                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
       
        setContentView(bsv);
    }
}
 

接下来介绍球类:

package cn.edu.heut.zcl;
 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.DisplayMetrics;
 
/**
 * 球类
 * @author zcl
 *
 */
public class Ball {
          
         /**
          * 球的高
          */
         public static final int HEIGHT = 93;
         /**
          * 球的宽
          */
         public static final int WIDTH = 93;
         private static final int STEPLENGTH = 10;//每次运动的间距
         private static final float REDUCEPERCENTAGE =  0.35F;//递减系数
         private int stepReduce ;//每次反向运动的缩短的距离
        
         private float runX ;//球的位置
         private float runY ;//球的位置
         private BallSurfaceView bsv ;
         private boolean upDirection = false;//if true,up direction,or is down direction
         private float maxHeight ;//当前运动最高的高度
         private Paint paint ;
        
         Bitmap ballBitmap ;//球的图片
         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);//加载图片
                   paint = new Paint();
                   sa = bsv.sportActivity;
         }
        
         public void onDraw(Canvas canvas) {
                   int c = paint.getColor();//保存颜色,之后还原为之前颜色
                   boundaryTest();
                   if(canvas != null) canvas.drawBitmap(ballBitmap,runX,runY,paint);
                   paint.setColor(c);
                   move();
         }
         /**
          * 运动
          */
         private void move() {
                   if(maxHeight >= (sa.screenHeight - HEIGHT)) {
                            return;
                   }
                   if(upDirection){//向上
                            runY = runY + STEPLENGTH ;
                   }else{
                            runY = runY - STEPLENGTH ;
                   }
         }
 
         /**
          * 边界检测,使球不会飞出边界
          */
         private void boundaryTest(){
 
                   if(runY > sa.screenHeight - HEIGHT){//向下运动到头
                            upDirection = !upDirection;//方向置反
                            runY = sa.screenHeight - HEIGHT;
                            stepReduce = (int) (maxHeight * REDUCEPERCENTAGE);
                            maxHeight = maxHeight + stepReduce ;//最大高度递减
                           
                   }
                   if(runY < maxHeight ){//向上运动到头
                            upDirection = !upDirection;//方向置反
                            if(maxHeight >= (sa.screenHeight - HEIGHT)) return;
                            runY = maxHeight ;
                           
                   }
         }
}
 

SurfaceView类:

package cn.edu.heut.zcl;
 
import android.R.color;
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
implements SurfaceHolder.Callback{
         SportActivity sportActivity ;//调用该SurfaceView的上下文引用
         private Ball ball ;//小球
         SurfaceHolder holder ;
        
         public BallSurfaceView(Context context) {
                   super(context);
                   this.sportActivity = (SportActivity)context ;
                   ball = new Ball(100, 100, this);
                   holder = this.getHolder();
                   holder.addCallback(this);
         }
 
         @Override
         protected void onDraw(Canvas canvas) {
                   super.onDraw(canvas);
                  
                   if(canvas == null) canvas = holder.lockCanvas();//锁定画布
                   Paint p = new Paint();
                   int c = p.getColor();
                   p.setColor(Color.WHITE);//设置背景白色
                   if(canvas != null)
                   canvas.drawRect(0, 0, sportActivity.screenWidth, sportActivity.screenHeight, p);
                   p.setColor(c);
                   ball.onDraw(canvas);
                   holder.unlockCanvasAndPost(canvas);//释放锁
         }
 
         @Override
         public void surfaceChanged(SurfaceHolder holder, int format, int width,
                            int height) {
                  
         }
 
         @Override
         public void surfaceCreated(SurfaceHolder holder) {
                   new RefreshThread().start();
         }
 
         @Override
         public void surfaceDestroyed(SurfaceHolder holder) {
                  
         }
        
         private class RefreshThread extends Thread{
 
                   @Override
                   public void run() {
 
                            while(true){
                                     Canvas canvas = null;
                                     try{
                                               onDraw(canvas);
                                     }catch(Exception e){
                                               e.printStackTrace();
                                     }
                                    
                                     try {
                                               Thread.sleep(40);
                                     } catch (InterruptedException e) {
                                               e.printStackTrace();
                                     }
                            }
                   }
                  
         }
 
}
 

代码中的注释已经很清楚了,具体原理参考上一节的内容。如需代码,留下邮箱。

 

 

 

   发表时间:2011-02-17  
回复是一种美德。
    运行了一下,基本明白了。 谢谢!
0 请登录后投票
   发表时间:2011-02-18  
运行了下,觉得不错。
谢谢分享!
0 请登录后投票
   发表时间:2011-02-21  
效果 不错
0 请登录后投票
   发表时间:2011-09-14  
一路看了你的三个帖子,受益匪浅,兰州加油
0 请登录后投票
   发表时间:2011-09-14  
yuanlairumeng5219@163.com
0 请登录后投票
   发表时间:2011-09-22  
zhouyu_84@hotmail.com
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics