【发布时间】:2015-10-04 08:44:50
【问题描述】:
问题
我正在尝试将一个视图组(它有一个CardView 作为它的子元素之一)保存为 PNG 文件。为此,
- 我膨胀视图组并使用所需信息填充视图
- 通过Glide将图像加载到图像视图
- 将
ViewTreeObserver.OnGlobalLayoutListener添加到图像视图的ViewTreeObserver并将要共享的整个(父)视图传递给将视图转换为位图的方法when 图像视图的底部大于零(图像视图的高度属性设置为wrap_content,因此在加载图像之前其底部将为零)。
通过这样做,我可以将视图转换为位图,但是需要注意一点:CardView 的节目不会呈现在位图上。
失败的尝试
到目前为止我已经尝试过:
- 在
layerType属性从“软件”到“硬件”之间切换。 - 设置
cardUseCompatPaddingCardView属性的开启和关闭。 - 尝试在不使用 Glide 的情况下设置可绘制图像。
- 尝试完全不加载可绘制的图像。
代码
这里有代码 sn-ps 可以帮助你们识别问题:
将视图转换为位图的方法
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;
}
正在膨胀并传递给上述getBitmapFromView() 的视图的XML 布局文件。
<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>
【问题讨论】:
-
不确定是否相关,但也许您可以尝试为卡片视图设置一个小的
android:padding和android:clipToPadding="false"? -
很确定它与父视图或卡片视图的填充无关。阴影在屏幕上渲染没有任何问题,并且有足够的空间来确保这一点。不过还是谢谢。
-
我也面临同样的问题,但我更感兴趣的是把圆角绘制到我的画布上。既没有圆角,也没有绘制阴影。我也使用 view.draw(canvas)
-
这里也一样。关于这个问题的任何更新?
-
@galex 不,没有:(
标签: android android-layout bitmap shadow android-cardview