为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

梵蒂冈之花 2019-07-27 15:15:11
将图像放入图像视图中,保持高宽比,然后将图像视图调整为图像尺寸?如何将随机大小的图像拟合到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。
查看完整描述

3 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

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

    android:scaleType="centerInside"
    android:adjustViewBounds="true"


查看完整回答
反对 回复 2019-07-28
?
交互式爱情

TA贡献1712条经验 获得超3个赞

下面的代码使位图完全符合相同大小的图像视图。获取位图图像的高度和宽度,然后借助图像视图的参数计算新的高度和宽度。这给了你所需要的图像与最佳的纵横比。


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)

好好享受吧。




查看完整回答
反对 回复 2019-07-28
  • 3 回答
  • 0 关注
  • 406 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信