web-dev-qa-db-ja.com

Android=

ImageViewのサイズ全体を占めるように画像を拡大したいと思います。これは、scaleType=fit_centerを使用する場合とは微妙に異なります。これは、画像のアスペクト比がImageViewのアスペクト比と完全に一致しない場合、 fit_center が画像の周りにバンドを残すためです。代わりに、画像を中央に配置して拡大し、周囲のビューを完全に埋めて、余分な部分を切り取りたいと思います。

OnCreate()で独自のカスタム画像マトリックスを計算することでこれを実現できます。

final Display display = getWindowManager().getDefaultDisplay();
final float screenWidth = display.getWidth();
final float screenHeight = display.getHeight();
final float imageWidth = splashView.getDrawable().getIntrinsicWidth();
final float imageHeight = splashView.getDrawable().getIntrinsicHeight();
final Matrix splashMatrix = new Matrix();
final float scale = Math.max(screenHeight/imageHeight,screenWidth/imageWidth);
splashMatrix.postScale( scale, scale );
splashView.setImageMatrix(splashMatrix);

これは正常に機能しますが、もっと簡単な方法があるはずです。画像のアスペクト比を維持し、ImageViewを完全に塗りつぶしながら、ImageViewで画像を拡大する方法を知っている人はいますか?

(注:私の場合、ImageViewは全画面を表示しているため、getWindowManager().getDisplay()を使用して目的の画像サイズを見つけます。splashView.getWidth()/getHeight()は使用できません。ビューはまだレイアウトされておらず、サイズもありません)

60
emmby

Android:scaleType="centerCrop"を使用できます。アスペクト比を維持し、イメージを希望どおりにスケーリングします。

詳細については、以下のリンクをご覧ください

http://developer.Android.com/reference/Android/widget/ImageView.html#attr_Android:scaleType

150
roplacebo
4
Kumar Bibek

場合によっては、必要なのはすべて

Android:adjustViewBounds="true"
2
Barbara K