package com.example.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView; /**
* 继承SurfaceView
* 实现SurfaceHolder.Callback。给SurfaceHolder用的
* 创建SurfaceHolder。并addCallback,SurfaceView的一切都有SurfaceHolder控制
*/
public class MySurfaceView extends SurfaceView
implements Callback, Runnable {
//SurfaceView基本
private SurfaceHolder sfh;//操控SurfaceView
private Paint p;
private Canvas c;
//线程
private Thread t;
private boolean flag;
//数据
private int screenW, screenH;
private int xx = 100, yy = 100; public MySurfaceView(Context context) {
super(context);
sfh = this.getHolder();
sfh.addCallback(this);
p = new Paint();
p.setColor(Color.RED);
} @Override
public boolean onTouchEvent(MotionEvent event) {
xx = (int) event.getX();
yy = (int) event.getY();
return true;
} public void myDraw() {
try {
//当SurfaceView不可编辑或尚未创建。会返回null
c = sfh.lockCanvas();
if(c != null) {
c.drawColor(Color.GREEN);//刷屏
c.drawText("me", xx, yy, p);//画
}
} catch(Exception e) { } finally {
if(c != null) {
sfh.unlockCanvasAndPost(c);
}
}
} //游戏逻辑
public void logic() {
} @Override
public void surfaceCreated(SurfaceHolder holder) {
screenW = this.getWidth();
screenH = this.getHeight();
//线程的初始化和启动都写在create里面
flag = true;
t = new Thread(this);
t.start();
} @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
} @Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
} @Override
public void run() {
while (flag) {
//刷屏时间保持一致
//通常会在10~20帧左右
//每秒20帧:1000ms/20=50ms每帧
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的生命周期:

SurfaceView的经典写法的更多相关文章

  1. Tkinter经典写法

    1.继承 tkinter.Frame 类,实现类的基本写法 2.创建主窗口及主窗口大小位置及标题 3.将需要添加的组件放入到类中进行创建, 继承的 Frame 类需要使用 master 参数作为父类的 ...

  2. javascript js表示中文日期的经典写法

    <div id="time"> <script>document.getElementById('time').innerHTML=new Date().t ...

  3. 下拉菜单得经典写法html5

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 操作argc, argv的经典写法

    [问题] 我在看boost源代码的时候看到如下的代码, template<class charT> basic_command_line_parser<charT>:: bas ...

  5. Android SurfaceView实战 带你玩转flabby bird (上)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42965779 ,本文出自:[张鸿洋的博客] 1.概述 哈,记得以前写过Andro ...

  6. 中间自适应,左右定宽的两种经典布局 ---- 圣杯布局 VS 双飞翼布局

    一.引子 最近学了些js框架,小有充实感,又深知如此节奏的前提需得基础扎实,于是回头想将原生CSS和Javascript回顾总结一番,先从CSS起,能集中它的就在基础的布局上,便查阅了相关资料,将布局 ...

  7. Checkbutton基本写法

    1.Checkbutton(self 窗口对象,text 按钮显示内容,variable 绑定变量->一起变化, onvalue 用户点击时得到的值,offvalue 没有点击得到的值) sel ...

  8. Label 组件基本写法

    1.width,height 指定区域大小 文本 汉字 2 个字节 2.font 指定字体和字体大小 font(font_name,size) 3.image 显示在 Label 上的图像 支持 gi ...

  9. c#.net单例模式的学习记录!

    一. 单例(Singleton)模式 单例模式的特点: 单例类只能有一个实例. 单例类必须自己创建自己的唯一实例. 单例类必须给所有其它对象提供这一实例. 单例模式应用: 每台计算机可以有若干个打印机 ...

随机推荐

  1. React Native - 2 控件Flexbox

    *强烈建议使用Genymotion模拟器,比AVD速度快,功能强大.   1. flexDirection Flexbox是连续布局,它有主轴(primary axis)和交叉轴(cross axis ...

  2. [转]认识session

    今天想用一个session来实现用户登录判断,也算是对之前session的探究,查了下资料session的运行机制如下: session是服务器端的一种会话机制,当客户端的请求服务器创建一个sessi ...

  3. php关键字

    \b( (a(bstract|nd|rray|s))| (c(a(llable|se|tch)|l(ass|one)|on(st|tinue)))| (d(e(clare|fault)|ie|o))| ...

  4. A - Enterprising Escape 【BFS+优先队列+map】

    The Enterprise is surrounded by Klingons! Find the escape route that has the quickest exit time, and ...

  5. Hibernate所有缓存机制详解

    hibernate提供的一级缓存 hibernate是一个线程对应一个session,一个线程可以看成一个用户.也就是说session级缓存(一级缓存)只能给一个线程用,别的线程用不了,一级缓存就是和 ...

  6. 【动态规划】bzoj3992 [Sdoi2015]序列统计 10分

    #include<cstdio> using namespace std; #define MOD 1004535809 int a[8001],f[1001][101],n,m,x,S; ...

  7. 【dfs】【高斯消元】【异或方程组】bzoj1770 [Usaco2009 Nov]lights 燈 / bzoj2466 [中山市选2009]树

    经典的开关灯问题. 高斯消元后矩阵对角线B[i][i]若是0,则第i个未知数是自由元(S个),它们可以任意取值,而让非自由元顺应它们,得到2S组解. 枚举自由元取0/1,最终得到最优解. 不知为何正着 ...

  8. 【最短路】【spfa】小vijos P1447 Updown

    小vijos P1447 Updown 背景 开启了升降梯的动力之后,探险队员们进入了升降梯运行的那条竖直的隧道,映入眼帘的是一条直通塔顶的轨道.一辆停在轨道底部的电梯.和电梯内一杆控制电梯升降的巨大 ...

  9. 【set】bzoj2761 [JLOI2011]不重复数字

    set去重. #include<cstdio> #include<set> using namespace std; set<int>S; ],b[],en; in ...

  10. 英尺和米之间的转换 Exercise06_09

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:英尺和米之间的转换 * */ public class Exercise06_09 { public static void ...