天天看点

Android模拟自由落体运动

     最近想看看android的游戏开发,因此首先绘图方面得练练,突然就想到模拟一下自由落体运动。本例采用serfaceView实现,接下来上代码:

一、首先定义一个自定义控件

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable{
    public static float g=198f;//模拟重力加速度
    private SurfaceHolder sfh;
    private Thread thread;
    private Canvas canvas;
    private Paint paint;
    private Date startTime;
    private int ScreenW, ScreenH;
    private Boolean tag=false;
    private float y=100;
    private float v=50f;
    public MySurfaceView(Context context) {
        super(context);
        thread=new Thread(this);
        sfh=this.getHolder();
        // callback接口:只要继承SurfaceView类并实现SurfaceHolder.Callback接口就可以实现一个自定义的SurfaceView了,
        // SurfaceHolder.Callback在底层的Surface状态发生变化的时候通知View.
        sfh.addCallback(this);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        this.setKeepScreenOn(true);// 保持屏幕常亮
//        if(canvas != null) {
//            RectF rectF = new RectF(0, 0, this.getWidth(), this.getHeight());
//            canvas.drawRect(rectF,paint);
//            sfh.unlockCanvasAndPost(canvas);
//        }
    }
    //重力公式y=1/2gt^2 g=9.8 t是时间
    private void draw() {
        try {
            canvas = sfh.lockCanvas(); // 得到一个canvas实例
            canvas.drawColor(Color.WHITE);// 刷屏
            canvas.drawCircle(100,y,100,paint);
            Date nowTime=new Date(System.currentTimeMillis());//可以获取当前时间

            if(!tag){
                v=v+TimeUtil.shijiancha(nowTime,startTime)*g;
                y=y+v*TimeUtil.shijiancha(nowTime,startTime);
            }else{
                v=v-TimeUtil.shijiancha(nowTime,startTime)*g;
                y=y-v*TimeUtil.shijiancha(nowTime,startTime);
            }
            startTime=new Date(System.currentTimeMillis());//可以获取当前时间
        } catch (Exception ex) {
        } finally {
            if (canvas != null)
                sfh.unlockCanvasAndPost(canvas);  // 将画好的画布提交
        }
    }
    @Override
    public void startAnimation(Animation animation) {
        super.startAnimation(animation);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        ScreenW = this.getWidth();
        ScreenH = this.getHeight();
        startTime=new Date(System.currentTimeMillis());//可以获取当前时间
//        this.draw();
        thread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    @Override
    public void run() {
        while (true) {
            draw();
            if(y>=ScreenH){
                tag=true;
                v=v-50;
            }
            if(v<=0){//是否改变运行轨迹要以速度大小为标准,我之前层判断y数值大小是不对的.
                tag=false;
            }
            Log.e("比较-------------", ScreenH+"-------"+y+tag);
//            try {
                Thread.sleep(1);
//            } catch (InterruptedException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
        }
    }
}
           

     二、接下来只需要在MainActivity中将布局加载进去即可。

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //隐去电池等图标和一切修饰部分(状态栏部分)
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new MySurfaceView(this));
    }

    @Override
    protected void onResume() {
        super.onResume();
//        setContentView(new MySurfaceView(this));
    }
}
           
下一篇: 自由落体

继续阅读