Convert inflated layout into a bitmap android

I am inflating a layout and converting it into and then i will share it using the as usual intents.

inflating the layout using -

LayoutInflater inflater2 = LayoutInflater.from(this);

        qr_sheetview = inflater2.inflate(R.layout.dummy_layout, mainlayout, false);

I tried three methods -

  • convert the inflated view into bitmap using this method
  • qr_sheetview.setDrawingCacheEnabled(true); qr_sheetview.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); qr_sheetview.layout(0, 0, qr_sheetview.getMeasuredWidth(), qr_sheetview.getMeasuredHeight()); qr_sheetview.buildDrawingCache(true); sharedbmap = Bitmap.createBitmap(qr_sheetview.getDrawingCache());

    But using this method my layout gets shrinked and overlapped.

    width was 1107 height was 448

    I am using Framelayout as rootlayout and relative layout for inner views.

  • Second method i am using is this - >

    sharedbmap = Bitmap.createBitmap(qr_sheetview.getWidth(), qr_sheetview.getHeight(), Bitmap.Config.ARGB_8888); canvas = new Canvas(sharedbmap); qr_sheetview.draw(canvas);

  • in this method error occurred : height and width must be > 0

    using this also gives me the same error

    qr_sheetview.post(new Runnable() {
                        @Override
                        public void run() {
    
                            sharedbmap = loadBitmapFromView(qr_sheetview);
    
                            shareBitmap(adjustOpacity(sharedbmap), "temporary");
    
                        }
                    });
    
  • the third method is this - >

    public static Bitmap loadBitmapFromView(View v) {

            int specWidth = View.MeasureSpec.makeMeasureSpec(0 /* any */, View.MeasureSpec.UNSPECIFIED);
            v.measure(specWidth, specWidth);
    
            Log.d("TAG", "height" + v.getMeasuredHeight());
            Log.d("TAG", "width" + v.getMeasuredWidth());
    
            Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(b);
            v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
            v.draw(c);
    
            return b;
        }
    
  • in this i am getting the same result as in first.

    the Layout is ->

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        >
    
        <RelativeLayout
            android:id="@+id/mainqrlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff">
    
            <RelativeLayout
                android:id="@+id/layout001"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp">
    
                <TextView
                    android:id="@+id/appname"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:gravity="center|bottom"
                    android:text="Some text"
                    android:textColor="#212121"
                    android:textStyle="bold"
                    android:textSize="24sp" />
    
                <ImageView
                    android:id="@+id/logo"
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:layout_centerHorizontal="true"
                    android:layout_below="@+id/appname"
                    android:layout_marginTop="10dp"
                    android:src="@drawable/logomain" />
    
    
            </RelativeLayout>
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/layout003"
                android:layout_below="@+id/layout001">
    
                <ImageView
                    android:id="@+id/qrimage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true" />
    
                <ProgressBar
                    android:id="@+id/progressBar"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_centerInParent="true"
                    android:visibility="gone" />
    
                <ImageView
                    android:id="@+id/qr_logo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:visibility="gone"
                    card_view:srcCompat="@drawable/kj_qr_logo_svg" />
    
    
    
    
            </RelativeLayout>
    
            <RelativeLayout
                android:id="@+id/layout003"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true">
    
                <TextView
                    android:id="@+id/vpaname"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:gravity="center"
                    android:text="kdvkjadsfkasmd"
                    android:textColor="#212121" />
    
                <TextView
                    android:id="@+id/qr_vpa"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/vpaname"
                    android:layout_centerHorizontal="true"
                    android:text="skdmfkjasdf"
                    android:textColor="#444444" />
    
                <TextView
                    android:id="@+id/text3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/qr_vpa"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="15dp"
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="ckasfdowmdmwkmdwkdmkwmdklmekld n sdafasdfasdfaasdf"
                    android:textColor="#727272" />
    
                <TextView
                    android:id="@+id/companyname"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/text3"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="10dp"
                    android:layout_marginTop="20dp"
                    android:gravity="bottom"
                    android:text="@string/companyname"
                    android:textColor="#727272"
                    android:textSize="10sp" />
    
            </RelativeLayout>
    
        </RelativeLayout>
    </FrameLayout>
    
    链接地址: http://www.djcxy.com/p/67634.html

    上一篇: ExtJS 4网格行编辑帮助

    下一篇: 将膨胀的布局转换为位图android