Android examples for android.graphics:Canvas
make Texture
import static android.opengl.GLES20.GL_NEAREST; import static android.opengl.GLES20.GL_RGBA; import static android.opengl.GLES20.GL_TEXTURE_2D; import static android.opengl.GLES20.GL_TEXTURE_MAG_FILTER; import static android.opengl.GLES20.GL_TEXTURE_MIN_FILTER; import static android.opengl.GLES20.GL_UNSIGNED_BYTE; import static android.opengl.GLES20.glBindTexture; import static android.opengl.GLES20.glGenTextures; import static android.opengl.GLES20.glTexImage2D; import static android.opengl.GLES20.glTexParameteri; import java.nio.ByteBuffer; import java.nio.IntBuffer; import android.graphics.Bitmap; import android.opengl.GLUtils; public class Main { public static int makeTexture(int x, int y, int color) { int[] tid = new int[1]; ByteBuffer bb = ByteBuffer.allocateDirect(x * y * 4); IntBuffer ib = bb.asIntBuffer(); // int color = 0x0000ffff; for (int i = 0; i < x * y; i++) { ib.put(color);/* w w w. j ava 2 s .c o m*/ } // Create and bind a single texture object. glGenTextures(1, tid, 0); glBindTexture(GL_TEXTURE_2D, tid[0]); // Copy the texture to the GPU glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, bb); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // Unbind texture glBindTexture(GL_TEXTURE_2D, 0); return tid[0]; } public static int makeTexture(Bitmap bitmap, int w, int h) { int textureId = makeTexture(w, h, 0xffffffff); if (bitmap != null) { Bitmap flippedbmp = flipBitmap(bitmap); glBindTexture(GL_TEXTURE_2D, textureId); GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, flippedbmp); glBindTexture(GL_TEXTURE_2D, 0); } return textureId; } /** * Flips the pixels in a Bitmap to be mirrored vertically. This is needed for * OpenGL to draw the texture properly as loads texture bottom up. * * @return a new Bitmap that is identical to the argument but flipped. * @param bitmap * The Bitmap to flip the pixels of */ static public Bitmap flipBitmap(Bitmap bitmap) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); int[] pixels = new int[w * h]; bitmap.getPixels(pixels, 0, w, 0, 0, w, h); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { pixels[y * w + x] = bitmap.getPixel(x, h - 1 - y); } } bitmap = Bitmap.createBitmap(pixels, w, h, Bitmap.Config.ARGB_8888); return bitmap; } }