【问题标题】:Custom view converting to bitmap returning black image自定义视图转换为位图返回黑色图像
【发布时间】:2014-05-08 07:42:18
【问题描述】:

我需要创建一个自定义视图,然后将其保存为 png 文件到 sdcard。现在我在 sdcard 中得到黑色图像。我无法在代码中找出问题所在。谁能帮帮我。

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical" >
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black" />
</LinearLyout>

在我的 java 文件中,我膨胀了线性布局并将数据设置为 textviews,然后我正在调用:

private Bitmap convertViewToBitmap(LinearLayout layout) {
    layout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    int width = layout.getMeasuredWidth();
    int height = layout.getMeasuredHeight();

    //Create the bitmap
    Bitmap bitmap = Bitmap.createBitmap(width, 
            height, 
            Bitmap.Config.ARGB_8888);
    //Create a canvas with the specified bitmap to draw into
    Canvas c = new Canvas(bitmap);

    //Render this view (and all of its children) to the given Canvas
    view.draw(c);
    return bitmap;
}

获取位图后,我将其保存到 sdcard,如下所示:

private void saveBitmapTpSdCard(Bitmap bitmap, String fileName) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);

        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + getString(R.string.app_name));

        try {
            if(!f.exists()) {
                f.mkdirs();
            }
            File imageFile = new File(f, fileName + ".png");
            FileOutputStream fo = new FileOutputStream(imageFile);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

【问题讨论】:

  • 您是否要获取 lnearlayout 内容的快照?
  • @Glenn——是的。我也想将内容放入图像中
  • @Glenn——我试过这个。但是 layout.getDrawingCache();正在返回 null
  • 让我们看看更新后的代码(你的代码带有绘图缓存方法)?

标签: android bitmap


【解决方案1】:

我通过添加这一行得到了它:

view.layout(0, 0, width, height);

在使用 Bitmap.createBitmap 创建位图之前

【讨论】:

  • 我想知道为什么该方法返回 null 但是,干得好!
【解决方案2】:

尝试以下代码将自定义视图转换为位图对象:

    LinearLayout layout = (LinearLayout) findViewById(R.id.layout_main_for_bitmap);
    layout.setDrawingCacheEnabled(true);
    layout.buildDrawingCache();
    Bitmap bitmap = layout.getDrawingCache();

【讨论】:

  • 我在膨胀和设置值后将布局传递给方法
【解决方案3】:

LinearLayout 是由您手动测量的这一事实并不意味着它已被布局(即 layout 未被调用)。因此孩子们没有得到布局,因此他们在ViewGroup 上没有收到正确的位置,因此他们不可见。

我建议您不要尝试模仿绘图生命周期,而是在您的 LinearLayout 上调用 linearLayout.post()(当然,如果它已添加到视图层次结构中)并在 Runnable 内部调用您的“捕捉”工作流程。

您也可以手动调用layout

【讨论】:

  • @Mathew 所以我的第一个想法是正确的 :) 恭喜!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 2014-04-27
相关资源
最近更新 更多