我有一个Android应用程序,我加载资源图像文件并为它们创建纹理,以便我可以使用OpenGL渲染它们 . 但是我使用的是mipmap,图像尺寸必须是2的幂 . 这就是为什么我需要在制作纹理之前增加给定位图的宽度或高度 .
这是我目前完成工作的代码:
Bitmap bitmap = BitmapFactory.decodeResource(this.context.getResources(), resourceId);
Bitmap newBitmap = Bitmap.createBitmap(nextPowerOfTwo(bitmap.getWidth()),
nextPowerOfTwo(bitmap.getHeight()), bitmap.getConfig());
int x=0,y=0,width=bitmap.getWidth(),height=bitmap.getHeight();
int [] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, x, y, width, height);
newBitmap.setPixels(pixels, 0, width, x, y, width, height);
bitmap.recycle();
gl.glGenTextures(1, textures, textureCount);
...
这完全适用于我的华硕TF101和Nexus 4,但我得到了Galaxy S3(可能还有更多设备)的OutOfMemory异常,如下所示:
int [] pixels = new int[width * height];
从做一些阅读我意识到对Bitmap.createBitmap的调用也是内存昂贵的,我试图想办法减少内存浪费 . 想到的第一个想法是为'int []像素使用较小的2D数组,并一次复制较少的像素 . 但我想知道是否还有其他更有效的方法来做到这一点 .
1 回答
因为你只是创建一个缩放的位图,这个方法应该是你的问题的解决方案:[http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap(android.graphics.Bitmap](http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap(android.graphics.Bitmap),int,int,boolean)