我想“杀死”纹理的黑色。或者,我想显示png文件,没有背景(透明bg)
我怎么能这样做? 任何一个例子?
答案 0 :(得分:3)
public static Bitmap loadBitmapFromId(Context context, int bitmapId) {
InputStream is = context.getResources().openRawResource(bitmapId);
try {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
return BitmapFactory.decodeStream(is, null, bitmapOptions);
} catch (Exception ex) {
Log.e("bitmap loading exeption", ex.getLocalizedMessage());
return null;
}
}
Bitmap.Config.RGB_565在这里非常重要。然后添加位图并像往常一样获取纹理ID。
现在在你的渲染器的onSurfaceCreated(GL10 gl,EGLConfig配置)中添加:
// Transparancy
// important: transparent objects have to be drawn last!
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
绘图部分(你可能已经这样做了):
// first disable color_array for save:
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
// Enabled the vertices buffer for writing and to be used during
// rendering.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// Specifies the location and data format of an array of vertex
// coordinates to use when rendering.
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
gl.glBindTexture(GL10.GL_TEXTURE_2D, myTextureId);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glDrawArrays(drawMode, 0, verticesCount);
gl.glDisable(GL10.GL_TEXTURE_2D);
// Disable the vertices buffer.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);