SurfaceView概述和基本使用
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概述和基本使用的更多相关文章
- Android SurfaceView概述
简介:SurfaceView继承自View,但它与View不同,View是在UI主线程中更新画面,而SurfaceView是在一个新线程中更新画面,View的特性决定了其不适合做动画,因为如果更新画面 ...
- [安卓] 12、开源一个基于SurfaceView的飞行射击类小游戏
前言 这款安卓小游戏是基于SurfaceView的飞行射击类游戏,采用Java来写,没有采用游戏引擎,注释详细,条理比较清晰,适合初学者了解游戏状态转化自动机和一些继承与封装的技巧. 效果展示 ...
- Android应用开发基础篇(8)-----SurfaceView
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/25/2368069.html 一.概述 SurfaceView也是一个用来画图的部件,不过由于它 ...
- Android SurfaceView实战 带你玩转flabby bird (下)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43063331,本文出自:[张鸿洋的博客] 1.概述 在Android Surfa ...
- Android SurfaceView实战 带你玩转flabby bird (上)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42965779 ,本文出自:[张鸿洋的博客] 1.概述 哈,记得以前写过Andro ...
- Android SurfaceView实战 打造抽奖转盘
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41722441 ,本文出自:[张鸿洋的博客] 1.概述 今天给大家带来Surfac ...
- Android群英传笔记——摘要,概述,新的出发点,温故而知新,可以为师矣!
Android群英传笔记--摘要,概述,新的出发点,温故而知新,可以为师矣! 当工作的越久,就越感到力不从心了,基础和理解才是最重要的,所以买了两本书,医生的<Android群英传>和主席 ...
- android SurfaceView绘制实现原理解析
在Android系统中,有一种特殊的视图,称为SurfaceView,它拥有独立的绘图表面,即它不与其宿主窗口共享同一个绘图表面.由于拥有独立的绘图表面,因此SurfaceView的UI就可以在一个独 ...
- Android视图SurfaceView的实现原理分析(示例,出错代码)
在Android系统中,有一种特殊的视图,称为SurfaceView,它拥有独立的绘图表面,即它不与其宿主窗口共享同一个绘图表面.由于拥有独立的绘图表面,因此SurfaceView的UI就可以在一个独 ...
随机推荐
- Python_算术运算符
1.算术运算符 示例: >>> num1=7 >>> num2=3 >>> num1+num2 #+ 10 >>> num1-n ...
- SSH安全优化
更改远程连接登陆的端口 禁止root管理员直接登陆 密码认证方式改为密钥认证 重要服务不使用公网IP地址 使用防火墙来限制来源IP地址 Port 666 变更SSH服务远 ...
- 备份一下我的88bugs的application文档
# 服务端口 server.port=8083 #spring.mvc.favicon.enabled=false server.servlet.context-path=/bug/ dateform ...
- mybatis遍历map
mytabis是可以遍历map的,试过很多其他的方法都不行,最终使用如下方法是可以的: 1.mapper.java如下(注意要加@Param注解,否则不行,我就在这里折腾了不少时间): int upd ...
- pid 及参数调试方法
所谓PID指的是Proportion-Integral-Differential.翻译成中文是比例-积分-微分. 记住两句话: 1.PID是经典控制(使用年代久远) 2.PID是误差控制() 对直流电 ...
- LOJ P10147 石子合并 题解
Analysis 区间dp+前缀和 #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- ES 的基本用法
ES的基本用法 ES的基本概念 1> 集群和节点 一个es集群是由一个或多和es节点组成的集合 每一个集群都有一个名字, 如之前的wali 每个节点都有自己的名字, 如之前的master, sl ...
- Cashe的使用
1.CacheHelper public class CacheHelper { public static ObjectCache Cache { get { return MemoryCache. ...
- TensorFlow(三):非线性回归
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # 非线性回归 # 使用numpy生成200个随机 ...
- windows使用强大的wget工具
原文链接:https://www.cnblogs.com/hzdx/p/6432161.html wget下载地址:http://www.interlog.com/~tcharron/wgetwin. ...