ImageView で画像を拡大縮小してアスペクト比を維持する方法
-
22-09-2019 - |
質問
Android では、 ImageView
さんの layout_width
することが fill_parent
(電話機の幅全体を占めます)。
私が貼った画像であれば、 ImageView
よりも大きいです layout_width
, Androidはスケールしますよね?しかし、高さはどうでしょうか?Android が画像を拡大縮小するとき、アスペクト比は維持されますか?
私が発見したのは、上部と下部に空白があることです。 ImageView
Android が画像を拡大縮小するとき、 ImageView
. 。本当?「はい」の場合、どうすればその空白を削除できますか?
解決
はい、デフォルトでは、Android はアスペクト比を維持しながら、ImageView に合わせて画像を縮小します。ただし、次を使用して画像を ImageView に設定していることを確認してください。
android:src="..."
それよりもandroid:background="..."
.src=
アスペクト比を維持しながら画像を拡大縮小しますが、background=
スケールを大きくします そして 画像を変形して、ImageView のサイズに正確に合わせます。(ただし、背景とソースを同時に使用することもできます。これは、1 つの ImageView だけを使用して、メイン画像の周囲にフレームを表示する場合などに便利です。)こちらもご覧ください
android:adjustViewBounds
スケール変更された画像に合わせて ImageView 自体のサイズを変更します。たとえば、通常は正方形の ImageView に長方形の画像がある場合、adjustViewBounds=true を指定すると、ImageView のサイズも長方形に変更されます。これは、他のビューが ImageView の周りにどのようにレイアウトされるかに影響します。次に、Samuh が書いているように、画像をデフォルトでスケールする方法を変更できます。
android:scaleType
パラメータ。ちなみに、これがどのように機能するかを知る最も簡単な方法は、自分で少し実験してみることです。Eclipse のプレビューは通常間違っているため、エミュレーター自体 (または実際の携帯電話) でレイアウトを確認することを忘れないでください。
他のヒント
を参照してください。 android:adjustViewBounds
するます。
を設定し、この本当のあなたはImageViewのは、その描画可能の縦横比を維持するためにその境界を調整したい場合。
。あなたはImageView
の幅を持ちたいというView
(または他のfill_parent
)を持っており、高さは比例してスケーリングします:
あなたのImageView
にこれらの2つの属性を追加します:
android:adjustViewBounds="true"
android:scaleType="centerCrop"
そしてImageView
にfill_parent
と高さにwrap_content
幅を設定します。
また、あなたはあなたのイメージをクロップしたくない場合は、これを試してください:
android:adjustViewBounds="true"
android:layout_centerInParent="true"
あなたはあなたのXMLにこれを追加し、適切なアスペクト比を維持しながら、両方のスケールアップとダウンというImageView
たい場合:
android:adjustViewBounds="true"
android:scaleType="fitCenter"
あなたのコードにこれを追加します。
// We need to adjust the height if the width of the bitmap is
// smaller than the view width, otherwise the image will be boxed.
final double viewWidthToBitmapWidthRatio = (double)image.getWidth() / (double)bitmap.getWidth();
image.getLayoutParams().height = (int) (bitmap.getHeight() * viewWidthToBitmapWidthRatio);
これは、この作業を取得するために私にしばらく時間がかかったが、場合によっては仕事に、この表示され、両方の場所の画像が画面の幅よりも小さく、画面幅よりも大きく、それは画像をボックスません。
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="39dip"
android:scaleType="centerCrop"
android:adjustViewBounds ="true"
Bitmap bitmapImage = BitmapFactory.decodeFile("Your path");
int nh = (int) ( bitmapImage.getHeight() * (512.0 / bitmapImage.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(bitmapImage, 512, nh, true);
your_imageview.setImageBitmap(scaled);
これは私の問題を解決しました。
android:adjustViewBounds="true"
android:scaleType="fitXY"
を制御するために ImageView.ScaleType のを見て、サイズ変更がImageView
で起こる方法を理解しています。 (アスペクト比を維持しながら)、画像のサイズが変更されたとき、チャンスは画像の高さまたは幅のいずれかがImageView
の寸法よりも小さくなることです。
アスペクト比を維持するImageViewの中にこれらのプロパティを使用します。
android:adjustViewBounds="true"
android:scaleType="fitXY"
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
私は、画面より小さい画像を持っています。それが最大に比例して伸びて、私は次のコードを使用していたビューの中央に持ってするには:
<ImageView
android:id="@+id/my_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitCenter" />
あなたは相対的なレイアウトを持っているとImageViewの上または下になるように設定いくつかの要素を持っている場合、彼らが最も可能性の高い画像が重なっされることを持っている心の中にかかわらず、。
正確な適切なスケーリングとImageViewのノートリミング使用に合うように画像を望んでいるあなたの人のための
imageView.setScaleType(ScaleType.FIT_XY);
ImageViewのは、あなたのImageViewのを表す図である。
あなたは、画面の幅を計算することができます。そして、あなたはビットマップを拡張することができます。
public static float getScreenWidth(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float pxWidth = outMetrics.widthPixels;
return pxWidth;
}
<時間>
の計算画面の幅によって画面幅とスケーリングされた画像の高さ。
float screenWidth=getScreenWidth(act)
float newHeight = screenWidth;
if (bitmap.getWidth() != 0 && bitmap.getHeight() != 0) {
newHeight = (screenWidth * bitmap.getHeight()) / bitmap.getWidth();
}
あなたはビットマップを拡張することができた後。
Bitmap scaledBitmap=Bitmap.createScaledBitmap(bitmap, (int) screenWidth, (int) newHeight, true);
プログラムでこれを行う場合は、正しい順序でセッターを呼び出してくださいます:
imageView.setAdjustViewBounds(true)
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP)
画質が低下した場合には: 使用
android:adjustViewBounds="true"
の代わりに
android:adjustViewBounds="true"
android:scaleType="fitXY"
あなたがしたい場合は、あなたのイメージが可能な最大のスペースを占有し、その後最良の選択肢は以下のようになります。
android:layout_weight="1"
android:scaleType="fitCenter"
これは、それがConstraintLayoutの内側に私のために働いていた方法です
<ImageView
android:id="@+id/myImg"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"/>
は、コードで、私は描画可能な設定しました
ImageView imgView = findViewById(R.id.myImg);
imgView.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.image_to_show, null));
これはうまくそのアスペクト比に応じて中央でそれを維持画像に適合する。
ImageViewのためandroid:layout_gravity
を使用してみてください
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
私のために働いた上記の例ています。
私は、そのアスペクト比を維持し、BESTFITにコンテナのサイズをビットマップをスケーリングするアルゴリズムを有します。 の
ここhref="https://stackoverflow.com/questions/12503184/android-image-scaling-to-support-multiple-resolutions/22631351#22631351"> 希望これは車線ダウン誰かを助けます!ヨ任意のJavaコードを必要としません。あなただけにしています:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
キーは幅と高さの一致親である
私はこれを使用します:
<ImageView
android:id="@+id/logo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="centerInside"
android:src="@drawable/logo" />
cardview
for丸めimageview
を用いた場合と固定android:layout_height
にヘッダーにこれがGlide
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="220dp"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
card_view:cardBackgroundColor="@color/colorPrimary"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="10dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true">
<ImageView
android:adjustViewBounds="true"
android:maxHeight="220dp"
android:id="@+id/iv_full"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"/>
</android.support.v7.widget.CardView>
</FrameLayout>
また、あなたのイメージのサイズを小さくします画像を拡大縮小することができます。 あなたはそれをダウンロードして使用することができ、それのためのライブラリがあります。 https://github.com/niraj124124/Images-Files-scale-そして、compress.git の
どのように使用します 1)圧縮機v1.0のインポート。あなたのプロジェクトにjarファイル。 2)試験にサンプルコードの下に入れます。 ResizeLimiterリサイズ= ImageResizer.build()。 resize.scale( "inputImagePath"、 "outputImagePath"、imageWidth、imageHeight)。 より多くの方法は、
あなたの条件に応じてありますあなたのImageViewのをパスし、画面の高さに基づいて、あなたが作ることができる幅も
public void setScaleImage(EventAssetValueListenerView view){
// Get the ImageView and its bitmap
Drawable drawing = view.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float xScale = ((float) 4) / width;
float yScale = ((float) 4) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
width = scaledBitmap.getWidth();
height = scaledBitmap.getHeight();
view.setImageDrawable(result);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
myImageView.setAdjustViewBounds(true);
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 130, 110, false));