【问题标题】:In Android Elevation/Shadow is not showing when generating Bitmap of a View在 Android 中,在生成视图的位图时不显示高程/阴影
【发布时间】:2018-11-22 16:30:21
【问题描述】:

我正在生成视图的位图以将其显示为地图上的标记。它工作正常,但我面临的问题是视图的高度没有显示。

private Bitmap getMarkerBitmapFromView(View view) {

        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        //view.buildDrawingCache();
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        /*canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
        Drawable drawable = view.getBackground();
        if (drawable != null)
            drawable.draw(canvas);
        */
        view.draw(canvas);
        return returnedBitmap;

    }

【问题讨论】:

  • but the issue that i'm facing is that elevation of the view is not showing. 那么如何设置标记视图的高度?
  • 能否请您提及您的安卓版本。
  • @jiteshmohite 它是 Android 7,0(牛轧糖)。
  • @Ibrahim 我正在为我的视图设置高度,如下所示:android:elevation="3dp"
  • 高程图形是在视图周围添加的,并不是严格意义上的一部分。如果要获取立面图,则需要将视图包装在另一个视图中,然后从中生成位图。确保容器视图有足够的填充来正确包含视图的阴影。即使禁用了剪辑,我认为在位图上绘图也不会超出视图的范围。

标签: android bitmap google-maps-markers android-canvas android-elevation


【解决方案1】:
public static Bitmap generateBitmap(Context context, String msg) {
        Bitmap bitmap = null;
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //Inflate the layout into a view and configure it the way you like
        RelativeLayout view = new RelativeLayout(context);
        try {
            mInflater.inflate(R.layout.point_helper, view, true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        TextView tv = (TextView) view.findViewById(R.id.textView);
        tv.setText(msg);

        //Provide it with a layout params. It should necessarily be wrapping the
        //content as we not really going to have a parent for it.
        view.setLayoutParams(new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));

        //Pre-measure the view so that height and width don't remain null.
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        //Assign a size and position to the view and all of its descendants
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        //Create the bitmap
        bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

        //Create a canvas with the specified bitmap to draw into
        Canvas c = new Canvas(bitmap);

        //Render this view (and all of its children) to the given Canvas
        view.draw(c);

        return bitmap;
    }

Drawable作为布局背景,放入drawable文件夹并设置。

bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <item>
                <shape>
                    <solid android:color="#35000000" />
                    <corners android:radius="2dp" />
                </shape>
            </item>
            <item android:bottom="3dp" android:left="1dp" android:right="3dp" android:top="1dp">
                <shape>
                    <solid android:color="#ffffff" />
                    <corners android:radius="1dp" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>

我稍微修改了你的逻辑,看看这段代码。它需要一个布局。如果无法提供布局,请尝试给位图视图的父视图。

【讨论】:

  • 如果我们以带有高度/阴影的卡片视图为例,这将无法解决问题,并且位图将出现没有硬件加速的阴影/高度。
  • 您能否突出显示会使输出与我的代码不同的代码部分?看来您正在做同样的事情,我正在设置布局文本等,然后将视图传递给此方法,而您正在方法内部执行此操作。
  • 你和我的生成方式是不同的位图生成。你可以使用drawable,也许它可以解决你的问题。
  • 我的view有ImageView和TextView,只有ImageView有阴影
  • 这就是为什么,我为您提供了不同类型的位图生成方法。或者在父视图中使用drawable。很简单
猜你喜欢
  • 2016-08-06
  • 1970-01-01
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
相关资源
最近更新 更多