Rod*_*ble 2 android bitmap android-linearlayout
我想将我的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>
Run Code Online (Sandbox Code Playgroud)
因此,我使用以下代码执行此操作:
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;
}
Run Code Online (Sandbox Code Playgroud)
但如果我运行此代码,我的位图只包含ImageView(ImageView_qrcode).我检查了一下.如果我给Linearview充气TextViews正确显示,但输出bmp只包含ImageView有人知道的原因吗?
你可以尝试这个解决方案:
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;
}
Run Code Online (Sandbox Code Playgroud)
有绘图缓存的解决方案:
public Bitmap getBitmapFromView(View view){
view.setDrawingCacheEnabled(true);
return view.getDrawingCache();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1469 次 |
| 最近记录: |