Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将布局另存为位图_Android_Bitmap_Android Linearlayout - Fatal编程技术网

Android 将布局另存为位图

Android 将布局另存为位图,android,bitmap,android-linearlayout,Android,Bitmap,Android Linearlayout,我想将线性布局保存为bmp <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout_QrCode" > <Space android

我想将线性布局保存为bmp

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout_QrCode" >

        <Space
            android:layout_width="15dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Space
                android:layout_width="match_parent"
                android:layout_height="15dp" />

            <ImageView
                android:layout_width="185dp"
                android:layout_height="185dp"
                android:id="@+id/imageView_qrcode" />

            <Space
                android:layout_width="match_parent"
                android:layout_height="15dp" />

        </LinearLayout>

        <RelativeLayout
            android:layout_width="417dp"
            android:layout_height="match_parent" >

            <Space
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_alignParentStart="true"
                android:id="@+id/space2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/textView_QrSticker_label"
                android:layout_alignParentStart="true"
                android:layout_above="@+id/space2"
                android:textStyle="bold"
                android:textSize="40sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/textView_QrSticker_serial"
                android:layout_below="@+id/space2"
                android:layout_alignParentStart="true"
                android:textSize="30sp" />
        </RelativeLayout>

    </LinearLayout>

但是如果我运行此代码,我的位图只包含
ImageView
(ImageView\u qrcode)。我查过了。如果我将线性视图充气,则
文本视图
显示正确,但输出bmp仅包含
图像视图
有人知道原因吗?

您可以尝试以下解决方案:

public static Bitmap getBitmapFromView(View view) {
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) {
    bgDrawable.draw(canvas);
    } else {
        canvas.drawColor(Color.WHITE);
    }
    view.draw(canvas);
    return returnedBitmap;
}
有一个图形缓存解决方案:

public Bitmap getBitmapFromView(View view){
    view.setDrawingCacheEnabled(true);
    return view.getDrawingCache();
}
使用setDrawingCacheEnabled(true)


多亏了@Gil Moshayof,它现在可以工作了

通过post调用getBitmapFromView方法后,它工作正常

                qrCodeSticker.setImageBitmap(((MainActivity) getActivity()).createQrCode(stringForQrCode));
                labelSticker.setText(qrDataList[1]);
                serialSticker.setText(qrDataList[2]);


                qrcodefinal.post(new Runnable() {
                    @Override
                    public void run() {
                        qrcodefinal.setImageBitmap(MainActivity.getBitmapFromView(qrStickerLayout));
                    }
                });

所以问题是你没有看到文本视图?什么时候在文本视图上设置文本?如果您在尝试绘制主线性布局的同时执行此操作,则可能需要在帖子上调用viewToBitmap方法。问题是我可以在线性布局中看到文本视图,但在“绘制”位图中看不到文本视图。以下是我执行的步骤:1。将图像设置为ImageView 2。将标签textview设置为3。将串行文本视图设置为4。调用getBitmapFromView方法all-in-1方法尝试通过post调用getBitmapFromView(即myView.post(new Runnable(){…viewToBitmap(…使用此代码,我看到整个高度和宽度都已保存。但图像仍然不包含TextView…)。。。。
View content = (LinearLayout)findViewById(R.id.linearLayout_QrCode);
    content.setDrawingCacheEnabled(true);
    Bitmap bitmap = content.getDrawingCache();
    File file = new File("/sdcard/" + yourimagename + ".png");
    try {
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, ostream);
        ostream.close();
        content.invalidate();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
            content.setDrawingCacheEnabled(false);
    }
                qrCodeSticker.setImageBitmap(((MainActivity) getActivity()).createQrCode(stringForQrCode));
                labelSticker.setText(qrDataList[1]);
                serialSticker.setText(qrDataList[2]);


                qrcodefinal.post(new Runnable() {
                    @Override
                    public void run() {
                        qrcodefinal.setImageBitmap(MainActivity.getBitmapFromView(qrStickerLayout));
                    }
                });