将图像放入图像视图中,保持高宽比,然后将图像视图调整为图像尺寸?

将图像放入图像视图中,保持高宽比,然后将图像视图调整为图像尺寸?

如何将随机大小的图像拟合到ImageView?
当:

  • 最初

    ImageView

    尺寸为250 dp*250 dp
  • 图像的更大尺寸应该向上/向下缩放到250 dp。
  • 图像应保持高宽比。
  • 这个

    ImageView

    缩放后尺寸应与缩放后的图像尺寸相匹配

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

如果我把界限设为

<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总是250 dp*250 dp。



梵蒂冈之花
浏览 407回答 3
3回答

三国纷争

可能不是这个特定问题的答案,但如果有人像我一样,正在寻找答案-如何在ImageView中匹配有界大小的图像(例如,maxWidth)在保持高宽比并消除ImageView占用的过多空间的同时,最简单的解决方案是在XML中使用以下属性:&nbsp;&nbsp;&nbsp;&nbsp;android:scaleType="centerInside" &nbsp;&nbsp;&nbsp;&nbsp;android:adjustViewBounds="true"

交互式爱情

下面的代码使位图完全符合相同大小的图像视图。获取位图图像的高度和宽度,然后借助图像视图的参数计算新的高度和宽度。这给了你所需要的图像与最佳的纵横比。int currentBitmapWidth = bitMap.getWidth();int currentBitmapHeight = bitMap.getHeight();int ivWidth = imageView.getWidth();int ivHeight = imageView.getHeight();int newWidth = ivWidth;newHeight = (int) Math.floor((double) currentBitmapHeight *( (double) new_width / (double) currentBitmapWidth));Bitmap newbitMap = Bitmap.createScaledBitmap(bitMap, newWidth, newHeight, true);imageView.setImageBitmap(newbitMap)好好享受吧。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android