自定义视图转换为位图返回黑色图像 [英] Custom view converting to bitmap returning black image

查看:313
本文介绍了自定义视图转换为位图返回黑色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个自定义视图,然后将其保存为PNG文件到SD卡。现在我在SD卡越来越黑的彩色图像。我无法描绘出在code中的问题。任何人都可以请帮我。

布局文件:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =200dp
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=@机器人:彩色/白
    机器人:方向=垂直>
        <的TextView
            机器人:ID =@ + ID /标题
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字颜色=@机器人:彩色/黑白/>        <的TextView
            机器人:ID =@ + ID /地址
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字颜色=@机器人:彩色/黑白/>
< / LinearLyout>

在我的java文件,我已经膨胀的线性布局和设置数据textviews然后我打电话:

 私人位图convertViewToBitmap(的LinearLayout布局){
    layout.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
    INT宽度= layout.getMeasuredWidth();
    INT高度= layout.getMeasuredHeight();    //创建位图
    位图位图= Bitmap.createBitmap(宽度,
            高度,
            Bitmap.Config.ARGB_8888);
    //创建一个画布指定的位图绘制成
    帆布C =新的Canvas(位图);    //渲染这一观点(以及所有子)给定的画布
    view.draw(C);
    返回位图;
}

让我保存到SD卡,如下所示位图后:

 私人无效saveBitmapTpSdCard(位图位图,字符串文件名){
        ByteArrayOutputStream字节=新ByteArrayOutputStream();
        bitmap.com preSS(Bitmap.Com pressFormat.PNG,40字节);        文件f =新的文件(Environment.getExternalStorageDirectory()
                +文件分割符+的getString(R.string.app_name));        尝试{
            如果(!f.exists()){
                f.mkdirs();
            }
            文件镜像文件=新的文件(F,文件名+巴纽);
            FileOutputStream中FO =新的FileOutputStream(镜像文件);
            fo.write(bytes.toByteArray());
            fo.close();
        }赶上(IOException异常五){
            e.printStackTrace();
        }
}


解决方案

我得到了它的工作,加入这一行:

  view.layout(0,0,宽度,高度);

使用Bitmap.createBitmap创建位图前

I need to create a custom view and then save it to png file into sdcard. Right now I am getting black colour images in sdcard. I couldn't trace out the issue in code. Can anyone please help me out.

layout file:

<?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>

In my java file, I have inflated linear layout and set data to textviews and then I am calling:

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;
}

After getting bitmap I am saving it to sdcard as follows:

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();
        }
}

解决方案

I got it worked by adding this line:

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

before creating Bitmap using Bitmap.createBitmap

这篇关于自定义视图转换为位图返回黑色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