2016-04-23 90 views
0

我想显示在我的应用程序谷歌地图的图像,以便它匹配屏幕的宽度。 我使用DisplayMetrics作为以下代码,并将其传递给谷歌以获取该大小的图像(请参阅url参数)。但正如你在截图中看到的那样,它显示错误的宽度。如何修复它?我认为我发送的参数为谷歌是不正确的?Android - 如何适合谷歌地图图像到屏幕宽度

DisplayMetrics displaymetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
int heightP = (int) displaymetrics.heightPixels; 
int widthP = (int) displaymetrics.widthPixels; 
int height = (int) (displaymetrics.heightPixels/displaymetrics.density); 
int width = (int) (displaymetrics.widthPixels/displaymetrics.density); 

................ 

    ImageView imgImage = (ImageView) findViewById(R.id.imgImage); 
    //TextView txtDetail = (TextView) findViewById(R.id.txtDetail); 

    imgImage.getLayoutParams().width = LayoutParams.MATCH_PARENT; 
    imgImage.getLayoutParams().height = (height/2); 
............... 

     ImageView gmap = (ImageView) findViewById(R.id.googlemap); 
     gmap.getLayoutParams().height = 200; 
     //gmap.requestLayout(); 
     String url = "http://maps.google.com/maps/api/staticmap?center=" + estate.getString("maplat") + "," + estate.getString("maplng") + "&zoom=" + estate.getString("mapzoom") + "&size=" + Integer.toString(widthP - 10) + "x200&sensor=false&format=jpeg&&markers=color:red|" + estate.getString("maplat") + "," + estate.getString("maplng"); 

     Picasso.with(this) 
       .load(url) 
       .placeholder(R.drawable.animated_progress) 
       .into(gmap); 

布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/detailLayer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layoutDirection="rtl" 
    android:padding="8dp" 
    tools:context=".DetailActivity"> 


    <ScrollView 
     android:id="@+id/scrollview1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:textAlignment="gravity" 
      android:textDirection="rtl"> 

      <Button 
       android:id="@+id/btnEdit" 
       style="@style/CKButton" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="12dp" 
       android:text="@string/txt_manage" 
       android:visibility="gone" /> 

      <ImageView 
       android:id="@+id/imageView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="-50dp" 
       android:src="@drawable/sold" 
       android:visibility="gone" /> 


      <ImageView 
       android:id="@+id/imgImage" 
       android:layout_width="match_parent" 
       android:layout_height="300dp" 
       android:clickable="true" 
       android:paddingBottom="8dp" 
       android:paddingTop="8dp" 
       android:scaleType="centerCrop" 
       android:textAlignment="gravity" /> 


</ScrollView> 

</FrameLayout> 

这里是问题的图像:

here is issue

+0

在layout中:在图像视图中设置属性android:scaleType =“fitXY”。 –

回答

0

尝试在您的ImageView设置属性android:scaleType="fitXY"

0

通过使用XML,只是把属性,

android:scaleType="fitXY" 

您的ImageView。像

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    **android:scaleType="fitXY"** 
    android:src="@drawable/logo" /> 

通过将Java,

imageview.setScaleType(ScaleType.FIT_XY); 
0

是的,因为你在这是不对的密度像素发送图像宽度的URL参数是不正确的。

在你的url中,只需要传递像素值而不是dp(密度像素)的值。

有关谷歌地图网址的更多信息,请阅读this doc

它可以帮助发送哪些类型的参数传递并获得更准确的地图图像大小。

screen_width = getResources().getDisplayMetrics().widthPixels; 

String url = "http://maps.google.com/maps/api/staticmap?center=" + estate.getString("maplat") + "," + estate.getString("maplng") + "&zoom=" + estate.getString("mapzoom") + "&size=" + Integer.toString(screen_width - 10) + "x200&sensor=false&format=jpeg&&markers=color:red|" + estate.getString("maplat") + "," + estate.getString("maplng"); 

并将其传递到您的网址。