Android屏幕刷新一遍时间间隔为16ms,如果view能够在16ms内完成所需要执行的绘图换作,那么在视觉上,界面就是流畅的,否则就会出现卡顿现象,在很多情况下,这些逻辑处理又是必须的,为了解决这个问题,Android 引用了surfaceView,在二个方面改进了View的绘图操作:

使用双缓冲技术

自带画布,支持在子线程中更新画布内容

view和surfaceView使用场景:

当界面需要被动更新时,用view较好,比如,与手势交互的场景,因为画面的更新是依赖ontouch来完成,所以可以直接使用invalidate函数,在这种一次touch和下一次touch时间长的话,就不会产生影响

当界面需要主动刷新,使用surfaceView比较好,比如视频播放摄像头等

基本用法:

surfaceView派生View

package com.loaderman.customviewdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceView; public class ViewGesturePath extends SurfaceView {
private Paint mPaint;
private Path mPath;
public ViewGesturePath(Context context) {
super(context);
init();
}
public ViewGesturePath(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ViewGesturePath(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} private void init(){
setWillNotDraw(false);
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5);
mPaint.setColor(Color.RED); mPath = new Path();
} @Override
public boolean onTouchEvent(MotionEvent event) { int x = (int)event.getX();
int y = (int)event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN){
mPath.moveTo(x,y);
return true;
}else if (event.getAction() == MotionEvent.ACTION_MOVE){
mPath.lineTo(x,y);
}
postInvalidate(); return super.onTouchEvent(event);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mPath,mPaint); }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#ffffff"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_red_dark"
android:text="捕捉手势轨迹,画吧,少年"/>
<com.loaderman.customviewdemo.ViewGesturePath
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

效果:

正确用法:

package com.loaderman.customviewdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView; public class SurfaceViewGesturePath extends SurfaceView {
private Paint mPaint;
private Path mPath; public SurfaceViewGesturePath(Context context) {
super(context);
init();
} public SurfaceViewGesturePath(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public SurfaceViewGesturePath(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} private void init() {
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5);
mPaint.setColor(Color.RED); mPath = new Path(); } @Override
public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getX();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mPath.moveTo(x, y);
return true;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
mPath.lineTo(x, y);
}
drawCanvas(); return super.onTouchEvent(event);
} private void drawCanvas() {
new Thread(new Runnable() {
@Override
public void run() {
SurfaceHolder surfaceHolder = getHolder();
Canvas canvas = surfaceHolder.lockCanvas();//使用缓冲Canvas绘图 canvas.drawPath(mPath, mPaint); surfaceHolder.unlockCanvasAndPost(canvas);
}
}).start();
}
}

效果同上

SurfaceView概述和基本使用的更多相关文章

  1. Android SurfaceView概述

    简介:SurfaceView继承自View,但它与View不同,View是在UI主线程中更新画面,而SurfaceView是在一个新线程中更新画面,View的特性决定了其不适合做动画,因为如果更新画面 ...

  2. [安卓] 12、开源一个基于SurfaceView的飞行射击类小游戏

    前言  这款安卓小游戏是基于SurfaceView的飞行射击类游戏,采用Java来写,没有采用游戏引擎,注释详细,条理比较清晰,适合初学者了解游戏状态转化自动机和一些继承与封装的技巧. 效果展示    ...

  3. Android应用开发基础篇(8)-----SurfaceView

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/25/2368069.html 一.概述 SurfaceView也是一个用来画图的部件,不过由于它 ...

  4. Android SurfaceView实战 带你玩转flabby bird (下)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43063331,本文出自:[张鸿洋的博客] 1.概述 在Android Surfa ...

  5. Android SurfaceView实战 带你玩转flabby bird (上)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42965779 ,本文出自:[张鸿洋的博客] 1.概述 哈,记得以前写过Andro ...

  6. Android SurfaceView实战 打造抽奖转盘

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41722441 ,本文出自:[张鸿洋的博客] 1.概述 今天给大家带来Surfac ...

  7. Android群英传笔记——摘要,概述,新的出发点,温故而知新,可以为师矣!

    Android群英传笔记--摘要,概述,新的出发点,温故而知新,可以为师矣! 当工作的越久,就越感到力不从心了,基础和理解才是最重要的,所以买了两本书,医生的<Android群英传>和主席 ...

  8. android SurfaceView绘制实现原理解析

    在Android系统中,有一种特殊的视图,称为SurfaceView,它拥有独立的绘图表面,即它不与其宿主窗口共享同一个绘图表面.由于拥有独立的绘图表面,因此SurfaceView的UI就可以在一个独 ...

  9. Android视图SurfaceView的实现原理分析(示例,出错代码)

    在Android系统中,有一种特殊的视图,称为SurfaceView,它拥有独立的绘图表面,即它不与其宿主窗口共享同一个绘图表面.由于拥有独立的绘图表面,因此SurfaceView的UI就可以在一个独 ...

随机推荐

  1. hive之建立分区表和分区

    1. 建立分区表 create table 单分区表:其中分区字段是partdate,注意分区字段不能和表字段一样,否则会报重复的错 create table test_t2(words string ...

  2. Linux学习之四-Linux发行版及版本比较

    Linux发行版及版本比较 三大家族: Fedora是基于RHEL,CentOS,Scientific Linux, 和Oracle Linux的社区版本.相比RHEL,Fedora打包了显著的更多的 ...

  3. KVM虚拟机的管理

    1.  查看KVM虚拟机配置文件及运行状态 (1) KVM虚拟机默认配置文件位置: /etc/libvirt/qemu/ autostart目录是配置kvm虚拟机开机自启动目录 (2) virsh命令 ...

  4. DDD总览

    DDD总览 领域驱动设计(DDD)编码实践   目录 写在前面DDD总览实现业务的3种常见方式基于业务的分包领域模型的门面——应用服务业务的载体——聚合根实体 vs 值对象聚合根的家——资源库创生之柱 ...

  5. SecureCRT中解决乱码的问题

    SecureCRT中文乱码的问题,解决方法如下: 打开Option菜单,点击Session Options-     在Appearance外观这里,选择编码--UTF-8     一定要记得先保存! ...

  6. LightOJ-1008-Fibsieve`s Fantabulous Birthday(推公式)

    链接: https://vjudge.net/problem/LightOJ-1008 题意: Fibsieve had a fantabulous (yes, it's an actual word ...

  7. 使用$.getJSON("xx.json" ,function(data){ console.logy(data); })在本地获取 json数据

    使用 $.getJSON() 在本地获取 json数据  默认在谷歌,火狐等其他浏览器,不允许在本地获取服务器数据 所以不能在本地中使用 但是可以IE 11 中又可以使用,所以,测试时建议在IE浏览器 ...

  8. window.external 是调用外部方法

    ie中,window.external 是调用外部方法,比如,是在 winform 中的 webbrower 中使用 window.external.SendData(),那么,SendData()  ...

  9. Oracle语句中IN和=的区别有哪些?

    Oracle语句中IN和=的区别有: 1.首先应用范围不一样:in 可以理解为是范围内的选择:= 只有一个.例如: select sno, sname from t1 where sno in ('s ...

  10. PHP全栈学习笔记25

    <?php /* *@Author: 达叔小生 **/ header("content-type:image/png"); //设置页面编码 $num = $_GET['nu ...