版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
精品文档-下载后可编辑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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年AI医疗智能机器人对话系统设计题库
- 2026年医药知识测试题库药物知识与用法
- 2026年地方史与地理常识综合测试题
- 2026年管理学企业团队建设与管理策略题
- 消防分项质量控制、检查、部验制度
- 校园安保设备使用保管制度
- 财务管理软件操作规范(标准版)
- 2025四川宜宾钲兴智造科技有限公司第三批项目制员工招聘7人笔试历年难易错考点试卷带答案解析2套试卷
- 2025四川国检检测有限责任公司人员招聘5人笔试参考题库附带答案详解
- 2025四川华丰科技股份有限公司招聘销售经理岗位测试笔试历年备考题库附带答案详解
- DL∕T 2609-2023 主动干预型消弧装置验收运维规范
- AQ/T 9009-2015 生产安全事故应急演练评估规范(正式版)
- DZ∕T 0211-2020 矿产地质勘查规范 重晶石、毒重石、萤石、硼(正式版)
- HG-T 20583-2020 钢制化工容器结构设计规范
- T-SHNA 0004-2023 有创动脉血压监测方法
- 缅甸矿产资源分布情况
- 建设方承包方和劳务公司三方代发协议模板
- 产前筛查培训课件
- 项目管理能力考核汇总表
- 文化区发展策略研究-以香港西九龙文化区和牛棚艺术村为例
- GB/T 35263-2017纺织品接触瞬间凉感性能的检测和评价
评论
0/150
提交评论