动态增加Google地图中自定义标记的大小-Android [英] Dynamically increase the size of custom marker in google maps - Android

查看:184
本文介绍了动态增加Google地图中自定义标记的大小-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Picasso检索位图图像以用作标记图标,并且成功应用了该图像.但是我需要为所有设备动态增加自定义标记的大小.因为下面的代码可以在某些设备上正常工作,但在某些其他设备上,标记非常大.

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.

这是我的代码

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()行中传递的值即可:

As per the code you provided, you can just modify the value you passed in the resize() in this line:

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

我尝试了一下,您的代码显示了这一点:

I tried it out, your code shows this:

然后我修改resize()中的值,如下所示:

Then I modified the value of in the resize(), like so:

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

结果是这样的:

resize()中传递的值全部取决于您. ;)

The value to pass in resize() all depends on you. ;)

编辑

如果您担心要根据屏幕大小通过什么值,则可以参考此答案来自其他帖子.这就是它的意思-

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

您可以在代码中使用LayoutParams来执行此操作.不幸的是,无法通过XML指定百分比(不是直接指定百分比,您可能会弄乱权重,但这并不总是有帮助,并且不会保持宽高比),但这应该对您有用:

You can do this with LayoutParams in code. 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:

//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.

这篇关于动态增加Google地图中自定义标记的大小-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