首页 > web前端 > html教程 > Drawable Animation 帧动画_html/css_WEB-ITnose

Drawable Animation 帧动画_html/css_WEB-ITnose

WBOY
发布: 2016-06-24 11:32:03
原创
940 人浏览过

MySurfaceView类:

package com.fm;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.Log;import android.view.KeyEvent;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.SurfaceHolder.Callback;public class MySurfaceView extends SurfaceView implements Callback, Runnable {    private SurfaceHolder sfh;    private Paint paint;    private Thread th;    private boolean flag;    private Canvas canvas;    private int screenW, screenH;    //首先声明十个容量的位图数组    private Bitmap fishBmp[] = new Bitmap[10];    //记录当前播放帧    private int currentFrame;    //    /**     * SurfaceView初始化函数     */    public MySurfaceView(Context context) {        super(context);        sfh = this.getHolder();        sfh.addCallback(this);        paint = new Paint();        paint.setColor(Color.WHITE);        paint.setAntiAlias(true);        setFocusable(true);        //将每张小鱼帧图生成位图存入小鱼帧数组中        for (int i = 0; i < fishBmp.length; i++) {            fishBmp[i] = BitmapFactory.decodeResource(this.getResources(), R.drawable.fish0+ i);        }    }    /**     * SurfaceView视图创建,响应此函数     */    @Override    public void surfaceCreated(SurfaceHolder holder) {        screenW = this.getWidth();        screenH = this.getHeight();        flag = true;        //实例线程        th = new Thread(this);        //启动线程        th.start();    }    /**     * 游戏绘图     */    public void myDraw() {        try {            canvas = sfh.lockCanvas();            if (canvas != null) {                canvas.drawColor(Color.WHITE);                canvas.drawBitmap(fishBmp[currentFrame], 0, 0, paint);            }        } catch (Exception e) {            // TODO: handle exception        } finally {            if (canvas != null)                sfh.unlockCanvasAndPost(canvas);        }    }    /**     * 触屏事件监听     */    @Override    public boolean onTouchEvent(MotionEvent event) {        return true;    }    /**     * 按键事件监听     */    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        return super.onKeyDown(keyCode, event);    }    /**     * 游戏逻辑     */    private void logic() {        currentFrame++;        if (currentFrame >= fishBmp.length) {            currentFrame = 0;        }    }    @Override    public void run() {        while (flag) {            long start = System.currentTimeMillis();            myDraw();            logic();            long end = System.currentTimeMillis();            try {                if (end - start < 50) {                    Thread.sleep(50 - (end - start));                }            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }    /**     * SurfaceView视图状态发生改变,响应此函数     */    @Override    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {    }    /**     * SurfaceView视图消亡时,响应此函数     */    @Override    public void surfaceDestroyed(SurfaceHolder holder) {        flag = false;    }}
登录后复制

MainActivity类:

package com.fm;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.view.WindowManager;public class MainActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //设置全屏        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);        requestWindowFeature(Window.FEATURE_NO_TITLE);        //显示自定义的SurfaceView视图        setContentView(new MySurfaceView(this));    }}
登录后复制

 

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板