【问题标题】:Dynamically increase the size of custom marker in google maps - Android在谷歌地图中动态增加自定义标记的大小 - Android
【发布时间】:2016-07-14 14:26:51
【问题描述】:

我使用 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);

请帮我解决问题。

提前致谢。

【问题讨论】:

  • 有什么解决办法吗?任何解决方案将不胜感激。

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


【解决方案1】:

根据您提供的代码,您可以在此行中修改您在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() 传入的值完全取决于你。 ;)

编辑

如果您关心根据屏幕尺寸传递的 什么 值,您可以参考其他帖子中的 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);

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

【讨论】:

  • 是的,我使用resize(84, 125),它在某些设备上运行良好。但是我尝试了其他一些设备,标记非常大。但我需要为所有设备动态增加自定义标记的大小。所以在运行这个应用程序时,标记会调整设备的屏幕。我该怎么办?
  • 您应该考虑提供您正在寻找的值的公式,其中宽度和高度取决于屏幕尺寸。我认为answer 可能适合您。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 2017-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
相关资源
最近更新 更多