Android 转换为位图时未渲染CardView阴影
问题 我正在尝试将一个视图组(其中有一个Android 转换为位图时未渲染CardView阴影,android,android-layout,bitmap,shadow,android-cardview,Android,Android Layout,Bitmap,Shadow,Android Cardview,问题 我正在尝试将一个视图组(其中有一个cardwiew作为其子视图之一)保存为PNG文件。为此, 我膨胀视图组并用所需信息填充视图 通过将图像加载到图像视图 将ViewTreeObserver.OnGlobalLayoutListener添加到图像视图的ViewTreeObserver,并将要共享的整个(父)视图传递给一个方法,该方法在图像视图的底部大于零时将视图转换为位图(图像视图的高度属性设置为包裹内容,因此在加载图像之前,其底部将为零) 通过这样做,我可以将视图转换为位图,但是有一点需要
cardwiew
作为其子视图之一)保存为PNG文件。为此,
ViewTreeObserver.OnGlobalLayoutListener
添加到图像视图的ViewTreeObserver
,并将要共享的整个(父)视图传递给一个方法,该方法在图像视图的底部大于零时将视图转换为位图(图像视图的高度属性设置为包裹内容
,因此在加载图像之前,其底部将为零)cardwiew
尝试失败
到目前为止,我已经尝试:
layerType
属性之间切换CardView
的cardUseCompatPadding
属性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>
只需将cardview更改为view,然后设置 android:background=“@android:drawable/dialog\u holo\u light\u frame”
因为你需要自己处理填充物我也有同样的问题。所以在搜索之后,我发现了这个。希望这对你有所帮助
正如@galex所说的那样-阴影不是仅通过调用measure and layout在视图上绘制的 所以我们不能使用仰角,也不能使用可绘制的阴影,因为这样我们得到的是锐角和直边 解决方案:使用png 9路径可调整大小的可绘制工具。为此,我们可以使用这个漂亮的工具:
不确定是否相关,但您可以尝试设置一个小的
android:padding
,以及android:clipToPadding=“false”
对于cardview?非常确定它与父视图或卡片视图的填充无关。阴影在屏幕上呈现没有任何问题,并且有足够的空间来确保这一点。不过谢谢。我也面临同样的问题,尽管我更感兴趣的是将圆角绘制到画布上。既不是圆角,也不是阴影gets绘制。我也在这里使用view.draw(canvas)相同。关于这个问题有任何更新吗?@galex nope,none:(虽然这不是最佳的,但在我的情况下效果很好!!请在您的答案中提供所有相关信息。请参阅此处:,为什么仅链接的答案不适合堆栈溢出的格式。
LDPI - x0.75
MDPI - x1.0// means original size
HDPI - x1.5
XHDPI - x2.0
XXHDPI - x3.0
XXXHDPI - x4.0