SmartImageView
2024-10-19 06:26:58
==
public class SmartImageView extends ImageView {
public SmartImageView(Context context)
{
super(context);
} public SmartImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
} public SmartImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
Drawable drawable = getDrawable(); if (drawable == null) {
setMeasuredDimension(0, 0);
}
else {
float imageRatio = (float)drawable.getIntrinsicWidth() / drawable.getIntrinsicHeight();
float viewRatio = (float)MeasureSpec.getSize(widthMeasureSpec) / MeasureSpec.getSize(heightMeasureSpec); // Image is wider than the display (ratio)
if (imageRatio >= viewRatio) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) (width / imageRatio);
setMeasuredDimension(width, height);
}
// Image is taller than the display (ratio)
else {
int height = MeasureSpec.getSize(heightMeasureSpec);
int width = (int)(height * imageRatio);
setMeasuredDimension(width, height);
}
}
} catch (Exception e) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
==
最新文章
- 关于git的简单实用命令
- sqlalchemy
- C# 调用Adodb对Access数据库执行批量插入
- PAT 1002. 写出这个数 (20)
- BestCoder8 1001.Summary(hdu 4989) 解题报告
- MYSQL INSERT INTO SELECT 不插入重复数据
- __block存储类型
- 初来乍到--------smarty
- win10十周年更新后cent os 虚拟机无法连接到xshell
- android中的 Toast 和 AlertDialog
- 【MYSQL】解决Mysql直接登录问题(删除匿名用户)(转)
- CVE-2016-10190 FFmpeg Http协议 heap buffer overflow漏洞分析及利用
- String 经常用法最优算法实现总结 (二)
- java中解决小数精度问题
- SCSS 使用@each 方法循环遍历数组颜色并给li赋值
- .net core 使用NPOI填充Word模板导出Word
- CGI-- FASTCGI
- c++ primer读书笔记之c++11(三)
- (转)Terraform,自动化配置与编排必备利器
- java代码块执行顺序