简体   繁体   English

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

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

I use Picasso to retrieve the Bitmap image to use as a marker icon and it apply successfully. 我使用毕加索检索位图图像以用作标记图标,并且它成功应用。 But I need to dynamically increase the size of the custom marker for all devices. 但是我需要为所有设备动态增加自定义标记的大小。 Because with my code below, it's working fine in some devices but in some others devices, the marker is very big. 因为下面的代码可以在某些设备上正常工作,但在其他一些设备上,标记很大。

Here's my code 这是我的代码

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);

Please help me to fix the issue. 请帮助我解决问题。

Thanks in advance. 提前致谢。

As per the code you provided, you can just modify the value you passed in the resize() in this line: 根据您提供的代码,您可以只修改在此行的resize()中传递的值:

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

I tried it out, your code shows this: 我尝试了一下,您的代码显示了这一点:

在此处输入图片说明

Then I modified the value of in the resize() , like so: 然后,我修改了resize()的值,如下所示:

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

And it turned out like this: 结果是这样的:

在此处输入图片说明

The value to pass in resize() all depends on you. 传递给resize()的值都取决于您。 ;) ;)

EDIT 编辑

If your concern is on what value to pass depending on the screen size, You can refer to this answer from a different post. 如果您担心要根据屏幕尺寸传递什么值,则可以从其他文章中参考此答案 Here's what it says -- 就是这样-

You can do this with LayoutParams in code. 您可以在代码中使用LayoutParams完成此操作。 Unfortunately there's no way to specify percentages through XML (not directly, you can mess around with weights, but that's not always going to help, and it won't keep your aspect ratio), but this should work for you: 不幸的是,没有办法通过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);

-- It's quite a good example, simply measure the layout to use it as reference on how you will size the image. -这是一个很好的示例,只需测量布局以将其用作如何调整图像大小的参考即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM