簡體   English   中英

將布局轉換為 bitmap?

[英]Convert layout to bitmap?

如何將以下布局轉換為 bitmap?

我嘗試使用 Kotlin 的view.drawToBitmap()但它僅適用於視圖而不適用於布局。

            <LinearLayout
                android:id="@+id/layout"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:orientation="vertical">

            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/tv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            </LinearLayout>

將您的視圖傳遞給viewToBitmap(view)

private Bitmap viewToBitmap(LinearLayout view) {
            try {
               // create bitmap screen capture
                view.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
                view.setDrawingCacheEnabled(false);
                return bitmap;
            } catch (Throwable e) {
                e.printStackTrace();
                Log.e(TAG, "takeScreenshot: Throwable: "+e.getMessage() );
                return null;
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM