Android中利用MediaPlay+SurfaceView播放视频的时候,我们一般都会在视频开始播放之前先展示的是视频的首帧预览图,这时候我们一般都是通过在surfaceview上面覆盖一个ImageVIew来显示预览图(imageView的显示与隐藏),而下面我们要说的是可以通过OpenGL来绘制预览图。
首先第一步我们要创建一个sufaceview
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated( SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
drawBitmapTOSurface(holder.getSurface(), vedioBitmap);//这个就是我们将视频的首帧bitmap绘制到surfaceview上的方法
startPlay();//表示的是播放视频
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
第二部,创建EGL环境
/**
* 向surfaceview中绘制bitmap
*
* EGL是介于诸如OpenGL 或OpenVG的Khronos渲染API与底层本地平台窗口系统的接口。
* 它被用于处理图形管理、表面/缓冲捆绑、渲染同步及支援使用其他Khronos API进行的高效、加速、混合模式2D和3D渲染。
*/
private void drawBitmapTOSurface(Surface surface, Bitmap bitmap) {
//1. 取得EGL实例
EGL10 egl = (EGL10) EGLContext.getEGL();
//2. 选择Display
EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
egl.eglInitialize(display, null);
int[] attribList = {
EGL10.EGL_RED_SIZE, 8,
EGL10.EGL_GREEN_SIZE, 8,
EGL10.EGL_BLUE_SIZE, 8,
EGL10.EGL_ALPHA_SIZE, 8,
EGL10.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
EGL10.EGL_NONE, 0, // placeholder for recordable [@-3]
EGL10.EGL_NONE
};
//3. 选择Config
EGLConfig[] configs = new EGLConfig[1];
int[] numConfigs = new int[1];
egl.eglChooseConfig(display, attribList, configs, configs.length, numConfigs);
EGLConfig config = configs[0];
//4. 创建Surface
EGLSurface eglSurface = egl.eglCreateWindowSurface(display, config, surface,
new int[]{
EGL14.EGL_NONE
});
//5. 创建Context
EGLContext context = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, new int[]{
EGL14.EGL_CONTEXT_CLIENT_VERSION, 2,
EGL10.EGL_NONE
});
egl.eglMakeCurrent(display, eglSurface, eglSurface, context);
//创建OpenGL环境并绘制bitmap纹理
new GLHelper().drawToBitmap(bitmap);
//6. 指定当前的环境为绘制环境
egl.eglSwapBuffers(display, eglSurface);
egl.eglDestroySurface(display, eglSurface);
egl.eglMakeCurrent(display, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_CONTEXT);
egl.eglDestroyContext(display, context);
egl.eglTerminate(display);
}
第三部,创建并渲染bitmap纹理。
/**
* 创建opengl环境并渲染bitmap纹理
*/
public class GLHelper {
private String mVertex = "precision highp float;\n" +
"precision highp int;\n" +
"attribute vec4 aVertexCo;\n" +
"attribute vec2 aTextureCo;\n" +
"\n" +
"uniform mat4 uVertexMatrix;\n" +
"uniform mat4 uTextureMatrix;\n" +
"\n" +
"varying vec2 vTextureCo;\n" +
"\n" +
"void main(){\n" +
" gl_Position = uVertexMatrix*aVertexCo;\n" +
" vTextureCo = (uTextureMatrix*vec4(aTextureCo,0,1)).xy;\n" +
"}";
private String mFragment = "precision mediump float;\n" +
"varying vec2 vTextureCo;\n" +
"uniform sampler2D uTexture;\n" +
"void main() {\n" +
" gl_FragColor = texture2D( uTexture, vTextureCo);\n" +
"}";
protected int mGLVertexCo;
protected int mGLTextureCo;
protected int mGLVertexMatrix;
protected int mGLTextureMatrix;
protected int mGLTexture;
private float[] mVertexMatrix = MatrixUtils.getOriginalMatrix();
private float[] mTextureMatrix = MatrixUtils.getOriginalMatrix();
protected FloatBuffer mVertexBuffer;
protected FloatBuffer mTextureBuffer;
public static float[] getOriginalTextureCo() {
return new float[]{
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f
};
}
/**
* 初始化顶点数据
*/
protected void initBuffer() {
ByteBuffer vertex = ByteBuffer.allocateDirect(32);
vertex.order(ByteOrder.nativeOrder());
mVertexBuffer = vertex.asFloatBuffer();
mVertexBuffer.put(MatrixUtils.getOriginalVertexCo());
mVertexBuffer.position(0);
ByteBuffer texture = ByteBuffer.allocateDirect(32);
texture.order(ByteOrder.nativeOrder());
mTextureBuffer = texture.asFloatBuffer();
mTextureBuffer.put(getOriginalTextureCo());
mTextureBuffer.position(0);
}
/**
*创建OpenGL环境 绘制bitmap纹理
* @param bitmap
*/
public void drawToBitmap(Bitmap bitmap) {
//清空屏幕
GLES20.glClearColor(1, 1, 1, 1);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
initBuffer();
int mGLProgram = GpuUtils.createGLProgram(mVertex, mFragment);
mGLVertexCo = GLES20.glGetAttribLocation(mGLProgram, "aVertexCo");
mGLTextureCo = GLES20.glGetAttribLocation(mGLProgram, "aTextureCo");
mGLVertexMatrix = GLES20.glGetUniformLocation(mGLProgram, "uVertexMatrix");
mGLTextureMatrix = GLES20.glGetUniformLocation(mGLProgram, "uTextureMatrix");
mGLTexture = GLES20.glGetUniformLocation(mGLProgram, "uTexture");
//绘制bitmap纹理texture
int[] texture = new int[1];
GLES20.glGenTextures(1, texture, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[0]);
//设置缩小过滤为使用纹理中坐标最接近的一个像素的颜色作为需要绘制的像素颜色
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
//设置放大过滤为使用纹理中坐标最接近的若干个颜色,通过加权平均算法得到需要绘制的像素颜色
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
//设置环绕方向S,截取纹理坐标到[1/2n,1-1/2n]。将导致永远不会与border融合
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
//设置环绕方向T,截取纹理坐标到[1/2n,1-1/2n]。将导致永远不会与border融合
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
GLES20.glUseProgram(mGLProgram);
GLES20.glUniformMatrix4fv(mGLVertexMatrix, 1, false, mVertexMatrix, 0);
GLES20.glUniformMatrix4fv(mGLTextureMatrix, 1, false, mTextureMatrix, 0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[0]);
GLES20.glUniform1i(mGLTexture, 0);
GLES20.glEnableVertexAttribArray(mGLVertexCo);
GLES20.glVertexAttribPointer(mGLVertexCo, 2, GLES20.GL_FLOAT, false, 0, mVertexBuffer);
GLES20.glEnableVertexAttribArray(mGLTextureCo);
GLES20.glVertexAttribPointer(mGLTextureCo, 2, GLES20.GL_FLOAT, false, 0, mTextureBuffer);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
GLES20.glDisableVertexAttribArray(mGLVertexCo);
GLES20.glDisableVertexAttribArray(mGLTextureCo);
}
}
下载DEMO