android SurfaceView绘制 重新学习--基础绘制
自从大二写了个android游戏去参加比赛,之后就一直写应用,一直没用过SurfaceView了,现在进入了游戏公司,准备从基础开始重新快速的学一下这个,然后再去研究openGL和游戏引擎。
直接上代码吧:
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.SurfaceHolder;
import android.view.animation.Animation; public class MySurfaceView extends SurfaceView implements Callback, Runnable { private SurfaceHolder sfh;
private Thread th;
private Canvas canvas;
private Paint paint;
private boolean threadFlag; public MySurfaceView(Context context) {
// this(context, null); super(context);
sfh = this.getHolder();
sfh.addCallback(this);
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setTextSize(20);
Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
paint.setTypeface(font);
this.setKeepScreenOn(true);
} public MySurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
// th = new Thread(this);
// sfh = this.getHolder();
// sfh.addCallback(this);
// paint = new Paint();
// paint.setAntiAlias(true);
// paint.setColor(Color.RED);
// this.setKeepScreenOn(true);
} public void startAnimation(Animation animation) {
super.startAnimation(animation);
} public void surfaceCreated(SurfaceHolder holder) {
threadFlag = true;
th = new Thread(this);
th.start();
} private void draw() {
try {
canvas = sfh.lockCanvas(); // 获取画布
if (canvas != null) {
canvas.drawColor(Color.WHITE);// 画布颜色
canvas.drawText("奋斗的小猿", 100, 100, paint);
canvas.drawText("加班,还是不加班,it's a question!", 100, 130, paint);
}
} catch (Exception ex) {
} finally {
if (canvas != null)
sfh.unlockCanvasAndPost(canvas);
}
} public void run() {
while (threadFlag) {
Log.i("ceshi", "threadFlag=true");
draw();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
} public void surfaceDestroyed(SurfaceHolder holder) {
threadFlag = false;
} }

这里Activity用的是
setContentView(new MySurfaceView(getApplicationContext()));
如果想在布局中引用:
<com.tq.listviewtest.MySurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" >
</com.tq.listviewtest.MySurfaceView>
setContentView(R.layout.activity_main);
则构造方法应改为:
public MySurfaceView(Context context) {
this(context, null);
}
public MySurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
sfh = this.getHolder();
sfh.addCallback(this);
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setTextSize(20);
Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
paint.setTypeface(font);
this.setKeepScreenOn(true);
}
因为android 加载布局时,只是去执行了第二个构造方法,第一个构造方法没有执行,所以我们要把初始化写在第二个构造方法中了。
android SurfaceView绘制 重新学习--基础绘制的更多相关文章
- android SurfaceView绘制实现原理解析
在Android系统中,有一种特殊的视图,称为SurfaceView,它拥有独立的绘图表面,即它不与其宿主窗口共享同一个绘图表面.由于拥有独立的绘图表面,因此SurfaceView的UI就可以在一个独 ...
- Android SurfaceView入门学习
学习资料: Android 开发群英传 搜索学习资料时,搜到了罗升阳老师的Android视图SurfaceView的实现原理分析,老罗老师写的一系列博客,一年前开始学习Android时看不懂,现在依然 ...
- Android(java)学习笔记151: SurfaceView使用
1.SurfaceView简介 在一般的情况下,应用程序的View都是在相同的GUI线程(UI主线程)中绘制的.这个主应用程序线程同时也用来处理所有的用户交互(例如,按钮单击或者文本输入) ...
- Android UI性能优化实战 识别绘制中的性能问题
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/45556391: 本文出自:[张鸿洋的博客] 1.概述 2015年初google ...
- Android自定义视图二:如何绘制内容
这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...
- Android(java)学习笔记94: SurfaceView使用
1. SurfaceView简介 在一般的情况下,应用程序的View都是在相同的GUI线程(UI主线程)中绘制的.这个主应用程序线程同时也用来处理所有的用户交互(例如,按钮单击或者文本输入). ...
- 【Android端 APP GPU过度绘制】GPU过度绘制及优化
一.Android端的卡顿 Android端APP在具体使用的过程中容易出现卡顿的情况,比如查看页面时出现一顿一顿的感受,切换tab之后响应很慢,或者具体滑动操作的时候也很慢. 二.卡顿的原因 卡顿的 ...
- Android手势源码浅析-----手势绘制(GestureOverlayView)
Android手势源码浅析-----手势绘制(GestureOverlayView)
- MFC+OpenGL基础绘制<转>
转载地址:https://blog.csdn.net/u013232740/article/details/47904115 ------------------------------------- ...
随机推荐
- launchMode使用详解
launchMode是很基础但是也很容易被忽视的问题,一个高性能的手机App不仅仅是代码完成的非常棒,也包括launchMode的合理使用.一个应用中,到底哪些Activity应该始终保持一个实例,哪 ...
- mysqldump导出csv格式文件
mysqldump bstar -t -T/tmp Nvr --fields-enclosed-by=\" --fields-terminated-by=, --where=" ...
- Spring MVC的异常统一处理方法
我们经常需要统一配置项目的异常处理,又希望统一处理异常代码,同时不侵入原有的正常代码.我们可以通过以下三种方式实现统一处理项目的自定义异常. 通过SimpleMappingExceptionResol ...
- linux服务器下tomcat部署项目内存溢出
今天将一个项目部署到linux服务器上,结果tomcat在启动运行到一定时间后就报错.根据以往的经验,查了一些资料,终于解决了该问题并顺便解决了生产环境中的get方式中文传递乱码问题. tomcat启 ...
- hibernate篇章一
我只想说一句话FUCK! 昨晚查了一晚上资料. 今早细致勃勃的准备搭建环境,早上到现在失败!失败!失败!蛋疼 有兴趣的可以试试.我暂时搁置,去做分页了 有兴趣需要相关资料的可以查阅我的微博java66 ...
- Bye 14 Hello 15
打开博客.空间 窥探到大家都在写自己的2014,抬头一看日历2015已近在咫尺了,看着别人的成长(例如 今年看了多少书.做了什么项目.工资涨了多少.职位角色的变化.去了多少地方.还有一些发善 ...
- jasper
package jasper; import java.util.ArrayList;import java.util.HashMap;import java.util.Map; import net ...
- PHP和MYSQL的编码问题
http://blog.csdn.net/martinkro/article/details/5352474 1 MYSQL中的字符集概念 Mysql的字符集里有两个概念,一个是"Char ...
- html调用applet
1.相同目录下 <applet code="*.class" width=250 height=50> </applet> 指定applet类名称,appl ...
- HTML解析引擎:Jumony
Jumony Core首先提供了一个近乎完美的HTML解析引擎,其解析结果无限逼近浏览器的解析结果.不论是无结束标签的元素,可选结束标签的元素,或是标记属性,或是CSS选择器和样式,一切合法的,不合法 ...