Mak*_*nov 13 android google-maps google-play-services
我在Fragment和最近的Android Map Utils中使用SupportMapFragment 进行聚类.Google Play服务更新至9.0.83后,谷歌单一地图标记将被缩放后的边界矩形所取代.只更换单个标记,簇标记很好.更改应用清单中的硬件加速参数不会改变任何内容.怎么解决?
PS
compile 'com.google.android.gms:play-services-maps:8.4.0'
Run Code Online (Sandbox Code Playgroud)
我在github项目中使用了问题的简化版@bishop87的解决方法.还为集群位图添加了缓存,这使得OOM更加安全.
如果您没有群集渲染器而不是使用此渲染器或将此代码移动到您自己的代码:
SimpleClusterRenderer.java
public class SimpleClusterRenderer extends DefaultClusterRenderer<AuctionItem> {
private static final int CLUSTER_PADDING = 12;
private static final int ITEM_PADDING = 7;
private final Bitmap mIconItemGreen;
private final IconGenerator mIconClusterGenerator;
private final float mDensity;
public SimpleClusterRenderer(Context context, GoogleMap map, ClusterManager<AuctionItem> clusterManager) {
super(context, map, clusterManager);
mDensity = context.getResources().getDisplayMetrics().density;
mIconClusterGenerator = new CachedIconGenerator(context);
mIconClusterGenerator.setContentView(makeSquareTextView(context, CLUSTER_PADDING));
mIconClusterGenerator.setTextAppearance(com.google.maps.android.R.style.ClusterIcon_TextAppearance);
IconGenerator iconItemGenerator = new IconGenerator(context);
iconItemGenerator.setContentView(makeSquareTextView(context, ITEM_PADDING));
iconItemGenerator.setBackground(makeClusterBackground(ContextCompat.getColor(context, R.color.simple_green)));
mIconItemGreen = iconItemGenerator.makeIcon();
}
@Override
protected void onBeforeClusterItemRendered(AuctionItem item, MarkerOptions markerOptions) {
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(mIconItemGreen));
}
@Override
protected void onBeforeClusterRendered(Cluster<AuctionItem> cluster, MarkerOptions markerOptions) {
int clusterSize = getBucket(cluster);
mIconClusterGenerator.setBackground(makeClusterBackground(getColor(clusterSize)));
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(mIconClusterGenerator.makeIcon(getClusterText(clusterSize)));
markerOptions.icon(descriptor);
}
@Override
protected boolean shouldRenderAsCluster(Cluster<AuctionItem> cluster) {
// Always render clusters.
return cluster.getSize() > 1;
}
private int getColor(int clusterSize) {
float size = Math.min((float) clusterSize, 300.0F);
float hue = (300.0F - size) * (300.0F - size) / 90000.0F * 220.0F;
return Color.HSVToColor(new float[]{hue, 1.0F, 0.6F});
}
private LayerDrawable makeClusterBackground(int color) {
ShapeDrawable mColoredCircleBackground = new ShapeDrawable(new OvalShape());
mColoredCircleBackground.getPaint().setColor(color);
ShapeDrawable outline = new ShapeDrawable(new OvalShape());
outline.getPaint().setColor(0x80ffffff);
LayerDrawable background = new LayerDrawable(new Drawable[]{outline, mColoredCircleBackground});
int strokeWidth = (int) (mDensity * 3.0F);
background.setLayerInset(1, strokeWidth, strokeWidth, strokeWidth, strokeWidth);
return background;
}
private SquareTextView makeSquareTextView(Context context, int padding) {
SquareTextView squareTextView = new SquareTextView(context);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
squareTextView.setLayoutParams(layoutParams);
squareTextView.setId(R.id.text);
int paddingDpi = (int) (padding * mDensity);
squareTextView.setPadding(paddingDpi, paddingDpi, paddingDpi, paddingDpi);
return squareTextView;
}
}
Run Code Online (Sandbox Code Playgroud)
CachedIconGenerator.java
public class CachedIconGenerator extends IconGenerator {
private final LruCache<String, Bitmap> mBitmapsCache;
private String mText;
public CachedIconGenerator(Context context) {
super(context);
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
mBitmapsCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
}
public Bitmap makeIcon(String text) {
mText = text;
return super.makeIcon(text);
}
@Override
public Bitmap makeIcon() {
if (TextUtils.isEmpty(mText)) {
return super.makeIcon();
} else {
Bitmap bitmap = mBitmapsCache.get(mText);
if (bitmap == null) {
bitmap = super.makeIcon();
mBitmapsCache.put(mText, bitmap);
}
return bitmap;
}
}
}
Run Code Online (Sandbox Code Playgroud)
PS您还需要更换所需的R.color.simple_green针脚颜色.
PPS忘了提一下,这种方法对性能的影响可以忽略不计.因此,如果Google将在下一个Play Services应用程序版本中修复此问题,最好使用Play Services 9.0.83和其他方法的不同方法更新此解决方案.
根据tyczj,当Google Play服务(专有二进制文件)更新到9.0.x时,就会发生这种情况.
从Github问题讨论的外观来看,解决方法是:
gmaps-api-issues页面可能的workarround:
change marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.drawableid));
到marker.setIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.drawableid))); //可能会影响一般内存消耗(?),我没有测试超过问题中报告的应用程序.
还注意到:
我可以证实这个问题.除了@Mavamaarten之外,您不能重复使用标记图像.
(来源:https://github.com/googlemaps/android-maps-utils/issues/276)
| 归档时间: |
|
| 查看次数: |
3275 次 |
| 最近记录: |