简体   繁体   English

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

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

In my google map application, I am using custom marker for different locations. 在我的Google Map应用程序中,我在不同位置使用自定义标记。 I implemented this successfully but I need to dynamically increase the size of the custom marker. 我成功实现了这一点,但是我需要动态增加自定义标记的大小。 I used the following code 我用下面的代码

imgView.getLayoutParams().height = 200;
imgView.getLayoutParams().width = 200;

the codes are working fine in some devices but in nexus4 and 5 the marker is very big. 该代码在某些设备上运行良好,但在nexus4和5中,标记非常大。 please help me to fix the issue 请帮助我解决问题

Custom Marker XMl 自定义标记XMl

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView     
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:src="@drawable/marker" />

    <TextView           
        android:layout_width="36dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:textSize="14sp"
        android:textStyle="bold" />

</RelativeLayout>

With

imgView.getLayoutParams().height = 200;

you are specifying the new height in pixels , ignoring the current device's pixel density. 您将以像素为单位指定新高度,而忽略当前设备的像素密度。 You should not do this. 您不应该这样做。

You can use your current method to enlarge the images by doing eg 您可以使用当前方法通过执行以下操作来放大图像

int height = imgView.getLayoutParams().height;
imgView.getLayoutParams().height = height * 2;

to make them 2x bigger. 使它们大2倍。 Instead of making the image a hardcoded 200px regardless of the device's pixel density, this will make the image 140px if it was 70px, or 200px if it was 100px. 不管设备的像素密度如何,都不必将图像硬编码为200px,如果图像为70px,则图像为140px;如果像素为100px,则图像为200px。
This will nicely adapt itself to the current device. 这将很好地适应当前设备。

You can also work with dp, by specifying a dp value and then converting it to pixels: 您还可以通过指定dp值,然后将其转换为像素来使用dp:

int dp = 72;
int height = imgView.getLayoutParams().height;
imgView.getLayoutParams().height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());

This will set the height to 72 by translating 72 into the right amount of pixels for the current device. 通过将72转换为当前设备的正确像素数量,这会将高度设置为72。

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

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