android - 在 Android 中,生成 View 的位图时不显示高程/阴影

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

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

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;

    }

最佳答案

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>

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

关于android - 在 Android 中,生成 View 的位图时不显示高程/阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50831336/

相关文章:

html - 在信息框中使用 closeclick 事件

Android 谷歌地图图钉在不同手机上显示不同尺寸

javascript - 谷歌地图仅在按 F12 后显示

android - 在适当的外部 map 应用程序中打开 KML/GPX 文件

java - Facebook 应用程序在 Android 中加载缓慢

c# - 在 C# 中使用 native HBitmap,同时保留 alpha channel /透明度

java - 内存泄漏与优化

安卓 : Deleting an occurrence of event deletes all of its occurrence

android - 如何让 Logcat 在 Android Studio 中保持全屏

android - 如何使用 ACTION_DEVICE_STORAGE_LOW 来避免 OutOfMemoryError?