温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Android中利用ImageView.src对图片进行拉伸处理

发布时间:2020-12-08 15:46:46 来源:亿速云 阅读:186 作者:Leah 栏目:移动开发

如何在Android中利用ImageView.src对图片进行拉伸处理?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

方法如下:

<LinearLayout 
 android:id="@+id/linearLayout1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:orientation="vertical" > 
 
<ImageView 
 android:layout_width="wrap_content" 
 android:layout_height="205dp" 
 android:scaleType="centerInside" 
 android:background="@drawable/feature_guide_1" > 
</ImageView> 
</LinearLayout> 
<LinearLayout 
 android:id="@+id/linearLayout1" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:orientation="vertical" > 
 
 <ImageView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_gravity="center" 
  android:adjustViewBounds="true" 
  android:scaleType="fitXY" 
  android:src="@drawable/feature_guide_0" > 
 </ImageView> 
</LinearLayout> 

以下为参考内容:

最近碰到一个需求,要求是在不知道图片宽度和高度的情况下,让图片在指定宽度内充满,同时高度自适应,在网络上查找了一下,也有很多解决方法,后来针对自己的应用,选择了一个修改较小的方案,最后证明效果还是蛮不错的,记录在这里,希望能帮助到有同样需求的人。

首先,需要给你的ImageView布局加上Android:adjustViewBounds="true"

<ImageView android:id="@+id/test_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_launcher" />

然后,在代码里设置ImageView.最大宽度和最大高度,因为adjustViewBounds属性只有在设置了最大高度和最大宽度后才会起作用

int screenWidth = getScreenWidth(this);
ViewGroup.LayoutParams lp = testImage.getLayoutParams();
lp.width = screenWidth;
lp.height = LayoutParams.WRAP_CONTENT;
testImage.setLayoutParams(lp);
testImage.setMaxWidth(screenWidth);
testImage.setMaxHeight(screenWidth * 5); 

关于如何在Android中利用ImageView.src对图片进行拉伸处理问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI