小编典典

将图像放入 ImageView,保持纵横比,然后将 ImageView 调整为图像尺寸?

all

如何将随机大小的图像适合ImageView
什么时候:

  • 初始ImageView尺寸为 250dp * 250dp
  • 图像的较大尺寸应放大/缩小至 250dp
  • 图像应保持其纵横比
  • ImageView缩放后尺寸应与缩放图像的尺寸匹配

例如,对于 100150 的图像,图像和ImageView应该是 166250。
例如,对于 150100 的图像,图像和ImageView应该是 250166。

如果我将界限设置为

<ImageView
    android:id="@+id/picture"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:adjustViewBounds="true" />

图像适合ImageView,但ImageView始终为 250dp * 250dp。


阅读 77

收藏
2022-07-13

共1个答案

小编典典

(在澄清原始问题后,答案被大量修改)

澄清后:
不能仅在 xml 中完成 。无法同时缩放图像和图像,ImageView因此图像的一维始终为
250dp,并且图像的ImageView尺寸与图像相同。

此代码缩放 DrawableImageView保持在 250dp x 250dp 之类的正方形中,一维恰好为 250dp
并保持纵横比。然后ImageView调整大小以匹配缩放图像的尺寸。该代码用于活动。我通过按钮单击处理程序对其进行了测试。

享受。 :)

private void scaleImage(ImageView view) throws NoSuchElementException  {
    // Get bitmap from the the ImageView.
    Bitmap bitmap = null;

    try {
        Drawable drawing = view.getDrawable();
        bitmap = ((BitmapDrawable) drawing).getBitmap();
    } catch (NullPointerException e) {
        throw new NoSuchElementException("No drawable on given view");
    } catch (ClassCastException e) {
        // Check bitmap is Ion drawable
        bitmap = Ion.with(view).getBitmap();
    }

    // Get current dimensions AND the desired bounding box
    int width = 0;

    try {
        width = bitmap.getWidth();
    } catch (NullPointerException e) {
        throw new NoSuchElementException("Can't find bitmap on given view/drawable");
    }

    int height = bitmap.getHeight();
    int bounding = dpToPx(250);
    Log.i("Test", "original width = " + Integer.toString(width));
    Log.i("Test", "original height = " + Integer.toString(height));
    Log.i("Test", "bounding = " + Integer.toString(bounding));

    // 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) bounding) / width;
    float yScale = ((float) bounding) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;
    Log.i("Test", "xScale = " + Float.toString(xScale));
    Log.i("Test", "yScale = " + Float.toString(yScale));
    Log.i("Test", "scale = " + Float.toString(scale));

    // 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);
    width = scaledBitmap.getWidth(); // re-use
    height = scaledBitmap.getHeight(); // re-use
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    Log.i("Test", "scaled width = " + Integer.toString(width));
    Log.i("Test", "scaled height = " + Integer.toString(height));

    // 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);

    Log.i("Test", "done");
}

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

的xml代码ImageView

<ImageView a:id="@+id/image_box"
    a:background="#ff0000"
    a:src="@drawable/star"
    a:layout_width="wrap_content"
    a:layout_height="wrap_content"
    a:layout_marginTop="20dp"
    a:layout_gravity="center_horizontal"/>

感谢对缩放代码的讨论:
http ://www.anddev.org/resize_and_rotate_image__-
_example-t621.html

2012 年 11 月 7 日更新:
按照评论中的建议添加了空指针检查

2022-07-13