• SurfaceView 使用demo 飞机游戏小样


    本demo 主要使用了surfaceview 画图。

    1.在线程中对canvas操作。

    2.实现画图

    3.surfaceView 继承了view 可以重写ontouchevent方法来操作输入。

    代码存放:https://github.com/wish521/SurvaceViewDemo

    主类:

    package com.survaceview.view;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    import com.survaceview.R;
    import com.survaceview.utils.ScreenUtil;
    import com.survaceview.view.Logic.BulletFactory;
    import com.survaceview.view.Logic.BulletManger;
    import com.survaceview.view.Logic.MainPlaneManerger;
    import com.survaceview.view.Logic.NpcManerger;
    import com.survaceview.view.maps.Map;
    
    /**
     * Created by hongtao on 2017/10/25.
     */
    
    public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    
    
        public MySurfaceView(Context context) {
            super(context);
            init();
        }
    
        public MySurfaceView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        SurfaceHolder mSurfaceHolder;
    
        private void init() {
            mSurfaceHolder = getHolder();
            mSurfaceHolder.addCallback(this);
        }
    
    
        boolean mSurfaceIsCreated = false;
        Map mMap;
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            mSurfaceIsCreated = true;
            BulletFactory.propear(getContext());
        }
    
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            ScreenUtil.SCREEN_W = width;
            ScreenUtil.SCREEN_H = height;
            mMap = new Map(width, height);
            mMap.init(getResources(), R.raw.bg);
    
            MainPlaneManerger.getInstance().init(getResources(), width, height);
    
            NpcManerger.getInstance().init(getResources());
            gameThread.start();
        }
    
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            mSurfaceIsCreated = false;
        }
    
        long startTime;
        long endTime;
        long renderTime;
        Thread gameThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
    
                    startTime = System.nanoTime();
                    Canvas canvas = mSurfaceHolder.lockCanvas();
                    try {
                        if (mSurfaceIsCreated) {
                            if (mMap != null) {
                                mMap.draw(canvas);
                            }
    
                            MainPlaneManerger.getInstance().run(canvas);
    
                            if (NpcManerger.getInstance() != null) {
                                NpcManerger.getInstance().run(canvas);
                            }
    
                            BulletManger.getInstance().draw(canvas);
    
                            if (MainPlaneManerger.getInstance().getIsDie()) {
                                break;
                            }
                        }
                        endTime = System.nanoTime();
                        renderTime = endTime - startTime;
    
                        if (1000 / 30 - renderTime > 0) {
                            Thread.sleep(1000 / 30 - renderTime);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        mSurfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
            }
        });
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    return true;
                case MotionEvent.ACTION_MOVE:
                    MainPlaneManerger.getInstance().move(event.getRawX(), event.getRawY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
            return super.onTouchEvent(event);
        }
    
    }
  • 相关阅读:
    MySQL next-key lock 加锁范围是什么?
    使用 Docker 安装并连接 MySQL
    Mac 常用软件推荐 —— Java 开发工程师
    Toolkit 大更新:UI 更美观,用起来更方便!
    工作中的设计模式 —— 建造者模式
    工作中的设计模式 —— 策略模式
    工作中的设计模式 —— 原型模式
    Centos7 安装Oracle记录
    Centos7 常用命令和自己平时使用命令
    AutoHotKey 简易自动化脚本的一些资料
  • 原文地址:https://www.cnblogs.com/mamamia/p/7767415.html
Copyright © 2020-2023  润新知