【问题标题】:Fast way to load bitmap and make a texture in Android在 Android 中加载位图和制作纹理的快速方法
【发布时间】:2016-04-05 06:53:12
【问题描述】:

我正在尝试在 Android 中使用 OpenGL ES 制作一种照片查看器。

在我的应用程序中,基本上当我单击一个按钮然后在 sdcard 中获取整个照片路径列表时,从照片制作位图并将纹理设置为该位图,然后将其绘制为简单的四边形。但这很慢,甚至 GL 视图在工作时也会冻结

制作纹理应该在 GLthread 中工作,所以我设置了 Listener 并使用 queueEvent()

sn-ps 代码如下:

public void setTexture(Context context, final String string) {
Bitmap bitmap = null; 
try {
    Uri uri = Uri.fromFile(new File(string));
    bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
// upload texture by bitmap
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}


queueEvent(new Runnable() {
               @Override
               public void run() {
                   for (int i = 1; i < photos.size() - 1; i++) {
                       try {
                           exifInterface = new ExifInterface(photoList.get(i));
                               } catch (IOException e) {
                                   e.printStackTrace();
                               }
                               String orientation = exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);
                               Log.d(TAG, "photoOrientation: " + orientation);
                               photos.get(i).setOrientation(Integer.parseInt(orientation));
                               photos.get(i).setTexture(mContext, photoList.get(i));
                           }
                       }
                   });

查看时间的时候,最耗时的工作是decodeStream()那么我该如何改进呢?有没有更有效的方法将位图上传到纹理?或至少我怎样才能避免冻结 GLView?

【问题讨论】:

    标签: android opengl-es bitmap


    【解决方案1】:

    尝试使用BitmapFactory.decodeFile() 而不是BitmapFactory.decodeStream()

    【讨论】:

      【解决方案2】:

      好吧,我刚刚从 GL 线程中更改了加载部分。然后它停止冻结。

      我还提到了a good training example 以实现高效的位图加载

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-06
        • 1970-01-01
        • 2021-05-31
        • 1970-01-01
        • 2016-12-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多