【问题标题】:GLSurfaceView sizing and black barsGLSurfaceView 大小调整和黑条
【发布时间】:2016-03-11 11:18:58
【问题描述】:

我正在做应用程序来将过滤器应用于图像。我从 android 示例中获得了渲染代码,但是 GLSurfaceView 保存或转换为位图时遇到了一些问题。

这是保存到位图的代码

private Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl)
            throws OutOfMemoryError {

        int bitmapBuffer[] = new int[w * h];
        int bitmapSource[] = new int[w * h];
        IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
        intBuffer.position(0);
        try {
            gl.glReadPixels(0, 0, imageView.getWidth(), imageView.getHeight(), GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
            int offset1, offset2;
            for (int i = 0; i < h; i++) {
                offset1 = i * w;
                offset2 = (h - i - 1) * w;
                for (int j = 0; j < w; j++) {
                    int texturePixel = bitmapBuffer[offset1 + j];
                    int blue = (texturePixel >> 16) & 0xff;
                    int red = (texturePixel << 16) & 0x00ff0000;
                    int pixel = (texturePixel & 0xff00ff00) | red | blue;
                    bitmapSource[offset2 + j] = pixel;
                }
            }
        } catch (GLException e) {
            return null;
        }

        return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
    }

这里你可以看到一个例子(上面和下面的黑条是多余的

我尝试更改 GlSurfaceView 的大小并以此消除黑条,但它一直都是“match_parent”。 (我正在准备 ImageView 的宽度和高度参数)

 ViewGroup.LayoutParams layoutParams=mEffectView.getLayoutParams();
 layoutParams.width=mImageViewWidth;
 layoutParams.height=mImageViewHeight;
 mEffectView.setLayoutParams(layoutParams);

我还尝试在创建位图时添加一些偏移量,但它什么也没做,或者破坏了图像。 我该怎么做才能摆脱黑条? (他们也可以从侧面) 谢谢。

【问题讨论】:

    标签: android android-layout android-bitmap glsurfaceview


    【解决方案1】:

    就在调用这个方法之前:

    createBitmapFromGLSurface(0, 0, mEffectView.getWidth(), mEffectView.getHeight(), gl);
    

    您可以做的就是将GLSurfaceViewHeightWidth 设置为HeightHeightWidth 就像这样。

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mEffectView.getLayoutParams().height = bitmapOriginal.getHeight();
            mEffectView.getLayoutParams().width = bitmapOriginal.getWidth();
            mEffectView.requestLayout();
        }
    });
    

    所以代码会是这样的:

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mEffectView.getLayoutParams().height = bitmapOriginal.getHeight();
            mEffectView.getLayoutParams().width = bitmapOriginal.getWidth();
            mEffectView.requestLayout();
        }
    });
    
    createBitmapFromGLSurface(0, 0, mEffectView.getWidth(), mEffectView.getHeight(), gl);
    

    更新:

    确保在您的 XML 中将 GLSurfaceView 设置为 wrap_content heightwidth

    希望这能解决问题?

    【讨论】:

      猜你喜欢
      • 2014-10-01
      • 1970-01-01
      • 2016-11-14
      • 2010-12-18
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多