动态增加 google 地图中自定义标记的大小 - Android

Dynamically increase the size of custom marker in google maps - Android

我使用 Picasso 检索位图图像用作标记图标,并且应用成功。但我需要为所有设备动态增加自定义标记的大小。因为使用下面的代码,它在某些设备上运行良好,但在其他一些设备上,标记非常大。

这是我的代码

dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude());
markerOption = new MarkerOptions().position(dest);
location_marker = mMap.addMarker(markerOption);
Target target = new PicassoMarker(location_marker);
targets.add(target);

Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(84, 125).into(target);
mMarkersHashMap.put(location_marker, myMarker);

请帮我解决这个问题。

提前致谢。

根据您提供的代码,您只需修改您在 resize() 中传递的值:

Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(84, 125).into(target);

我试过了,你的代码是这样的:

然后我修改了resize()中的值,像这样:

Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(184, 1125).into(target);

结果是这样的:

要传递的值 resize() 完全取决于您。 ;)

EDIT

如果您关心 根据屏幕尺寸传递什么 值,您可以从另一个 post 参考此 answer。这是它所说的 --

您可以在代码中使用 LayoutParams 来做到这一点。不幸的是,没有办法通过 XML 指定百分比(不是直接的,你可以乱用权重,但这并不总是有帮助,而且它不会保持你的纵横比),但这应该适合你:

//assuming your layout is in a LinearLayout as its root
LinearLayout layout = (LinearLayout)findViewById(R.id.rootlayout);

ImageView image = new ImageView(this);
image.setImageResource(R.drawable.image);

int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = image.getDrawable().getIntrinsicWidth();
int orgHeight = image.getDrawable().getIntrinsicHeight();

//double check my math, this should be right, though
int newWidth = Math.floor((orgWidth * newHeight) / orgHeight);

//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    newWidth, newHeight);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.addView(image);

-- 这是一个很好的例子,只需测量布局即可将其用作如何调整图像大小的参考。