Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 转换为位图时未渲染CardView阴影_Android_Android Layout_Bitmap_Shadow_Android Cardview - Fatal编程技术网

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
    属性
  • 尝试在不使用Glide的情况下将图像设置为可绘制
  • 尝试完全不加载可绘制的图像
  • 代码

    下面是一些代码片段,可以帮助你们识别问题:

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

    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路径可调整大小的可绘制工具。为此,我们可以使用这个漂亮的工具:

  • 为所有mdpi、hdpi、xhdpi、xxhdpi和xxxhdpi创建9路径文件
  • 将所有png文件放入res/drawables文件夹
  • 现在,我们可以在想要看到阴影的地方使用这种可绘制的背景
  • 请注意,对于不同的密度,必须更改以下参数:视图的高度和宽度、阴影偏移(x和y)、模糊、圆角半径和填充

    不同密度的乘数:


    不确定是否相关,但您可以尝试设置一个小的
    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