【问题标题】:getting inverted image while converting layout to bitmap in android在android中将布局转换为位图时获取倒置图像
【发布时间】:2013-04-11 05:19:35
【问题描述】:

我正在尝试使用下面的代码将我的布局转换为图像。

LinearLayout rlpage = (LinearLayout)findViewById(R.id.rlpage);
rlpage.setDrawingCacheEnabled(true);
Bitmap viewBitmap = rlpage.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
viewBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
 byte[] toSend = baos.toByteArray();  
        try {
            fileOutputStream.write(toSend);
            fileOutputStream.flush();
            fileOutputStream.close();
        }
        catch(Exception e)
        {

        }

这是我的布局

这是输出图像

这是什么原因以及如何克服?

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    希望对您有所帮助:

    image_view.setDrawingCacheEnabled(true);
                Bitmap bmp =Bitmap.createBitmap(image_view.getDrawingCache());
                image_view.setDrawingCacheEnabled(false);
    
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
                byte[] image = baos.toByteArray();
    

    您可以用您的特定布局替换您的 image_view。

    【讨论】:

    • 已经用过png,结果还是一样。用image_view.setDrawingCacheEnabled(false);有什么好处
    • 不,它只会关闭您的 DrawingCache 以进行布局。因为我们使用 setDrawingCacheEnabled(true);它允许获取图像。据我所知,与我们用来关闭绘图缓存的方法相同。并且在这一行中具体说明 Bitmap bmp=Bitmap.createBitmap(image_view.getDrawingCache());
    • 当我使用 png 格式时,它会给出一个透明区域的图像,这是黑色区域的替换。
    【解决方案2】:

    我认为原因是 JPEG 只支持完全不透明的像素。正如 Meghs 建议的那样,对 PNG 使用压缩会更好。看看compress() method documentation

    【讨论】:

      猜你喜欢
      • 2011-04-30
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      相关资源
      最近更新 更多