我有一系列xml-drawables特别是drawable文件夹,例如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));
我在位图var。
中得到null此问题Set Image from drawable as marker in Google Map version 2,尤其是此评论
嗯,你可以 - 你只需要先将它画成Canvas (drawable.draw(canvas)),然后将Canvas转储到Bitmap。 - 克里斯 布罗德福特
我无处可去,我无法复制上述程序...
拜托,欢迎任何帮助!
答案 0 :(得分:2)
icon(BitmapDescriptorFactory.fromResource(R.drawable.your_icon))
应该工作得很好。如果图标在每个文件夹(mdpi,hdpi ...)中具有相同的名称,android将选择正确使用的文件夹。为什么标记图标的名称不同?
答案 1 :(得分:-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。