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 ------------------------------------- ...
随机推荐
- C#泛型对类型参数的推断
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Web Services之SOAP学习
Web Services之SOAP [toc] 什么是SOAP SOAP(Simple Object Access Protocol)简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一 ...
- sklearn两种保存模型的方式
作者:卢嘉颖 链接:https://www.zhihu.com/question/27187105/answer/97334347 来源:知乎 著作权归作者所有,转载请联系作者获得授权. 1. pic ...
- Top 10 Uses of a Message Queue
Top 10 Uses of a Message QueueAsynchronicity, Work Dispatch, Load Buffering, Database Offloading, an ...
- Devexpress 使用经验 —— ASPxGridView命令行自定义按钮灵活使用
ASPX <dx:ASPxGridView ID="ASPxGridView1" runat="server" DataSourceID="Ob ...
- Asp.net中前台javascript与后台C#交互
方法一:使用Ajax开发框架,后台方法定义前添加[AjaxPro.AjaxMethod],然后就可以在前台js脚本中调用后台C#函数. 方法二:后台方法声明为public或者protected,然后前 ...
- Conversion to Dalvik format failed:Unable toexecute dex: method ID not in [0, 0xffff]: 65536
关于方法数超限,Google官方给出的方案是这样的:https://developer.android.com/intl/zh-cn/tools/building/multidex.html 我也写过 ...
- ASP.NET问题处理---“数据请求超时错误“”
数据请求超时,一般有2中解决方式: 1.页面AJAX处理数据时延长时间: 2.后台数据库连接取数据时延长时间. 由于我的后台数据库连接取数据为循环读取数据,所以不存在超时问题,这里具体说说如何修改AJ ...
- Spring.net架构示例(含Aop和Ioc)源码
最近写了一个Spring.net的架构. 一.架构主图 架构图的数据流程走向是: UI层=>UILogic>=>Service>Business=>DataAccess ...
- 二、 What's Maven,How to learning?
1. 哈哈,什么是Maevn, ←_←|| ?我怎么知道,来看看官方解释, Apache Maven is a software project management and comprehensio ...