android-openGL-canvas

Introduction: An Android library that provides views using openGL canvas to draw things on SurfaceView or TextureView.
More: Author   ReportBugs   
Tags:

English README

此项目灵感来源:

  • Android package com.android.gallery3d.glrenderer 下的源代码
  • GPUImage
  • grafika

感谢这些源代码提供者!

项目应用

功能

  • 提供一个类似 Android Canvas 类的使用 OpenGL 来实现绘制的 canvasGL。可以像传统自定义 View 那样直接继承 GLViews, 再使用这个 canvas 绘制需要的东西。
  • 提供类似 GPUImage 里的 Filter 的 API,可以在使用 canvasGL 画东西时实现图像处理。
  • 提供的 View 是继承 GLSurfaceView 或 TextureView 的,可以使用这两种 View 的特性,特别是 TextureView 的特性。

  • 另外,因为使用 OpenGL 在另一线程渲染,所以里面的 GLContinuousView 还提供能够实现高性能的动画的方法。(如果只要这个功能不要其它得话,那么我建议你直接继承 View,见浅析 Android 的 canvas 性能anim

与 GPUImage 对比:

  • 提供无限循环渲染线程的 GLContinuousView 和 GLContinuousTextureView。
  • 使用 TextureView 来实现 OpenGL 的绘制,可以利用 TextureView 的优点--TextureView 不会创建一个分离的 window,而是像一个普通的 view 那样显示, 这样就不会像 GLSurfaceView 那样,要么在所有 View 上方,要么被其它 View 遮住(看 setZOrderOnTop(boolean) 的说明)。而且像 myView.setAlpha(0.5f)这种方法调用后也会有效果了。
  • 提供一个 canvas,不只是图片处理,可以画上想画的其它东西。

使用要求

  • Android API 14 以上(OpenGL ES 2.0 以上)。 建议 21 以上

用法

Gradle dependency

sample:

// in root build.gradle
allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

// module build.gradle
dependencies {
    implementation 'com.github.ChillingVan:android-openGL-canvas:v1.5.4.0'
}

样例代码

  • 自定义一个 View. ```java public class MyGLView extends GLView {

    public MyGLView(Context context) {

      super(context);
    

    }

    public MyGLView(Context context, AttributeSet attrs) {

      super(context, attrs);
    

    }

@Override
protected void onGLDraw(ICanvasGL canvas) {
    // draw things with canvas here
}

}



![canvas](https://github.com/ChillingVan/android-openGL-canvas/raw/master/screenshots/canvas-example-v1.png)

* 其中, GLContinuouslyView, GLTextureView, GLContinuousTextureView, GLMultiTexProducerView and GLMultiTexConsumerView 用法相似.


* 使用 CanvasGL 实现绘制
```java
        canvas.drawBitmap(textBitmap, left, top);

        // transform
        canvas.save();
        canvas.rotate(rotateDegree, x, y);
        canvas.drawBitmap(bitmap, left, top);
        canvas.restore();
        // or
        CanvasGL.BitmapMatrix matrix = new CanvasGL.BitmapMatrix();
        matrix.postScale(2.1f, 2.1f);
        matrix.postRotate(90);
        canvas.drawBitmap(bitmap, matrix);

        // apply filter to the bitmap
        textureFilter = new ContrastFilter(2.8f);
        canvas.drawBitmap(bitmap, left, top, textureFilter);

filters

  • 可以与 Camera 结合,注意运行样例代码的时候尽量使用真机而不是模拟器。 camera

  • 如果不想使用 View,可以使用 MultiTexOffScreenCanvas 实现脱离屏幕的绘制,然后使用 getDrawingBitmap 方法获取绘制的内容。

  • MediaPlayer

可以用 MediaPlayer 去解码视频,并绘制到 TextureView 上。 如果用本项目里的 GLSurfaceTextureProducerView ,那么还可以做视频处理。 结合AndroidInstantVideo的 stream publisher,就能生成新视频。

  • AndroidCanvasHelper

这个 GLCanvas 不能绘制文本。只能先把文本转成 Bitmap 来绘制 可以使用 AndroidCanvasHelper 来画任意东西再转化为 Bitmap 给 GLCanvas 这有同步和异步模式,视情况用对应的模式,详细请看例子。

注意事项和常见问题

  • 每一个 View 的 onGLDraw 都运行在自己的线程而非主线程。
  • 目前的 Filter 没有 GPUImage 里那么多,如果有需要,参照我的代码自己实现即可,难度不高。
  • 为什么 Bitmap 修改后,再次绘制时并没更新?

    因为没有调用 canvasGL 的 invalidateTextureContent(bitmap)。改变了的 Bitmap 需要重新绑定 texture。因为绑定需要耗时,所以库里面才不做成每次都重新绑定。

  • CanvasGL 里面没有 drawPath 或者 drawText,要实现的话本库提供了 IAndroidCanvasHelper,但这个只是用安卓自己的 canvas 生成一个 Bitmap,所以要注意性能

相关博客文章

最近更新

  • 添加高斯模糊 & 修复 FilterGroup ViewPort 宽高问题(1.5.4 感谢@iffly))
  • 添加录屏 demo
  • 添加 clearTextureCache,比弱引用更快释放内存 (1.5.2)
  • AndroidCanvasHelper 能直接操作 Canvas 里的 bitmap 了 (1.5.2)
  • TwoTextureFilter 支持 RawTexture 了
  • drawSurfaceTexture 也支持 BitmapMatrix 了
  • 添加 OrthoBitmapMatrix 以支持正交投影。 默认 BitmapMatrix 用的是透视投影。
  • 支持裁切图片的 CropFilter
  • 增加 MultiTexOffScreenCanvas, GLMultiTexProducerView, GLMultiTexConsumerView,支持提供多张纹理的和消化多张纹理

License

Copyright 2016 ChillingVan.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on
 an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools