图形制作概述

Android途径供给丰盛的官方控件给开发者完毕界面UI开发,但在实践事务中经常会遇到林林总总的定制化需求,这有必要由开发者经过自绘控件的办法来完毕。一般Androidopengl和directx供给了CanvasOpenGL ES两种办法来完毕,其中Canvas仰仗于Android底层的Skia 2D向量图形处理函数库来完毕的。具体怎接口英文么通接口crc过错计数过Canvas和OpenGL来制作图形呢?这有必要依赖于Android供给的View类来具体完毕,下面组合几种常见的运用办法,如下所示:

Canvas

  • View +嵌套if函数 Canvas
  • SurfaceView + Canv接口as
  • TextureView + Canvaswindows10

Op接口自动化enGL ES

  • SurfaceView + OpenGL ES
  • GLSurfaceView + OpenGL ES
  • TextureView + OpenGL Ecanvas动画S

View + C接口和抽象类的差异anvas

这是一种一般运用的自绘控件办法,经过重写View类的onDraw(Canvas canvas)办法完毕。当需求改写制作图形时,调用invalidate()办法让View政策本身进行改写。该方案比较简单,触及自定义逻辑较少,缺点是制作逻辑在UI线程中进行,接口的作用改写功率不高,且不支持3D烘托。

public class CustomView extends View {
public Customopengl版别过低怎样办View(Context context) {
super(context);
}
public CustomView(Cwindows7旗舰版ontext context, @Nullable AttributeSet attropengles3.1扩展包s) {嵌套if函数
super(context, attrs);
}
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyle接口卡Attr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
// draw whatever.
}
}

SurfaceView + Canvas

这种办法接口英文相对于View + Canvas办法在于运用SurfaceView,因此会在Android的WMS体系上创立一块自己的Surface进行烘托制作,其制作逻辑能够在独立的线程中进行,因此功用相对于View + Canvas办法更高效。但一般情况下需求创立一个制作线程,以及完毕SurfaceHolder.Callback接口来管理Surfwindows7aceView的生命周期,其完毕逻辑比较View + Cacanvasnvas略凌嵌套分类汇总的操作过程乱。其他嵌套分类汇总它依然不支持3D烘托,且Surface因不在View hieopengl三重缓冲开不开rachy中,它的闪现也opengles3.1扩展包不受View的特色操控,所以不能进行平移,缩放等转换,也不能放在其windows10激活密钥它ViewGroup中,SurfaceView 不能嵌套运用。

public clascanvas登录s CustomSurfaceView extends Surfopengl下载aceView imcanvas登录plements SurfaceHolder.Callback, Runnable {
private boole嵌套是什么意思an mRunning = false;
private SurfaceHolder mSurfaceHoldercanvas登录;
p接口自动化ublic CustomSurfaceView(Context context)windows7 {
super(context);
initView()接口文档;
}
pCanvasublic CustomSurfaceView(Cont嵌套查询sql句子ext contextcanvas研讨, AttribcanvassinguteSet attrs) {
super(context, attr嵌套调用s);
initView();
}
public CustomSurfaceView(Coopengl三重缓冲开不开ntext context, Attcanvas研讨ributeSet attrs, int defStyleAttr) {
super(context, attrwindows是什么意思s, defStyleAttr);
initView();
}
private void initView() {
getHolder().addCallback(this);
}
@Ovopengl版别过低怎样办erride
publi接口文档c void surfaceCreated(SurfaceHolder holder) {
mSurfaceHolder = holder;
new Thread(this).start();
}
@Override
public void swindows更新有必要吗urfaceChanged(SurfaceHolder holder嵌套是什么意思, int format, int width, int height) {
mSurfaceHolder = holdwindows怎样激活er;
}
@Overr嵌套是什么意思ide
public void surfaceDestroyed(SurfaceHolder holder) {
mRunning = false;
}
@Override
pucanvas登录blic void run() {
mRunning = true;
while (mRunning) {
SystemClock嵌套函数.sleep(333);
Canopengl和directxvas canvas = mSurfaceHolder.lockCanvas();
if (canvas != null) {
try {
synchronizopengl渲染gpu怎样设置ed (mSurfaceHolder) {
onRender(canvas);
}
} finally {
mSurfaceHolder.unlockCanvasAndPoswindows未能发动t(canvas);
}
}
}
}
privatwindows体系e void onRender(Canvas canvas) {
// draw whatever.
}
}

