缩放图像以完全适合屏幕

时间:2015-06-04 09:24:51

标签: android scale

我正在使用HorizontalScrollView并且我想在其中放置图像,但图像将比屏幕大,并且当我使用setScaleType(ImageView.ScaleType.CENTER_CROP)或其他属性时,用户可以水平滚动。 如何让图像完全适合屏幕?如果可能的话,我更喜欢使用XML属性。

activity_main.xml中

<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/main"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal">

</LinearLayout>

MainActivity.java

ImageView image = new ImageView(this);
image.setImageResource(getResources().getIdentifier('file_name', "drawable", getPackageName()));
image.setAdjustViewBounds(true);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
image.setLayoutParams(layoutParams);
parent.addView(image);

1 个答案:

答案 0 :(得分:1)

您可以将此xml属性用于ImageView,

android:scaleType="fitXY"

或者使用此动态代码段来相应地调整ImageView的屏幕比例

private void scaleImage(ImageView view, int boundBoxInDp)
{
    // 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();

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) boundBoxInDp) / width;
    float yScale = ((float) boundBoxInDp) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Create a new bitmap and convert it to a format understood by the ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    width = scaledBitmap.getWidth();
    height = scaledBitmap.getHeight();

    // Apply the scaled bitmap
    view.setImageDrawable(result);

    // Now change ImageView's dimensions to match the scaled image
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}

private int dpToPx(int dp)
{
    float density = getApplicationContext().getResources().getDisplayMetrics().density;
    return Math.round((float)dp * density);
}

用于测试的示例代码:

ImageView image = new ImageView(this);
scaleImage(image , 250); // in dp

这是输出,

前,

enter image description here

后,

enter image description here

来源:Scale image into ImageView, then resize ImageView to match the image