Android ImageView未使用位图的alphachannel

Android ImageView未使用位图的alphachannel,android,opengl-es,imageview,alpha,Android,Opengl Es,Imageview,Alpha,我在渲染屏幕外的对象以创建其位图并在imageview中显示时遇到了一个小问题。它无法正确拍摄alpha通道 当我将位图保存为png并加载它时,它工作得很好。但是当我直接将它加载到imageview中时,我会看到一个白色背景,这是没有alpha通道的实际背景色 下面是从我的EGL曲面导出位图的代码: public Bitmap exportBitmap() { ByteBuffer buffer = ByteBuffer.allocateDirect(w*h*4); GLES20

我在渲染屏幕外的对象以创建其位图并在imageview中显示时遇到了一个小问题。它无法正确拍摄alpha通道

当我将位图保存为png并加载它时,它工作得很好。但是当我直接将它加载到imageview中时,我会看到一个白色背景,这是没有alpha通道的实际背景色

下面是从我的EGL曲面导出位图的代码:

public Bitmap exportBitmap() {
    ByteBuffer buffer = ByteBuffer.allocateDirect(w*h*4);
    GLES20.glReadPixels(0, 0, w, h, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    bitmap.copyPixelsFromBuffer(buffer);
    Log.i("Render","Terminating");
    EGL14.eglMakeCurrent(oldDisplay, oldDrawSurface, oldReadSurface, oldCtx);
    EGL14.eglDestroySurface(eglDisplay, eglSurface);
    EGL14.eglDestroyContext(eglDisplay, eglCtx);
    EGL14.eglTerminate(eglDisplay);
    return bitmap;
}
这里设置imageview r的代码是包含前面函数的类:

Bitmap bm;
bm = r.exportBitmap();
ImageView infoView = (ImageView)findViewById(R.id.part_icon);
infoView.setImageBitmap(bm);
我必须在ImageView上设置一些标志还是在位图的配置中设置一些

我将添加一些代码示例和图片来澄清问题: 首先,我希望它的工作方式:

bm = renderer.exportBitmap();
其次是它的工作方式,使用“保存到png”解决方案:

bm = renderer.exportBitmap();

//PNG To Bitmap
String path = Environment.getExternalStorageDirectory()+"/"+getName()+".png";
bm.compress(CompressFormat.PNG, 100, new FileOutputStream(new File(path)));
bm = BitmapFactory.decodeFile(path);
第三,澄清我的前倍数。阿尔法的考虑方式是错误的:

        bm = renderer.exportBitmap();
        for(int x=bm.getWidth()-50; x<bm.getWidth(); x++) {
            for(int y=bm.getHeight()-50; y<bm.getHeight(); y++) {
                int px = bm.getPixel(x,y);
                bm.setPixel(x, y, 
                        Color.argb(255,
                        Color.red(px),
                        Color.green(px),
                        Color.blue(px)));
            }
        }  

抱歉发了这么长的帖子。

我已经更改了exportBitmap功能。基本上我读像素,然后再写。这就解决了问题。我真的不知道为什么。如果有人能给我解释一下,那就太好了。为这些bitmapdata创建一个新的整数数组并不是那么简单

public Bitmap exportBitmap() {
    ByteBuffer buffer = ByteBuffer.allocateDirect(w*h*4);
    GLES20.glReadPixels(0, 0, w, h, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    bitmap.copyPixelsFromBuffer(buffer);

    //This is the modification
    int[] pixels = new int[w*h];
    bitmap.getPixels(pixels, 0, w,0, 0,w, h);
    bitmap.setPixels(pixels, 0, w,0, 0,w, h);

    EGL14.eglMakeCurrent(oldDisplay, oldDrawSurface, oldReadSurface, oldCtx);
    EGL14.eglDestroySurface(eglDisplay, eglSurface);
    EGL14.eglDestroyContext(eglDisplay, eglCtx);
    EGL14.eglTerminate(eglDisplay);
    return bitmap;
}

你看过glReadPixels之后得到的原始字节了吗?你能看到正确的alpha值吗?此外,您是否为EGL上下文启用了alpha通道?当您从glReadPixels接收RGBA数据但创建具有ARGB字节顺序的位图时,您确定字节顺序正确吗?我确实在egl配置中启用了alpha通道。实际测试的对象颜色为正确的蓝色、红色、绿色。另外,如果我通过bm.compressBitmap.CompressFormat.png将位图另存为png,100,new FileOutputStreamnew Filepath;它有正确的颜色和阿尔法通道。我刚检查过。应为半透明的白色值为0。所以0,0,0,0。这意味着这些值是预乘的。当我将alphachannel设置为255时,图像为黑色。所以这似乎是安卓显示器的问题,因为它不会呈现实际的黑色值,而是显示白色值。我想我仍然没有完全理解这个问题。当值为0,0,0,0时,它们是完全半透明的,这意味着应该绘制图像下方的背景。如果这个背景默认为白色,我不知道,白色将被渲染。当你将alpha通道设置为255,这意味着你得到了0,0,0255个RGBA值,那么它应该是黑色的。这正是问题所在。该值为0,0,0,0。下面的颜色是灰色,白色由图像绘制。因此android可以识别alpha通道,但不显示imageview下面的背景。再一次如果我保存并加载为png,图像是半透明的。