TextureView + Canvas

该办法同SurfaceView + Can接口类型vas办法有些相似,但因为它canvas研讨是经过TextureView来完毕的,所以能够摒嵌套if函数接口文档Surface不在View hierachy中缺点,TextureView嵌套查询sql句子不会在WMS中独自创立窗口,而是作为View hierachy中的一个一般View,因此能够和其它一般View相同进行移动,canvassing旋转,缩放,动画等改动。这种办法也有本身缺点,它有必要在硬件opengl-legacy什么意思加快的接口crc过错计数窗口中才能运用,占用内存比SurfaceView要高,在5.0曾经在主UI线程烘托,5.0往后有独自的嵌套函数烘托线程。

public class CustomTextureView extend嵌套s TextureView implements TextureView.Surfcanvas研讨aceTextureListencanvas动画er, Runnable {
private booWindowslean mRunning = fa接口测验lse;windows10激活密钥
private SurfaceTexture mSurfaceTexture;
privatcanvas教育渠道e Surface mSurface;
privatewindows7 Rect mRect;
public CustomTextureView(Context context) {
super(context);
initView();
}
public CustomTextureView(Context context, AttributeSet attrs) {
super(windows是什么意思context, attrs);
initView();
}
public CustomTextureView(Context contex嵌套分类汇总的操作过程t, AttributeSet attrs, int defcanvas登录StyleAttr) {
super(context, attrscanvassing, defStyleAttr);
initView();
}
privatwindows许可证行将过期怎样办e void initView() {
setSurfaopengl三重缓冲开不开ceTextureListener(this);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mSurfacopengl版别过低怎样办eTexture = surface;
mRect = new Rect(0, 0, width, height);
mSurface = new Surface(mSurfaceTexture);
new Thread(this).staCanvasrt();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int heightwindows体系) {
mSuropengl和directxfaceTexture = surface;
mRect = new Rect(0, 0, width, height);
mSurface = new Surface(mSurfaceTexture);
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mRunning = false;
return false;
}嵌套查询
@Override
public void o接口卡nSurfaceTextureUpdated(SurfaceTcanvas标签exture surface) {
}
@Override
public void ru嵌套分类汇总n() {
mRunning = true;
while (mRunning) {
SystemClock.sleep(333);
Canvas canvas = mSurface.lockCanvas(mRect);
if (canvascanvas翻译 != null) {
try {
synchronized (mSurface) {
onRender(canvawindows7旗舰版s);嵌套查询
}
} finally {
mSurface.unlockCanvasAndPost(canvas);
}
}
}
}
private void onRender(Canvas canvas) {
canvas.drawColor(Color.RED);
// dr接口测验aw wha接口测验tever.
}
}

以上都是2D图形烘托常见的办法,假定想要进行3D图嵌套分类汇总的操作过程形烘托或者是高级图画处理(比如滤镜、AR等作用),就有必要得引入OpenGL ES来完毕了。OpenGL ES (OpenGL for Embedded Systems) 是 OpenGL 三嵌套分类汇总的操作过程维图形 API 的子集,针对手机opengl怎样升级、PDA和游戏主机等嵌入式设备而规划,是一种图形烘托API的规划标准,不同的软硬opengles3.1扩展包件开发商在OpenGL API内部或许会有不同的嵌套分类汇总的操作过程完毕办法。下面介绍一下在Androiwindows10激活密钥d途径opengles3.1扩展包上,怎样进行OpenGL ECanvasS烘托制作,一般有以下三种办法:

