问题
我尝试保存一个视图组(其中有一个CardView
作为其子级之一)作为 PNG 文件。为了达成这个,
- 我膨胀视图组并使用所需信息填充视图
- 通过以下方式将图像加载到图像视图Glide https://github.com/bumptech/glide
- Add a
ViewTreeObserver.OnGlobalLayoutListener
to the ViewTreeObserver
图像视图的并将要共享的整个(父)视图传递给将视图转换为位图的方法when图像视图的底部大于零(图像视图的高度属性设置为wrap_content
,因此在加载图像之前其底部将为零)。
通过这样做,我可以将视图转换为位图,但是有一个警告:CardView
的显示未呈现在位图上。
失败的尝试
到目前为止我已经尝试过:
- 之间切换
layerType
属性从“软件”到“硬件”。
- 设置开启和关闭
cardUseCompatPadding
的属性CardView
.
- 尝试在不使用 Glide 的情况下设置可绘制图像。
- 尝试完全不加载可绘制图像。
THE CODE
以下是可能帮助你们识别问题的代码片段:
用于将视图转换为位图的方法
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(b);
//Get the view's background
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return b;
}
正在膨胀并传递给视图的 XML 布局文件getBitmapFromView()
above.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp">
<com.devspark.robototextview.widget.RobotoTextView
android:id="@+id/title"
style="@style/text_subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="@dimen/lessons_horizontal_margin_narrow"
android:layout_marginLeft="@dimen/lessons_horizontal_margin_narrow"
android:layout_marginRight="@dimen/lessons_horizontal_margin_narrow"
android:layout_marginTop="@dimen/lessons_horizontal_margin_narrow"
android:gravity="left"
app:typeface="roboto_medium" />
<com.devspark.robototextview.widget.RobotoTextView
android:id="@+id/text"
style="@style/text_subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/lessons_horizontal_margin_narrow"
android:layout_marginRight="@dimen/lessons_horizontal_margin_narrow"
android:gravity="left"
android:textColor="@color/text"
app:typeface="roboto_regular" />
<android.support.v7.widget.CardView
android:id="@+id/image_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/lessons_horizontal_margin_narrow"
app:cardCornerRadius="@dimen/lessons_image_card_corner_radius"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@null"
app:riv_corner_radius_top_left="@dimen/lessons_image_card_corner_radius"
app:riv_corner_radius_top_right="@dimen/lessons_image_card_corner_radius" />
<com.devspark.robototextview.widget.RobotoTextView
android:id="@+id/caption"
style="@style/text_caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/lessons_image_card_caption_margin"
android:gravity="left"
app:typeface="roboto_condensed_regular" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Some other views that aren't particularly interesting -->
</LinearLayout>
只需将cardview更改为view,然后设置
android:background="@android:drawable/dialog_holo_light_frame"
当然你需要自己处理填充
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)