转换为位图时未渲染CardView阴影 [英] CardView shadow not rendered when converted to bitmap

查看:76
本文介绍了转换为位图时未渲染CardView阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我尝试将视图组(其子级之一为CardView)另存为PNG文件.为此,

I'm try to save a view group (which has a CardView as one of its childern) as a PNG file. To achieve this,

  1. 我将视图组膨胀,并在视图中填充必需的信息
  2. 通过 Glide
  3. 将图像加载到图像视图
  4. 在图像视图的ViewTreeObserver中添加ViewTreeObserver.OnGlobalLayoutListener并将要共享的整个(父)视图传递给将图像转换为位图的方法.视图的底部大于零(图像视图的height属性设置为wrap_content,因此在加载图像之前其底部将为零).
  1. I inflate the view group and populate the views with required information
  2. Load an image to an image view via Glide
  3. Add a ViewTreeObserver.OnGlobalLayoutListener to the ViewTreeObserver of the image view and pass the entire (parent) view that is going to be shared to a method that converts the view to a bitmap when image view's bottom is greater than zero (image view's height attribute is set to wrap_content, thus its bottom will be zero until image is loaded).

这样做,我可以将视图转换为位图,但是需要注意的是:CardView的显示未呈现在位图上.

By doing this, I'm able to convert the view to a bitmap, however, with one caveat: the CardView's show is not rendered on the bitmap.

失败的尝试

到目前为止,我已经尝试过:

So far I've tried:

  1. layerType属性从软件"到硬件"之间切换.
  2. 设置和关闭CardViewcardUseCompatPadding属性.
  3. 尝试设置不使用Glide的可绘制图像.
  4. 尝试不加载任何可绘制的图像.
  1. Switching between layerType attribute from "software" to "hardware".
  2. Setting on and off cardUseCompatPadding attribute of the CardView.
  3. Tried setting the image drawable without using Glide.
  4. Tried without loading an image drawable at all.

代码

以下代码段可以帮助您找出问题所在:

Here are code snippets that might help you guys identify the problem:

用于将视图转换为位图的方法

The method used to convert a view to a bitmap

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布局文件.

XML layout file of the view that's being inflated and passed to the 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即可查看并设置

just change cardview to view, and set

android:background ="@ android:drawable/dialog_holo_light_frame"

android:background="@android:drawable/dialog_holo_light_frame"

因为您需要自己进行填充

ofcause you need to deal the padding yourself

这篇关于转换为位图时未渲染CardView阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