Suopengl下载rfaceView + OpenGL ES

EGL是OpenGL API和原opengl和directx生窗口体系之间的接口,OpenGL ES 的途径无关性正是仰仗 EGL 完毕的,EGL 屏蔽了不同途径的差异。假定运用OpenGOpenGLL API来制作图形就有必要先构canvas下载建EGL环境。

一般运用 EGL 烘托的一般进windows未能发动程:

– 获取 EGLDisplay政策,树立与本地窗口体系的联接调用canvas翻译eglGetDisplay办法得到EGLDisplay。

– 初始化EGL办法,翻开联接之后,调用eglInitialize办法初始化。

– 获取EGLConfig嵌套查询sql句子政策,确定烘托外表的装备信息调用eglChoosopengl-legacy什么意思eConfig方windows7法得到 EGLConfig。

– 创立烘托外表EGLSurface经过EGLDisplay嵌套和EGLConfig,调用eglCreateWindowSurface或eglCreatePbufferSurfaccanvas交大e办法创立烘托外表得到EGLSurface。

– 创立烘托上下文EGLContext经过EGLDisplay和EGLConfig,调用eglCreateContecanvas研讨xt办法创立烘托上下文,得到EGLContext。

– 绑定上下文经过eglMakeCurrent 办法将 EGLSurface、EGLContext、EGLDisplay 三者绑定,绑定成功之后OpenGLES环境就创立好了,接下来便能够进行烘托。

– 交流缓冲OpenGLES 制作完毕后,运用ewindows怎样激活glSwapBuffers办法交流前后缓冲,将制作内容闪现到接口是什么屏幕上,而屏幕外的烘托不需求调用此办法。

– 开释EGL环境制作完毕后,不再需求运用EGL时,需求吊销eglMakeCurrent的绑定,毁掉 EGLDwindows10isplay、EGLSur接口自动化face、EGLContexopengl版别过低怎样办t三个政策。

以上EGL环境构建比较凌乱,这儿先不做过多解windows10说,下面能够经过代码参考其具体完毕:

public class OpenGLSurfaceView extends SurfaceView imp嵌套查询lements SurfaceHolder.Callback, Runnable {
private boolean mRunning = f嵌套结构alse;
private SurfaceHolder mSurfaceHolder;
public OpenGLS嵌套查询sql句子urfacanvas交大ce嵌套分类汇总View(Contex嵌套分类汇总t context) {
supewindows怎样激活r(contextopengl下载);
iniwindows许可证行将过期怎样办tView();
}接口和抽象类的差异
public OpenGLSurfaceView(Context context, Attributecanvas动画Set attrs) {
super(context, attrs);
inicanvas翻译tView();
}
public OpenGLSurfaceView(Context context, AttributeSet attrs, int defStyleAttwindows怎样激活r) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
getHolder().addCallback(this);
}
@Override
public void surfaceCreated(Su接口文档rfaceHolder holder) {
mwindows许可证行将过期怎样办SurfaceHolder = holder;windows未能发动
ne接口英文w Thread(this).start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, in接口的作用t width, int height) {
mSurfaceHolder = holder;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mRunning =canvas研讨 false;
}
@Overridewindows许可证行将过期怎样办
public void run() {
//创立一个EGL实例
EGL10 egl = (EGL10) EGLContext.getEGL();
//
EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DIS嵌套PLAY);
//初始化EGLDisplay
int[] version = new int[2];
egl.eglInitialize(dpy, version);
int[] configSpec = {
EGL10.EGL_RED_SIZE,      5,
EGL10.EGL_GREEN_SIZE,    6,
EGL1windows体系0.EGL_BLUE_SIZE,     5,
EGL10.EGL_DEPTHcanvas_SIZE,   16,
EGL10.EGL_NONEcanvas动画
};
EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[1];
//挑选config创立opengl工作环境
egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
EGLConfig config = configs[0];
EGLContext contopengl怎样升级ext = egl.eglCreateContext(dpy, config,
EGL10.EGL_NO_CONTEXT, null);
//创立新的surface
EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, mSurfaceHolder, null);
//将opengles环境设置为当时
egl.egl嵌套查询sql句子MakeCurrent(dpy, surface, sur接口测验facanvas标签ce嵌套, context);
//获取当时opengles画布
GL10 gl = (GL10)context.gwindows7etGL(接口crc过错计数);
mRunning = true;
while (mRunning) {
SystemClock.sleep(333);
synchronized (mSurfaceHolder) {
onRender(gl);
//闪现制作作用到屏幕上
egl.eglSwapBuffers(dpy, surface);
}
}
egl.eglMakeCurrent(dpycanvassing, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,canvas标签 EGL10.EGL_NO_CONTEXT);
egl.eglDestroySurface(dpy, su接口rface);
egl.eglDestroyContext(dpy, context);
egl.eglTerminCanvasate(dpy);
}
private vowindows7旗舰版id onRewindows是什么意思nder(GL10 gOpenGLl) {
gl.gl接口英文ClearColor(1.0F, 0.0F, 0.0F, 1.0F);
// Clears the screen and depth buffer.
gl.glClear(GL10.GL_COLOR_BUFFER_BIT
| GL10.GL_DEPTHopengl版别过低怎样办_BUFFER_BIT);
}
}

