我在百度地图上遇到的问题和这个 SO 问题很像,但是它的回答没有解决我的问题。
我想用自己的 res/drawable 资源来作为百度地图上的 Marker。但是这样使用后发现图片(icon_marker.png)加载出来边缘很模糊,而且即使我使用更大的图片,这个 Marker 边缘仍然是模糊的(只是被放大了)。
我已经尝试了在包括 hdpi, mdpi, xhdpi, xxhdpi, xxxhdpi 的各个文件夹中都放这个图片的各种版本,或者只在那个没有后缀的 drawable 文件夹下放一个 png,但仍然是模糊的。大图大模糊,小图小模糊。
以下是我用百度地图显示 Marker 的代码。请问我的问题出在了哪里?
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_marker);
LatLng latLng = ...;
OverlayOptions option = new MarkerOptions()
.position(latLng)
.icon(bitmap);
mBaiduMap.addOverlay(option);
伊谢尔伦2017-04-18 09:59:17
I found the answer to this question by a very, very coincidental attempt. Inspired by this: Custom map pin appears way too large and is pixelated?.
The guy below is a Top Contributor, probably a Google engineer. His reply (translated):
Google Maps has adjusted all icons to
32*32 px
的大小(有时候是两倍,64px
)。
所以你应该把你的图像文件设置成32*32 px
. If you want a smaller icon, you should keep the transparent pixels on the sides.
(If you change the icon, it's best to use a new file name, as the original image may stay in the cache and you won't see the change.)
I tried it with the attitude that Baidu Map plagiarized Google Map (because it turns out that the length and width of my image resources are not equal)...
Sure enough, setting it to 32*32 px
后,图标变得清晰无比!64*64 px
,128*128 px
is no problem.
大家讲道理2017-04-18 09:59:17
I have never studied Android SDK for Baidu Maps. After reading the documentation, there is such a method
public static BitmapDescriptor fromBitmap(Bitmap image)
See if you can create Bitmap
objects from resources yourself, specify the size, load the large image with a smaller size, and see if there will be an improvement.