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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一拳小和尚LXY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值