从上面的代码能够看到,相对于SurfaceView + Canvas的制作办法,主要有以下两点改动:

  • 在while(true)循opengl三重缓冲开不开环前后增加了EGL环境结构的代码
  • onRender()办法内参数用的是GL10而不接口是什么是Canvas

GLSurfaceView + OpenGL ES

因为构建EGL环境比较繁琐,以及还需求健壮地保护一个线程,直接运用SurfaceView进行OpenGL制作并不方便。幸亏Android途径供给GLSurfaceView类,很好地封装了这些逻辑,使开发者能够快速地进行OpenGL的烘托开发。要运用GLSurfaceView类进行图形烘托,需求完接口类型结GLSurfaceView.Renderer接口,该接口供给一个owindows7nDrawFrame(GL10 gl)办法,在opengl-legacy什么意思该办法内完毕具体的烘托逻辑。

public classopengl和directx OpenGLGLSurfaceView extends GLSurfaceView implements GLSurfaceView.Renderer {
public OpenGLGwindows7LSurfaceVi接口卡ew(Context context) {
super(conopengl版别过低怎样办text);
s嵌套分类汇总etRenderer(this);
}
public Op嵌套if函数enGLGLSurwindows怎样激活f接口文档aceView(Context context, AttributeSet attrs) {
super(context, attrs);
setRenderer(this);
}
@Override
public嵌套查询 void onSurfaceCreated(GL10 gl, EGLConfig config) {
// pass through
}
@Override
public void onSurfaceChopengl渲染gpu怎样设置anged(GL10 gl, int width, int height) {
gopengl版别过低怎样办l.glViewport(0, 0,windows未能发动 width, height);
}
@Override
public void onDraopengl版别过低怎样办wFrame(GLwindows7旗舰版10 gl) {
gl.glClearColor(1.0F, 0.0F, 0.0F, 1.0F);
// Clears the s嵌套creen and depth buffecanvas下载r.
gl.glClear(GL10.GL_COLOR_BUFFER_BIT
| GL10.GL_opengl-legacy什么意思DEPTH_BUFFER_BIT);
}
}

TextureView + OpenGL ES

该办法跟SurfaceView + OpenGL ES运用办法比较相似,运用该办法有个优点是它是经过TextureView来嵌套是什么意思完毕的,windows许可证行将过期怎样办所以能够摒弃Surcanvas下载face不在View hierachy中缺点,Texturewindows7旗舰版View不会在WMS中独自创立窗口,而是作为View hierachy中的一个一般View接口crc过错计数,因此能够和其它嵌套是什么意思一般View相同进行移动,旋转,缩放,动画等改动。这儿运用TextureView类在构建EG接口的作用L环境时需求留意,传入eglCreateWicanvas标签ndowSurface嵌套是什么意思()的参数是SurfaceTexture实例。

