我想将我的Linearlayout保存为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>
因此我使用以下代码执行此操作:
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
但如果我运行此代码,我的位图只包含ImageView
(ImageView_qrcode)。我检查了一下。如果我给Linearview充气,TextViews
显示正确,但输出bmp只包含ImageView
有人知道为什么吗?
答案 0 :(得分:6)
你可以尝试这个解决方案:
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();
}
答案 1 :(得分:1)
使用setDrawingCacheEnabled(true);
........ LIKE %".$search."% ORDER BY braid ASC LIMIT 1";
答案 2 :(得分:1)
感谢@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));
}
});