首页 > 其他分享 >Andorid图片等比例缩放解决方案示例

Andorid图片等比例缩放解决方案示例

时间:2022-11-04 12:06:46浏览次数:88  
标签:缩放 示例 layoutParams pic height rate Andorid 图片


前言

图片等比例缩放平时经常用到,网上也提供了很多种方式来解决。这里记录自己开发过程中用到的一种等比例缩放场景。

Android imageView adjustViewBounds属性设置,可以支持等比缩放,再加上动态计算图片比例调整宽高比。

1.配置ImageView 的 adjustViewBounds属性为true;
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:visibility="gone" />

android:adjustViewBounds=“true” 调整ImageView的边界,使得ImageView和图片有一样的长宽比例。 如果图片与ImageView组件长宽比例不同。图片也不能完美呈现效果。

​Android:谈谈最被误读的属性adjustViewBounds​

2.动态计算原始图片的宽高比,根据原始图片宽高比作为锚点,太正图片的宽高比例与原始宽高比一致。

规则:
1.当宽高比1:1时 设置宽高为140 :140
2.当图片比例小于3:1,宽度固定,高度等比例缩放;当图片比例大于3:1时。图片的宽度和高度都保持一致,并都以3:1的缩略图尺寸展示。
3.当图片比例小于1:3,宽度固定,高度等比例缩放;当图片比例大于1:3时。图片的宽度和高度都保持一致,并都以1:3的缩略图尺寸展示。

源码如下:

public void setIMG(ChatMessage chatMessage) {
// android:adjustViewBounds="true"
//必须设置MaxHeight MaxWidth 才会有效
//content://media/external/images/media/804383
String thumImg = chatMessage.thumbImg;
int with = chatMessage.imgWidth;
int height = chatMessage.imgHeight;
boolean isRefresh = chatMessage.isRefresh;
ViewGroup.LayoutParams layoutParams = pic.getLayoutParams();
int defaultWith = DpUtils.dip2px(mContext, 140);
int defaultHeith = DpUtils.dip2px(mContext, 140);
float rate = 0;
//兼容未获取到宽高值的情况
if (0 < with && 0 < height) {
rate = with / Float.valueOf(height);
}
if (rate == 1 || rate == 0) {
pic.setMaxWidth(defaultWith);
pic.setMaxHeight(defaultHeith);
layoutParams.width = defaultWith;
layoutParams.height = defaultHeith;
} else if (rate > 1) {
with = defaultWith;
pic.setMaxWidth(defaultWith);
rate = Math.min(3, rate);
pic.setMaxHeight((int) (with / rate));
layoutParams.width = defaultWith;
layoutParams.height = (int) (with / rate);
} else {
height = defaultHeith;
rate = Math.max(1 / 3.0f, rate);
pic.setMaxWidth((int) (height * rate));
pic.setMaxHeight(defaultHeith);
layoutParams.width = (int) (height * rate);
layoutParams.height = defaultHeith;
}
if (isRefresh) {
if (thumImg.startsWith("content://")) {
Glide.with(this)
.load(Uri.parse(thumImg))
.into(pic);
} else {
Glide.with(this)
.load(thumImg)
.into(pic);
}
} else {
chatMessage.isRefresh = true;
}
}

这里只要根据规则写出计算方式。根据计算出来的结果,设置setMaxWidth,setMaxHeight,以及layoutParams.width,layoutParams.height。
效果

Andorid图片等比例缩放解决方案示例_android


总结:adjustViewBounds =true 设置imageView的宽高比例跟随图片比例,动态调整图片缩放比例与原始比例相等就能够缩放图片不失真。


标签:缩放,示例,layoutParams,pic,height,rate,Andorid,图片
From: https://blog.51cto.com/u_15861646/5823424

相关文章

  • Andorid Jetpack Hilt
    前言现代开发语言,低代码,减少开发中模板代码的编写越来越被一线技术开发所提倡,google官方在这方面也下了很大的功夫推出jectpack架构组件,而Hilt依赖注入就是一个减少样板......
  • Linux下grep讲解及用法示例
    关于正则表达式的讲解请看博文:​这里写链接内容​​简介grep(缩写来自GloballysearchaRegularExpressionandPrint)是一种强大的文本搜索工具,它能使用特定模式匹配(包括......
  • [IE编程] 多页面基于IE内核浏览器的代码示例
    有不少人发信问这个问题,我把答案贴在这里:建议参考WTL(WindowsTemplateLibrary)的代码示例工程TabBrowser(在WTL目录/Samples/TabBrowser下面)。该工程演示了如何用W......
  • MyBatis框架:第二章:传统mybatis的hello world 示例
    创建javaProject项目,建库建表插入数据,到数据库中执行添加mybatis的核心jar添加mysql数据库连接驱动添加log4j日记需要的核心jar在config目录下添加log4j.properties......
  • Qt编写音频播放示例(带音频曲线/振幅/传输/录制等)
    一、功能特点自动计算音频振幅,绘制音频振幅曲线和音频数据曲线。支持音频录制,可选音频输入设备、采样频率、通道等参数,Qt5默认保存wav格式,Qt6默认保存mp3格式,Qt6可选wma......
  • Laravel入门与实战示例代码----数据库和Eloquent
    示例8-1数据库默认连接列表'connections'=>['sqlite'=>['driver'=>'sqlite','database'=>database_path('database.sqlite'),'prefix'......
  • BigDecimal保留两位小数点示例详解
     BigDecimal保留两位小数点非常简单,BigDecimal是一个java数据类型,它能有效的保持数据的精度,下面是一个BigDecimal保留两位小数点的用法示例。publicclassMyDemo{pu......
  • 大文件上传如何做断点续传示例
    ​ 总结一下大文件分片上传和断点续传的问题。因为文件过大(比如1G以上),必须要考虑上传过程网络中断的情况。http的网络请求中本身就已经具备了分片上传功能,当传输的文件比......
  • 第一章、Flink wordcount 入门示例
    概述希望通过本示例对flink有一个轮廓性的认识本示例实现效果:flink连接SocketServer,从SockerServer中按行读取数据作为数据输入,将输入的数据根据空格切分、分组、......
  • JS_0069:禁止双指缩放触发浏览器缩放
    1,//禁止双指缩放触发浏览器缩放document.documentElement.addEventListener('touchstart',function(event){if(event.touches.length......