public class OpenGLTextureView extends TextureView implements TextureView.SurfaceTextureList接口自动化ener, Runnable {
private boolean mRunning = false;
private Surfac嵌套if函数eTexture mSurfaceTexture;
public OpenGLTextureView(canvasContext context) {
super(context);
initView();
}
public OpenGLTcanvasextureView(Context context,嵌套函数 Attributcanvas标签eSet attrs) {
super(context, attrs);
initView();
}
public OpenGLTextureView(Contextopengl渲染 context, AttributeSet attrs, int defStyleAttr) {
super(context, acanvas教育渠道ttrs, defStyleAttr);
initView();
}
private void initView() {
setSurfaceTextureLisopengl渲染tener(this);
}
@Override
public void onSurfaceTextureAvailabl嵌套查询e(windows10SurfaceTexturopengl渲染gpu怎样设置e surface, int width, int height) {
mSurfaceTexture = surface;
new Thread(topengles3.1扩展包his).start();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTextuWindowsre surface, int width, int height) {
mopengles3.1扩展包Surfacopengl三重缓冲开不开eTexture = surface;
}
@Override
p接口测验ublic boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mRunning接口英文 = false;
return false;
}
@Override
public void on嵌套查询sql句子SurfaceTextureUpdated(SurfaceTexture surface) {
}
@Overriwindows是什么意思de
public void run() {
//创立一个EGL实例
EGL10 egl = (EGL10) EGLContext.getEGL();
//
EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
//初始化EGLDisplay
int[] verWindowssion = new int[2];
ewindows许可证行将过期怎样办gl.eglInitialize(dpy, version);
in嵌套查询sql句子t[] configSpec = {
EGL10.EGL_RE嵌套分类汇总的操作过程D_SIZE,      5,
EGL10.EGL_GREEN_SIZE,    6,
EGL10.EGL_BL接口类型UE_SIZE,     5,
EGL10.EGL_DEPTH_SIZE,   16,
EGL10.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[1];
//挑选config创立opengl工作环境
egl.eglChooseConfiwindows是什么意思g(dpy, configSpec, config接口卡s, 1, num_config);
EGLConfig config = configs[0];
EGLContext context = egl.eglCreateContext(dpy, config,
EGL10.EGL_NO_CONTEXT接口的作用, null);
//创立新的surface
EGLSuopengl下载rface surface = egl.eglCreateW嵌套分类汇总的操作过程indowSurfa嵌套函数ce(dpy, config, mSurfaceTexture, null);
//将opengles环境设置为当时
egl.eglMa嵌套是什么意思keCurrent(dpy, surface,opengl下载 surface, context);
//获取当时opengles画布
GL10 gl = (GL10)coOpenGLntext.getGL();
mRunning = true;
while (mRunnin接口的作用g) {
S嵌套是什么意思ystemClock.sleep(333);
synchro嵌套分类汇总nized (mSurfaceTexture)windows未能发动 {
onRender(gl);
//闪现制作作用到屏幕上
egl.eglSwapBuffer接口和抽象类的差异s(dpy, surface);
}
}
egl嵌套调用.eglMakeCurrent(dpy, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEwindows未能发动XT);
egl.eglDestroySurface(dpy, surface);
egl.eglDestroyContext(dpy, context);
egl.eglTe嵌套函数rminate(dpy);
}
private void onRendopengl版别过低怎样办er(GL10 gl) {
gl.glClearColor(1.0F, 0.0F, 1.0F, 1.0F);
// Clears the screen and depth buffer.
gl.glClear(GL10.GL_COcanvasL接口是什么OR_BUFFER_BIT
| GL10.GL_DEPwindows许可证行将过期怎样办THopengl下载_BUFFER_BIT);
}
}

代码示例参考

githucanvas教育渠道b.com/sunjinbo/hi…