こんにちは私は、URLから画像を表示するImageViewを持っています。画像のサイズは異なります。それらは、正方形または肖像画または横長のいずれかの形状であってもよい。ストレッチやトリミングをせずに、画像の表示サイズを適切に設定する必要があります。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>
削除アンドロイド:XML からscaleType = "fitXY" &あなたのイメージの高さは、それがImageViewの中に空白スペースを作成します削除(deviceheight * imagewidth)/ devicewidth – user3040153
のようなアスペクト比で計算します。 –