2016-04-23 91 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"); 

並將其傳遞到您的網址。