2016-03-25 28 views
0

enter image description here画像は幅900 * 346の画像で、詳細画像とサムネイル画像を表示するのに同じ画像を使用しています。画像は2列200dpの高さでグリッドビューに表示されていますが、画像は伸びています。ストレッチなしで幅の広い画像を表示する方法。前もって感謝します。画像を拡大する

のGridViewのXml:

<?xml version="1.0" encoding="utf-8"?> 
<GridView 
    android:id="@+id/grid_img" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:horizontalSpacing="@dimen/portfolio_grid_space" 
    android:verticalSpacing="@dimen/portfolio_grid_space" 
    android:layout_marginTop="@dimen/portfolio_grid_space" 
    android:layout_marginBottom="@dimen/portfolio_grid_space" 
    android:numColumns="2"/> 

GridViewのアダプタのxml:

<?xml version="1.0" encoding="utf-8"?> 
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/img" 
    android:scaleType="fitCenter" 
    android:layout_width="match_parent" android:layout_height="200dp"> 

</ImageView> 
+0

xmlファイルとスクリーンショットを投稿してください。 –

+0

@ Ravikumar11私の答えを見てください。 –

答えて

0

私は誤解しない場合は、画像を引き伸ばしながら、画像の幅/高さの比率を維持したいです。 は、あなたがご希望の画像を取得するためにscaleTypeで遊ぶことができます

android:adjustViewBounds="true" 
android:scaleType="centerCrop" 

:あなたは、あなたのxml ImageViewで以下の行を置くことができます。 あなたはAPI 17の下にデバイスを使用している場合は、カスタム ImageViewことができます。

public class ScaleAspectFillImageView extends ImageView { 

public ScaleAspectFillImageView(Context context) { 
    super(context); 
} 

public ScaleAspectFillImageView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public ScaleAspectFillImageView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    // If API level <= 17, 
    // Only need custom measurement for image whose size is smaller than imageView size. 
    // http://developer.android.com/reference/android/widget/ImageView.html#setAdjustViewBounds(boolean) 
    if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
     float viewWidth = MeasureSpec.getSize(widthMeasureSpec); 
     float viewHeight = MeasureSpec.getSize(heightMeasureSpec); 
     // Calculate imageView.height based on imageView.width and drawable aspect ratio using scale-aspect-fill mode. 
     Drawable drawable = getDrawable(); 
     if (drawable != null) { 
      float drawableWidth = drawable.getIntrinsicWidth(); 
      float drawableHeight = drawable.getIntrinsicHeight(); 
      // Only need custom measurement for image whose size is smaller than imageView size. 
      // Avoid divide by zero error. 
      boolean needCustomMeasurement = drawableWidth < viewWidth || drawableHeight < viewHeight; 
      if (needCustomMeasurement && drawableHeight > 0) { 
       float drawableAspectRatio = drawableWidth/drawableHeight; 
       // imageView.width and drawable aspect ratio must be greater than zero. 
       if (drawableAspectRatio > 0 && viewWidth > 0) { 
        float targetHeight = viewWidth/drawableAspectRatio; 
        setMeasuredDimension((int) viewWidth, (int) targetHeight); 
        return; // Already set the measured dimension. 
       } 
      } 
     } 
    } 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

}

+0

2行追加しましたが動作しません。 – Ravikumar11

+0

@ Ravikumar11答えを編集しました。上の2行でカスタムビューを使用できます。 –

+0

ありがとうございました – Ravikumar11

0

レイアウトandroid:scaleType="fitXY" PSのxmlファイルに与える:これはイメージがかなりandroid:src="..."で設定されている場合に適用されます背景がデフォルトで設定されているため、ビューにストレッチしてフィットするため、android:background="..."より大きくなります。