Converting Android Bitmap to LibGdx's Texture

前端 未结 4 1254
失恋的感觉
失恋的感觉 2020-12-03 16:31

i am trying to convert bitmap into a libGDX Texture by converting:

  1. Android Bitmap to byte[]
  2. byte[] to libGDX Pi
4条回答
  •  甜味超标
    2020-12-03 17:16

    If the goal is to convert an Android Bitmap to a libgdx Texture, you don't need to use Pixmap at all. You can do it directly with the help of simple OpenGL and Android GLUtils. Try the followings; it is 100x faster than your solution. I assume that you are not in the rendering thread (you should not most likely). If you are, you don't need to call postRunnable().

           Gdx.app.postRunnable(new Runnable() {
            @Override
            public void run() {
              Texture tex = new Texture(bitmap.getWidth(), bitmap.getHeight(), Format.RGBA8888);
              GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex.getTextureObjectHandle());
              GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
              GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
              bitmap.recycle();
              // now you have the texture to do whatever you want
            }
          });
    

提交回复
热议问题