Android ImageView: 适配宽度

我下载图像,并设置为屏幕背景动态使用 Imageview。我已经尝试了 ScaleType,以缩放图像。

如果图像高度大于宽度,则 ScaleTypes fitStartfitEndfitCenter不工作。Android 将照片缩小,并根据高度进行调整,但我看到一些额外的空白空间作为宽度的一部分。

I want to scale down the photo based on the width so that it fits the width and I don't care if there's some extra blank space as part of the height or if height is too long it is fine if it's going out of the view(if that's possible?).

ScaleType.XY缩放照片,并适合在 ImageView的一切,并不关心图像的高度/重量比例。

<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitStart"
/>
103356 次浏览

I think you cant do it only with XML, you need to resize yourself, the bitmap

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();


try {
((ImageView) findViewById(R.id.background))
.setImageBitmap(ShrinkBitmap(width));
} catch (FileNotFoundException e) {
e.printStackTrace();
}

then

private Bitmap ShrinkBitmap(int width)
throws FileNotFoundException {


BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.img, bmpFactoryOptions);
int widthRatio = (int) android.util.FloatMath
.ceil(bmpFactoryOptions.outWidth / (float) width);




bmpFactoryOptions.inSampleSize = widthRatio;






if (bmpFactoryOptions.inSampleSize <= 0)
bmpFactoryOptions.inSampleSize = 0;
bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.img, bmpFactoryOptions);
return bitmap;


}

And the layout

 <ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

This elegant solution found here will work like a charm for you.

Basically you just have to create a small class that extends ImageView and simply override the onMeasure method to adjust the width and height as you want. Here it scales to fit width by using:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
setMeasuredDimension(width, height);
}

You would use this special ImageView like this:

<your.activity.package.AspectRatioImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:src="@drawable/test" />

I ended up using this code:

<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/name"
/>

Make sure you set the image using src instead of background.

There is a simple method if you simply want to fill the width of screen and have height proportional (and know the ratio of the image) you can do this in OnCreate():

setContentView(R.layout.truppview_activity);
trupImage = (ImageView) findViewById(R.id.trupImage);


Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float scWidth = outMetrics.widthPixels;
trupImage.getLayoutParams().width = (int) scWidth;
trupImage.getLayoutParams().height = (int) (scWidth * 0.6f);

Where 0.6 is the ratio adjustment (you could also calc automatically of course if you know the w & h of image).

PS:
Here is the XML side:

  <ImageView
android:id="@+id/trupImage"
android:layout_width="match_parent"
android:background="@color/orange"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />

android:adjustViewBounds="true" does the job!

This works for me.

Create a class extends ImageView

    package com.yourdomain.utils;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;


public class ResizableImageView extends ImageView {


public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();


if (d != null) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}

In xml use the following instead of ImageView

    <com.yourdomain.utils.ResizableImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/test" />

if you want to fit the image whole parent block
it will stretch the image

        android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"

And if you want to fit image whole parent with original ratio of image
it will crop out some part of image

        android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"