-1

i'm developing an app for pax printer, so i need to convert my xml Layout(Linear,Relative,...) to Bitmap,my problem is when i convert any layout to image, the Created Bitmap width size is reshaped. why this happens? how i can solve this problem? this is my convert code:

LayoutInflater lInflater = (LayoutInflater)getApplicationContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = lInflater.inflate(R.layout.testlayout, null);
view.setDrawingCacheEnabled(true);
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false); // clear drawing cache
imgview.setImageBitmap(b);
storeImage(b); 

i tried many other ways of converting but i got same results. this is one of my layouts:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/printLL"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#fff">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/printing">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >


            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >

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



                    <TextView
                        android:id="@+id/text_logo"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_margin="2sp"
                        android:gravity="center"
                        android:text="Header"
                        android:textSize="13sp" />


                    <TextView
                        android:id="@+id/counter"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_margin="2sp"
                        android:gravity="center"
                        android:text="Title" />

                    <TableLayout
                        android:id="@+id/record_table"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_margin="5dp"
                        android:background="#ffffff"
                        android:gravity="center"
                        android:shrinkColumns="*"
                        android:stretchColumns="*"

                        >

                        <TableRow android:layout_gravity="center_horizontal">


                            <TextView
                                android:id="@+id/CustomerName"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="3sp"
                                android:text="Username" />


                        </TableRow>

                        <TableRow android:gravity="center_horizontal">

                            <TextView
                                android:id="@+id/customer_id"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="3sp"
                                android:text="TextView" />

                            <TextView
                                android:id="@+id/postalcode"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="3sp"
                                android:text="TextView" />


                        </TableRow>

                        <TableRow android:gravity="center_horizontal">


                            <TextView
                                android:id="@+id/Address"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="3sp"
                                android:text="TextView" />

                        </TableRow>

                        <TableRow android:gravity="center_horizontal">


                            <TextView
                                android:id="@+id/ID"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="3sp"
                                android:text="TextView" />


                            <TextView
                                android:id="@+id/customer_number"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="3sp"
                                android:text="TextView" />


                        </TableRow>

                        <TableRow android:gravity="center_horizontal">


                            <TextView
                                android:id="@+id/Serial"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="3sp"
                                android:text="TextView" />


                            <TextView
                                android:id="@+id/Digit"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="3sp"
                                android:text="TextView" />


                        </TableRow>

                        <TableRow android:gravity="center_horizontal">


                            <TextView
                                android:id="@+id/PrevCode"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="3sp"
                                android:text="TextView" />

                            <TextView
                                android:id="@+id/PrevDebt"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="3sp"
                                android:text="TextView" />


                        </TableRow>

                        <TableRow android:gravity="center_horizontal">


                            <TextView
                                android:id="@+id/PrevDate"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="3sp"
                                android:text="TextView" />


                        </TableRow>

                        <TableRow>

                            <TextView
                                android:id="@+id/Usage_type"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="3sp"
                                android:text="TextViwe" />
                        </TableRow>

                    </TableLayout>




                </LinearLayout>

            </ScrollView>







        </LinearLayout>

    </FrameLayout>

</LinearLayout>

the layout view

converted layout to bitmap

note that no matter what's in layout, textview,imageview,... or which layout type i used .allways same problem

thanks for any help

Alex Neo
  • 1
  • 4

1 Answers1

0

Try this out :

public static Bitmap view2Bitmap(final FrameLayout view) {
        if (view == null) return null;
        boolean drawingCacheEnabled = view.isDrawingCacheEnabled();
        boolean willNotCacheDrawing = view.willNotCacheDrawing();
        view.setDrawingCacheEnabled(true);
        view.setWillNotCacheDrawing(false);
        Bitmap drawingCache = view.getDrawingCache();
        Bitmap bitmap;
        if (null == drawingCache) {
            view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
            view.buildDrawingCache();
            drawingCache = view.getDrawingCache();
            if (drawingCache != null) {
                bitmap = Bitmap.createBitmap(drawingCache);
            } else {
                bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                view.draw(canvas);
            }
        } else {
            bitmap = Bitmap.createBitmap(drawingCache);
        }
        view.destroyDrawingCache();
        view.setWillNotCacheDrawing(willNotCacheDrawing);
        view.setDrawingCacheEnabled(drawingCacheEnabled);
        return bitmap;
    }