【问题标题】:set image from drawable xml to google map marker将可绘制 xml 的图像设置为谷歌地图标记
【发布时间】:2014-06-21 20:33:17
【问题描述】:

我在特定的可绘制文件夹中有这一系列 xml-drawables,例如在 drawable-mpdi 中:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/m0_48px"
/> 

drawable-hdpi 不同之处仅在于 src 指向 m0_72px 等等。

我想让 android 决定在标记中使用什么图像分辨率。如果我这样做:

int imageRes = c.getResources().getIdentifier("drawable/m0",
                    null, c.getPackageName());
Marker marker = m.addMarker(new MarkerOptions()
            .position(new LatLng(s.lat, s.lon))
            .icon(BitmapDescriptorFactory.fromResource(imageRes))
            .anchor(0.5f, 1f));

我在 imagesRes 中获得了一些资源 ID,但 BitmapDescriptorFactory 不会接受。

如果我这样做:

int imageRes = c.getResources().getIdentifier("drawable/m0",
                        null, c.getPackageName());
Bitmap bitmap = BitmapFactory.decodeResource(c.getResources(), imageResource);

Marker marker = m.addMarker(new MarkerOptions()
    .position(new LatLng(s.lat, s.lon))
    .icon(BitmapDescriptorFactory.fromResource(imageRes))
    .anchor(0.5f, 1f));

我在位图变量中得到空值。

这个问题Set Image from drawable as marker in Google Map version 2 尤其是这个评论

嗯,你可以 - 你只需要先把它画到画布上 (drawable.draw(canvas)),然后将 Canvas 转储到位图。 - 克里斯 阔脚

让我无处可去,我无法复制提到的程序...

请,欢迎任何帮助!

【问题讨论】:

    标签: android google-maps-markers drawable google-maps-android-api-2 android-drawable


    【解决方案1】:
    icon(BitmapDescriptorFactory.fromResource(R.drawable.your_icon))
    

    应该可以正常工作。如果图标在每个文件夹(mdpi、hdpi...)中具有相同的名称,android 将选择正确的一个来使用。为什么标记图标有不同的名称?

    【讨论】:

    • 1) 问题是我只有在可绘制文件夹和 hdpi、xhdpi、.. 我有 xml(如上)指向所需分辨率的特定图像 2)它表示一些数量特定位置
    【解决方案2】:

    这应该可以正常工作,我知道谷歌地图正在使用图像的密度,但是一旦你尝试解码源,Android 将决定使用哪个图像。

        Bitmap bitmap = BitmapFactory.decodeResource(c.getResources(), R.drawable.icon);
        Marker marker = m.addMarker(new MarkerOptions()
        .position(new LatLng(s.lat, s.lon))
        .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
        .anchor(0.5f, 1f));
    

    drawable 必须指向 PNG drawable 或以位图为根的 drawable。

    【讨论】:

    • 这不起作用并崩溃并出现异常:com.google.maps.api.android.lib6.common.apiexception.b:无法解码图像。提供的图像必须是位图。
    • @Meanman 你确定这不是你的代码,或者你的结构中的某些东西失败了吗?您的异常与应该是 Bitmap 对象的解码图像有关,可以通过不同方式获取 Bitmap 对象
    • 只有当drawable是PNG drawable或以位图为根的drawable时才有效。
    猜你喜欢
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 2019-06-29
    相关资源
    最近更新 更多