49

正方形の画像があります (ただし、この問題は長方形の画像にも当てはまります)。縦横比を維持しながら、必要に応じて画像を拡大して親を埋めるために、画像をできるだけ大きく表示したいと考えています。画像は ImageView より小さいです。問題は、画像を引き伸ばして、ImageView の高さと幅を「一致」させることができないことです。

これは私の XML レイアウト ファイルです。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="10dp">

    <ImageView android:id="@+id/image"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:adjustViewBounds="true"
               android:scaleType="fitCenter"
               android:layout_marginTop="10dp"/>

    <TextView android:id="@+id/name"
              android:layout_below="@id/image"
              android:layout_alignLeft="@id/image"
              android:layout_marginTop="20dp"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textSize="18dp"/>

    <TextView android:id="@+id/name2"
              android:layout_below="@id/name"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textSize="14dp"/>


</RelativeLayout>

複数の scaleTypes: 、、fill_parent、の多くの組み合わせを使用しましたが、それらはすべて正しい縦横比で画像を描画しますが、実際に画像と ImageView 自体を拡大するものはありません。画面からはみ出している、ImageView 内の空白、画像が拡大縮小されていない、または画像が切り取られています。wrap_contentfitCenterfitStartfitEndcenterInside

これの正しい組み合わせがよくわかりません。

4

7 に答える 7

116

これらは:

android:layout_height="wrap_content"
android:scaleType="fitStart"
android:adjustViewBounds="true"

画像のサイズを変更し、新しい画像サイズに合わせて境界のサイズを変更する必要があります。デバイスでそれが行われない場合は、使用しているイメージとテストしているデバイスを投稿してください。

于 2012-05-31T02:06:12.450 に答える
49

このコードを使用してください: android:scaleType="fitXY"

画像のスケーリングの詳細については、この記事で説明されている内容を参照してください。

概要:

  • center

画像をビューの中央に配置しますが、スケーリングは実行しません

ここに画像の説明を入力

  • centerCrop

画像の両方の寸法 (幅と高さ) がビューの対応する寸法 (パディングを引いたもの) 以上になるように、画像を均一にスケーリングします (画像の縦横比を維持します)。画像はビューの中央に配置されます

ここに画像の説明を入力

  • centerInside

画像の両方の寸法 (幅と高さ) がビューの対応する寸法 (パディングを差し引いたもの) 以下になるように、画像を均一にスケーリングします (画像の縦横比を維持します)。

ここに画像の説明を入力

  • fitCenter

ここに画像の説明を入力

  • fitEnd

ここに画像の説明を入力

  • fitStart

ここに画像の説明を入力

  • fitXY

ここに画像の説明を入力

  • matrix

描画時にイメージ マトリックスを使用してスケーリングする

ここに画像の説明を入力

詳細はこちら新しい記事

于 2013-12-09T18:13:08.040 に答える
13

このコードをビュー レイアウト パラメーターと共に使用して、wrapcontent として使用します。

android:adjustViewBounds="true"

これがうまくいくことを願っています。

于 2012-07-25T10:20:38.623 に答える
3

プログラムで調整しようとしましたか?TextViews の高さを計算し、これに基づいて画像の高さと幅を調整すると、非常にうまく機能すると思います。

private void adjustImageView()
{
    //Get the display dimensions
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    //TextView name
    TextView name = (TextView) findViewById(R.id.name);
    name.setText("your name text goes here");
    name.measure(0, 0);

    //name TextView height
    int nameH = name.getMeasuredHeight();

    //TextView name2
    TextView name2 = (TextView) findViewById(R.id.name2);
    name2.setText("your name2 text goes here");
    name2.measure(0, 0);

    //name2 TextView height
    int name2H = name2.getMeasuredHeight();

    //Original image
    Bitmap imageOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.image);

    //Width/Height ratio of your image
    float imageOriginalWidthHeightRatio = (float) imageOriginal.getWidth() / (float) imageOriginal.getHeight();

    //Calculate the new width and height of the image to display 
    int imageToShowHeight = metrics.heightPixels - nameH - name2H;
    int imageToShowWidth = (int) (imageOriginalWidthHeightRatio * imageToShowHeight);

    //Adjust the image width and height if bigger than screen
    if(imageToShowWidth > metrics.widthPixels)
    {
        imageToShowWidth = metrics.widthPixels;
        imageToShowHeight = (int) (imageToShowWidth / imageOriginalWidthHeightRatio);
    }

    //Create the new image to be shown using the new dimensions 
    Bitmap imageToShow = Bitmap.createScaledBitmap(imageOriginal, imageToShowWidth, imageToShowHeight, true);

    //Show the image in the ImageView
    ImageView image = (ImageView) findViewById(R.id.image);
    image.setImageBitmap(imageToShow);
}
于 2012-06-05T20:53:30.533 に答える