21天学习android开发教程之SurfaceView与多线程的混搭
作者:aishu 发布时间:2021-07-22 05:25:09
标签:android,SurfaceView,多线程
上一篇简单介绍了SurfaceView的基本使用,这次就介绍SurfaceView与多线程的混搭。SurfaceView与多线程混搭,是为了防止动画闪烁而实现的一种多线程应用。android的多线程用法与JAVA的多线程用法完全一样,本文不做多线程方面的介绍了。直接讲解SurfaceView与多线程的混合使用,即开一条线程专门读取图片,另外一条线程专门绘图。
本文程序运行截图如下,左边是开单个线程读取并绘图,右边是开两个线程,一个专门读取图片,一个专门绘图:
对比一下,右边动画的帧速明显比左边的快,左右两者都没使用Thread.sleep()。为什么要开两个线程一个读一个画,而不去开两个线程像左边那样都“边读边画”呢?因为SurfaceView每次绘图都会锁定Canvas,也就是说同一片区域这次没画完下次就不能画,因此要提高动画播放的效率,就得开一条线程专门画图,开另外一条线程做预处理的工作。
main.xml的源码:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<linearlayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="单个独立线程">
<button android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="两个独立线程">
<surfaceview android:id="@+id/SurfaceView01"
android:layout_width="fill_parent" android:layout_height="fill_parent">
本文程序的源码:
package com.testSurfaceView;
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class testSurfaceView extends Activity {
/** Called when the activity is first created. */
Button btnSingleThread, btnDoubleThread;
SurfaceView sfv;
SurfaceHolder sfh;
ArrayList imgList = new ArrayList();
int imgWidth, imgHeight;
Bitmap bitmap;//独立线程读取,独立线程绘图
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSingleThread = (Button) this.findViewById(R.id.Button01);
btnDoubleThread = (Button) this.findViewById(R.id.Button02);
btnSingleThread.setOnClickListener(new ClickEvent());
btnDoubleThread.setOnClickListener(new ClickEvent());
sfv = (SurfaceView) this.findViewById(R.id.SurfaceView01);
sfh = sfv.getHolder();
sfh.addCallback(new MyCallBack());// 自动运行surfaceCreated以及surfaceChanged
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
if (v == btnSingleThread) {
new Load_DrawImage(0, 0).start();//开一条线程读取并绘图
} else if (v == btnDoubleThread) {
new LoadImage().start();//开一条线程读取
new DrawImage(imgWidth + 10, 0).start();//开一条线程绘图
}
}
}
class MyCallBack implements SurfaceHolder.Callback {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.i("Surface:", "Change");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i("Surface:", "Create");
// 用反射机制来获取资源中的图片ID和尺寸
Field[] fields = R.drawable.class.getDeclaredFields();
for (Field field : fields) {
if (!"icon".equals(field.getName()))// 除了icon之外的图片
{
int index = 0;
try {
index = field.getInt(R.drawable.class);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 保存图片ID
imgList.add(index);
}
}
// 取得图像大小
Bitmap bmImg = BitmapFactory.decodeResource(getResources(),
imgList.get(0));
imgWidth = bmImg.getWidth();
imgHeight = bmImg.getHeight();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("Surface:", "Destroy");
}
}
/*
* 读取并显示图片的线程
*/
class Load_DrawImage extends Thread {
int x, y;
int imgIndex = 0;
public Load_DrawImage(int x, int y) {
this.x = x;
this.y = y;
}
public void run() {
while (true) {
Canvas c = sfh.lockCanvas(new Rect(this.x, this.y, this.x
+ imgWidth, this.y + imgHeight));
Bitmap bmImg = BitmapFactory.decodeResource(getResources(),
imgList.get(imgIndex));
c.drawBitmap(bmImg, this.x, this.y, new Paint());
imgIndex++;
if (imgIndex == imgList.size())
imgIndex = 0;
sfh.unlockCanvasAndPost(c);// 更新屏幕显示内容
}
}
};
/*
* 只负责绘图的线程
*/
class DrawImage extends Thread {
int x, y;
public DrawImage(int x, int y) {
this.x = x;
this.y = y;
}
public void run() {
while (true) {
if (bitmap != null) {//如果图像有效
Canvas c = sfh.lockCanvas(new Rect(this.x, this.y, this.x
+ imgWidth, this.y + imgHeight));
c.drawBitmap(bitmap, this.x, this.y, new Paint());
sfh.unlockCanvasAndPost(c);// 更新屏幕显示内容
}
}
}
};
/*
* 只负责读取图片的线程
*/
class LoadImage extends Thread {
int imgIndex = 0;
public void run() {
while (true) {
bitmap = BitmapFactory.decodeResource(getResources(),
imgList.get(imgIndex));
imgIndex++;
if (imgIndex == imgList.size())//如果到尽头则重新读取
imgIndex = 0;
}
}
};
}


