将图像放入图像视图中,保持高宽比,然后将图像视图调整为图像尺寸?如何将随机大小的图像拟合到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 回答
![?](http://img1.sycdn.imooc.com/545869510001a20b02200220-100-100.jpg)
三国纷争
TA贡献1804条经验 获得超7个赞
maxWidth
android:scaleType="centerInside" android:adjustViewBounds="true"
![?](http://img1.sycdn.imooc.com/545862e700016daa02200220-100-100.jpg)
交互式爱情
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)
好好享受吧。
- 3 回答
- 0 关注
- 406 浏览
添加回答
举报
0/150
提交
取消