Dynamically increase the size of custom marker in

2019-09-02 21:26发布

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.

1条回答
看我几分像从前
2楼-- · 2019-09-02 22:01

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:

enter image description here

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

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

And it turned out like this:

enter image description here

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

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

查看更多
登录 后发表回答