2017-02-20 17 views
0

こんにちは私は、URLから画像を表示するIm​​ageViewを持っています。画像のサイズは異なります。それらは、正方形または肖像画または横長のいずれかの形状であってもよい。ストレッチやトリミングをせずに、画像の表示サイズを適切に設定する必要があります。Androidは画像サイズによって画像サイズを動的に変更します

画像サイズが512x512の場合、ImageViewは四角形で、画面の中央に配置する必要があります。

画像のサイズが1024x800の場合、ImageViewのサイズは水平の矩形で、画面の中央に揃えてください。

すべての場合、幅は親と一致する必要がありますが、高さはそれに応じて調整する必要があります。これどうやってするの?

以下、私のXMLコードです。

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <android.support.v7.widget.CardView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:maxHeight="400dp" 
     android:maxWidth="300dp" 
     android:layout_margin="10dp" 
     app:cardBackgroundColor="@color/white"> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <android.support.v4.widget.NestedScrollView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:scrollbars="none"> 
       <ImageView 
        android:id="@+id/image" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:adjustViewBounds="true" 
        android:scaleType="fitXY" 
        android:minWidth="260dp" 
        tools:ignore="ContentDescription" /> 
      </android.support.v4.widget.NestedScrollView> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="1dp" 
       android:background="@color/grey_light" /> 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="match_parent" 
       android:layout_height="35dp" 
       android:layout_gravity="right" 
       android:layout_margin="@dimen/space_small_low" 
       android:background="@drawable/transparent_bg" 
       android:textColor="@color/orange" /> 
     </LinearLayout> 
    </android.support.v7.widget.CardView> 
</FrameLayout> 
+0

削除アンドロイド:XML からscaleType = "fitXY" &あなたのイメージの高さは、それがImageViewの中に空白スペースを作成します削除(deviceheight * imagewidth)/ devicewidth – user3040153

+0

のようなアスペクト比で計算します。 –

答えて

1

ImageViewの一連の幅XMLでmatch_parent、コードの下に使用してアスペクト比を維持するために、実行時に画像の高さを計算する:

Display display = getActivity().getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int newWidth = size.x; 

//Get actual width and height of image 
int width = bitmap.getWidth(); 
int height = bitmap.getHeight(); 

// Calculate the ratio between height and width of Original Image 
float ratio = (float) height/(float) width; 
float scale = getApplicationContext().getResources().getDisplayMetrics().density; 
int newHeight = (int) (width * ratio)/scale; 

このnewHeightとnewWidthを渡しイメージ処理ライブラリの.resize関数を呼び出してイメージのサイズを変更します。

android:scaleType="centerInside"し、 ImageView幅応じて画面の幅を設定し

+0

ありがとう@nnn。それはうまくいった。 –

0

変更android:scaleType="fitXY"、あなたは画像の縦横比を変えずに、高さ自体を計算することができる画面幅またはそのような何かの60パーセントでImageViewのための幅を計算し、android:scaleType="centerInside"を使用する必要がありますを意味します。

0

J put put wrap_content自動的にサイズがかかります。 アンドロイド:layout_height = "wrap_cont`enterコードhere`ent" Picassoライブラリの />

+0

完全な質問をお読みください。彼はイメージの幅を常にmatch_parentにしたいと思っています。 – nnn

0

聞いたことありますか?それはイメージを中心にするだけでなく、キャッシュの複雑さを管理します。

セットアップ方法は?この依存関係をあなたのGradleに追加して同期します。

compile 'com.squareup.picasso:picasso:2.5.2' 

ライブラリの使用方法は?単純ですが、シングルトンコードを呼び出すだけです。

ImageView imageView = .... 
Picasso.with(contextHere()) 
     .load("http://example.com/myimage.jpg) 
     .fit() 
     .centerCrop() 
     .into(imageView); 
関連する問題