




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
精品文档-下载后可编辑SurfaceView的双缓冲使用AndroidAndroid一词的本义指“机器人”,同时也是Google于2022年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统、中间件、用户界面和应用软件组成,号称是为移动终端打造的真正开放和完整的移动软件。目前,版本为Android2.4Gingerbread和Android3.0Honeycomb。
Android是基于Linux开放性内核的操作系统,是Google公司在2022年11月5日公布的手机操作系统。早期由原名为"Android"的公司开发,谷歌在2022年收购"Android.Inc"后,继续进行对Android系统开发运营,它采用了软件堆层的架构,主要分为三部分。底层Linux内核只提供基本功能,其他的应用软件则由各公司自行开发,部分程序以Java编写。
这次介绍SurfaceView的双缓冲使用。双缓冲是为了防止动画闪烁而实现的一种多线程应用,基于SurfaceView的双缓冲实现很简单,开一条线程并在其中绘图即可。本文介绍基于SurfaceView的双缓冲实现,以及介绍类似的更高效的实现方法。
本文程序运行截图如下,左边是开单个线程读取并绘图,右边是开两个线程,一个专门读取图片,一个专门绘图:
对比一下,右边动画的帧速明显比左边的快,左右两者都没使用Thread.sleep()。为什么要开两个线程一个读一个画,而不去开两个线程像左边那样都“边读边画”呢?因为SurfaceView每次绘图都会锁定Canvas,也就是说同一片区域这次没画完下次就不能画,因此要提高双缓冲的效率,就得开一条线程专门画图,开另外一条线程做预处理的工作。
main.xml的源码:
viewplaincopytoclipboardprint?
android:orientation="vertical"
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:layout_height="wrap_content"android:text="单个独立线程"android:layout_height="wrap_content"android:text="两个独立线程"android:layout_width="fill_parent"android:layout_height="fill_parent"
android:layout_width="fill_parent"android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:layout_height="wrap_content"android:text="单个独立线程"android:layout_height="wrap_content"android:text="两个独立线程"
android:layout_width="fill_parent"android:layout_height="fill_parent"
本文程序的源码:
viewplaincopytoclipboardprint?
packagecom.testSurfaceView;
importjava.lang.reflect.Field;
importjava.util.ArrayList;
importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.Paint;
importandroid.graphics.Rect;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.SurfaceHolder;
importandroid.view.SurfaceView;
importandroid.view.View;
importandroid.widget.Button;
publicclasstestSurfaceViewextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
ButtonbtnSingleThread,btnDoubleThread;
SurfaceViewsfv;
SurfaceHoldersfh;
ArrayListimgList=newArrayList();
intimgWidth,imgHeight;
Bitmapbitmap;//独立线程读取,独立线程绘图
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSingleThread=(Button)this.findViewById(R.id.Button01);
btnDoubleThread=(Button)this.findViewById(R.id.Button02);
btnSingleThread.setOnClickListener(newClickEvent());
btnDoubleThread.setOnClickListener(newClickEvent());
sfv=(SurfaceView)this.findViewById(R.id.SurfaceView01);
sfh=sfv.getHolder();
sfh.addCallback(newMyCallBack());//自动运行surfaceCreated以及surfaceChanged
}
classClickEventimplementsView.OnClickListener{
@Override
publicvoidonClick(Viewv){
if(v==btnSingleThread){
newLoad_DrawImage(0,0)。start();//开一条线程读取并绘图
}elseif(v==btnDoubleThread){
newLoadImage()。start();//开一条线程读取
newDrawImage(imgWidth+10,0)。start();//开一条线程绘图
}
}
}
classMyCallBackimplementsSurfaceHolder.Callback{
@Override
publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,
intheight){
Log.i("Surface:","Change");
}
@Override
publicvoidsurfaceCreated(SurfaceHolderholder){
Log.i("Surface:","Create");
//用反射机制来获取资源中的图片ID和尺寸
Field[]fields=R.drawable.class.getDeclaredFields();
for(Fieldfield:fields){
if(!"icon".equals(field.getName()))//除了icon之外的图片
{
intindex=0;
try{
index=field.getInt(R.drawable.class);
}catch(IllegalArgumentExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IllegalAccessExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
//保存图片ID
imgList.add(index);
}
}
//取得图像大小
BitmapbmImg=BitmapFactory.decodeResource(getResources(),
imgList.get(0));
imgWidth=bmImg.getWidth();
imgHeight=bmImg.getHeight();
}
@Override
publicvoidsurfaceDestroyed(SurfaceHolderholder){
Log.i("Surface:","Destroy");
}
}
/*
*读取并显示图片的线程
*/
classLoad_DrawImageextendsThread{
intx,y;
intimgIndex=0;
publicLoad_DrawImage(intx,inty){
this.x=x;
this.y=y;
}
publicvoidrun(){
while(true){
Canvasc=sfh.lockCanvas(newRect(this.x,this.y,this.x
+imgWidth,this.y+imgHeight));
BitmapbmImg=BitmapFactory.decodeResource(getResources(),
imgList.get(imgIndex));
c.drawBitmap(bmImg,this.x,this.y,newPaint());
imgIndex++;
if(imgIndex==imgList.size())
imgIndex=0;
sfh.unlockCanvasAndPost(c);//更新屏幕显示内容
}
}
};
/*
*只负责绘图的线程
*/
classDrawImageextendsThread{
intx,y;
publicDrawImage(intx,inty){
this.x=x;
this.y=y;
}
publicvoidrun(){
while(true){
if(bitmap!=null){//
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《第四章 第5节 光的色散》教学设计-2023-2024学年初中物理人教版八年级上册
- 2025年浸酸剂项目合作计划书
- 零阶能量有限元方法:船舶结构声辐射领域的理论突破与应用探索
- 2025年制动总泵项目发展计划
- 2025年多级飘尘采样计项目合作计划书
- 中国沿海地区城镇用地时空演变及驱动因素:基于多维度视角的剖析
- 八年级物理上册 第1章 第4节测量平均速度教学实录 (新版)新人教版
- 高三数学上学期第十九周 空间向量及其加减 数乘和数量积运算教学设计
- Unit 7 Section B(1a-2b) 教学设计 2024-2025学年人教版(2024年)英语七年级上册
- 安史之乱与唐朝衰亡课件+2024-2025学年统编版七年级历史下册
- 校园春季传染病预防
- 医院危险化学品安全管理
- 2024年劳动合同(30篇)
- 燃气公司安全生产实施方案
- 2024年安全员理论考试题库附答案解析
- 【非洲出海专题】2024年摩洛哥投资环境深度分析及中资 企业在摩洛哥投资合作策略
- 新闻记者职业资格《新闻基础知识》考试题库(含答案)
- 2024解析:第十四章内能的利用-基础练(解析版)
- 《制造业信息化》课件
- 2024年度股权激励代持协议
- 北师大版(2024新版)七年级上册数学全册教案
评论
0/150
提交评论