猜你喜欢
- 本文实例讲述了C#实现的文件上传下载工具类。分享给大家供大家参考,具体如下:这里给出的工具类是在VS2013环境下采用C#语言实现文件上传、
- 1.ArrayList以数组实现。节约空间,但数组有容量限制。超出限制时会增加50%容量,用System.arraycopy()复制到新的数
- Java的外部类为什么不能使用private和protected进行修饰对于这个问题,一直没有仔细思考,今天整理一下:对于顶级类(外部类)来
- Socket里面的协议解析是Socket通讯程序设计中最复杂的地方,如果你的应用层协议设计或实现不佳,Socket通讯中常见的粘包,分包就难
- 1.RSA加密算法是一种非对称加密算法。在公开密钥加密和电子商业中RSA被广泛使用。RSA公开密钥密码体制。所谓的公开密钥密码体制就是使用不
- 在Android Studio 2.1 Preview 3之后,官方开始支持双向绑定了。可惜目前Google并没有在Data Binding
- 1. 为什么写这篇文章?事情是这样的,在 2021年6月10日早上我在CSDN上发布了文章《你真的懂Java怎么输出Hello World吗
- 前言感觉Jpa的动态构建查询不好使用,然后mybatis-plus没有动态构建表的功能,有没有可能使两者混合使用,利用Jpa自动建表的优势
- 如果标题栏过多,超过屏幕的宽度,该怎么弄,下面我们就来解决一下,效果如下:其实和之前写的也差不多,我就是在哪个demo里面添加和修改了一下,
- 前情提要:本demo是基于springboot+mybatis-plus实现加密,加密为主,全局异常处理,日志处理为辅,而登录密码加密是每个
- 一. 关于变量在之前的文章中,已经给大家详细地介绍过变量相关的内容,比如变量的概念、命名规范、变量的定义及底层原理等内容。但其实变量还有作用
- 前言:项目中我们经常会遇到有时候需要等待其他线程完成任务后,主线程才能执行其他任务,那么我们将如何实现呢?Join 解决方案join 的工作
- 一、File类1、简介java.io.File类:文件和文件目录路径的抽象表示形式,与平台无关 File 能新建、删除、重命名文件和目录,但
- 本文实例为大家分享了Unity虚拟摇杆的简单实现代码,供大家参考,具体内容如下简单的Unity虚拟摇杆实现,有详细注释。Game界面Insp
- 最近遇到了一个问题,一份很老的代码要修改里面的变量,源码早就和开发者一起不知去向,其中引用了一些jar包导致无法直接编译,只能直接修改.cl
- 前言嗯。最近工程上遇到一个byte数组转换为int的问题,解决过程中遇到了几个坑,经过各种查资料终于还是解决了。撒花。Java的位运算以及b
- 背景:重做系统后重新配置Android studio 安装虚拟机后无法启动log中显示为启动AVD的进程被杀控制台显示为:在虚拟机列表里没有
- 一、概念 工厂方法模式是类的创建模式,又叫虚
- 在android 6.0开始,部分的权限需要我们动态申请,也就是说当我们的打开app的时候系统不会主动像您申请app所需要的部分权限,需要客
- 网上杂七杂八的说法不一,大多数都是抄来抄去,没有实践,近期在项目频繁遇到boot+jackson处理日期的问题,故开此贴。首先是POM<