首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将图像放入ImageView,保持纵横比,然后调整ImageView大小以适应图像尺寸?

将图像放入ImageView,保持纵横比,然后调整ImageView大小以适应图像尺寸?
EN

Stack Overflow用户
提问于 2011-11-23 03:29:56
回答 17查看 346.7K关注 0票数 187

如何使任意大小的图像适合ImageView

在以下情况下:

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

例如,对于100*150的图像,图像和ImageView应为166*250。

例如,对于150*100的图像,图像和ImageView应为250*166。

如果我将边界设置为

代码语言:javascript
复制
<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。

EN

回答 17

Stack Overflow用户

回答已采纳

发布于 2011-11-23 04:06:55

(在澄清了原问题后,对答案进行了大量修改)

澄清后:

这个不能只在xml的中完成。不可能同时缩放图像和ImageView,这样图像的一个维度将始终是250dp,而ImageView将具有与图像相同的维度。

此代码将 Drawable of a ImageView缩放为一个正方形,如250dp x 250dp,其中一个维度恰好为250dp,并保持纵横比不变。然后调整ImageView的大小以匹配缩放图像的尺寸。代码在活动中使用。我通过按钮点击处理程序进行了测试。

好好享受吧。:)

代码语言:javascript
复制
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);
}

ImageView的xml代码

代码语言:javascript
复制
<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

更新7,2012年11月:

添加了注释中建议的空指针检查

票数 147
EN

Stack Overflow用户

发布于 2013-10-11 19:15:58

可能不是这个特定问题的答案,但是如果有人,像我一样,正在寻找如何在保持宽高比的同时将图像以有限的大小(例如,maxWidth)适合于ImageView,然后摆脱ImageView占用的过多空间,那么最简单的解决方案就是在XML中使用以下属性:

代码语言:javascript
复制
    android:scaleType="centerInside"
    android:adjustViewBounds="true"
票数 281
EN

Stack Overflow用户

发布于 2013-12-10 15:15:47

代码语言:javascript
复制
<ImageView android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:scaleType="centerCrop"
           android:adjustViewBounds="true"/>
票数 48
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8232608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档