博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SmartImageView
阅读量:6916 次
发布时间:2019-06-27

本文共 1551 字,大约阅读时间需要 5 分钟。

==

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);        }    }}

 

==

转载地址:http://tcacl.baihongyu.com/

你可能感兴趣的文章
1864. [ZJOI2006]三色二叉树【树形DP】
查看>>
js 日期控件laydate使用
查看>>
典型重构6 (多态)
查看>>
C# 环境
查看>>
基于高德地图Windows Phone API 快速开发地图相关APP(一)
查看>>
Ecshop模板在首页调用指定分类的热销、推荐、新品商品
查看>>
ZOJ - 3987 - Numbers (大数 + 贪心)
查看>>
highcharts延迟加载及刷新数据
查看>>
Chapter 10. WinForm-DataGridView(确认删除、多条件查询、数据区别显示)
查看>>
浏览器中实现3D全景浏览
查看>>
linux下zip文件解压乱码的问题
查看>>
2018-2019-1 20165231 《信息安全系统设计基础》第六周学习总结
查看>>
我是一只IT小小鸟读后感
查看>>
rename 修改文件名
查看>>
列表元素对应及列表操作
查看>>
OAF 多语言的实现
查看>>
基于VML/SVG配电站接线系统
查看>>
Map services, SDE and locks
查看>>
centos7下LVM挂载和扩容
查看>>
BZOJ 2729 高精度+组合数学
查看>>