`
158067568
  • 浏览: 326958 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

SurfaceView简单例子

阅读更多

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();
                                     }
                            }
                   }
                  
         }
 
}
 

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

 

 

 

分享到:
评论
8 楼 jnssvh 2015-08-08  
楼主,还发代码吗?
jnssvh@aliyun.com
7 楼 linchuan02 2013-08-30  
linth0920@126.com 谢谢!
6 楼 maoqianqian99 2013-08-15  
楼主这个RefreshThread线程写的好像有点问题哦。
Canvas canvas = null; 
                                     try{ 
                                               onDraw(canvas); 
                                     }catch(Exception e){ 
                                               e.printStackTrace(); 
                                     } 
                                     
这段代码 里canvas一直是 Null? 那直接写成 onDraw(null)不就行了?
5 楼 huxingnong 2013-05-15  
楼主给我一份代码哈。邮箱1534143789@qq.com
4 楼 hezhiyi 2011-08-20  
       博主威武!!!感动中国
3 楼 zxflb 2011-02-21  
效果 不错
2 楼 15921310063 2011-02-18  
运行了下,觉得不错。
谢谢分享!
1 楼 qianlei007 2011-02-17  
回复是一种美德。
    运行了一下,基本明白了。 谢谢!

相关推荐

Global site tag (gtag.js) - Google Analytics