简体   繁体   English

View.getDrawingCache() 总是返回 Bitmap 中的黑色区域用于透明 PNG。 我们可以将 Bitmap 中的透明黑色区域更改为白色吗?

[英]View.getDrawingCache() always return Black area in Bitmap for transparent PNG. Can we change Tranparent Black Area to White Color in Bitmap?

I have a method for screenshot which takes the View from XML and convert to Bitmap object, and I have multiple PNG on layout View.我有一种截屏方法,它从 XML 获取视图并转换为 Bitmap object,并且我在布局视图上有多个 PNG。 Some PNG have transparent Area which appears as Black color in Bitmap (which i want to change to White) or want to get rid of Black transparent area.一些PNG具有透明区域,在Bitmap(我想将其更改为白色)中显示为黑色或想要摆脱黑色透明区域。

private void takescreenshot(LinearLayout preview) throws IOException
{
    View z  = preview;   // get whole layout view
    z.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(z.getDrawingCache());
    z.destroyDrawingCache();
}

create a new blank bitmap创建一个新的空白 bitmap
create canvas object创建 canvas object
fill the canvas a background填写canvas一个背景
put old bitmap in your canvas.将旧的 bitmap 放入您的 canvas 中。

Bitmap newBitmap = Bitmap.create(bitmap.width,bitmap.height,ARGB_8888)
Canvas canvas = new Canvas(newBitmap)
canvas.drawColor(Color.white)
canvas.drawBitmap(bitmap,0F,0F,null)

finally i figure out the solution as well最后我也想出了解决方案

View z  = preview;   // get whole layout view
        z.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(z.getDrawingCache());
        z.destroyDrawingCache();

Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),ARGB_8888);
        newBitmap.eraseColor(Color.WHITE);
        Canvas canvas2 = new Canvas(newBitmap);
        canvas2.drawBitmap(bitmap,0F,0F,null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM