【Android】自定义ImageView高度自适应,宽度成比例缩放
来源:互联网 发布:mac加密文件夹弹出 编辑:程序博客网 时间:2024/04/27 21:28
/** * 高度自适应ImageView,高度始终充满显示区域,宽度成比例缩放 */public class AutoHeightImageView extends ImageView { public AutoHeightImageView(Context context) { super(context); } public AutoHeightImageView(Context context, AttributeSet attrs) { super(context, attrs); } public AutoHeightImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Drawable drawable = getDrawable(); if(drawable != null){ int width = drawable.getMinimumWidth(); int height = drawable.getMinimumHeight(); float scale = (float)width/height; //强制根据图片原有比例,重新计算ImageView显示区域宽度 int heightMeasure = MeasureSpec.getSize(heightMeasureSpec); int widthMeasure = (int)(heightMeasure * scale); //并设置为MeasureSpec.EXACTLY精确模式保证之后的super.onMeasure()不再调整 widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthMeasure, MeasureSpec.EXACTLY); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); }}
相反地,如果是宽度自适应,高度成比例缩放的话,使用以下代码:
/** * 宽度自适应ImageView,宽度始终充满显示区域,高度成比例缩放 */public class AutoScaleWidthImageView extends ImageView { public AutoScaleWidthImageView (Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Drawable drawable = getDrawable(); if(drawable != null){ int width = drawable.getMinimumWidth(); int height = drawable.getMinimumHeight(); float scale = (float)height/width; int widthMeasure = MeasureSpec.getSize(widthMeasureSpec); int heightMeasure = (int)(widthMeasure*scale); heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightMeasure, MeasureSpec.EXACTLY); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); }}
阅读全文
1 0
- 【Android】自定义ImageView高度自适应,宽度成比例缩放
- Android 自定义imageview图片高度固定大小宽度按比例自适应
- Android 自定义imageview 图片宽度固定大小高度按比例自适应
- Android 自定义imageview 图片高度固定大小宽度按比例自适应
- android ImageView 宽度设定,高度自适应
- android ImageView 宽度设定,高度自适应
- android ImageView 宽度设定,高度自适应
- android ImageView 宽度设定,高度自适应
- Android ImageView设置宽度,高度自适应
- android创建固定宽度,高度的图片,比例缩放
- android中自定义ImageView的高度自适应
- ImageView 按固定宽度等比例缩放
- 可以设置宽高比,宽度确定,高度比例缩放的ImageView快速实现
- Android 设置ImageView宽度固定,其高度按比例缩放适应
- Android 设置ImageView宽度固定,其高度按比例缩放适应
- Android 设置ImageView宽度固定,其高度按比例缩放适应
- Android基础UI之ImageView宽度设定,高度自适应
- android imageview中 scaletype 与 宽度固定高度自适应
- 编译器中和64位编程有关的预定义宏
- R语言 堆叠可视化/可视化对比分析简单学习案例(R语言&大数据分析qq群 456726635 欢迎讨论交流)
- oracle语句执行顺序
- 观察者模式
- unity3d中用incontrol检测输入设备切换
- 【Android】自定义ImageView高度自适应,宽度成比例缩放
- DES加密解密
- ==与equals的区别
- 并查集简单总结
- 每天一个linux命令目录 开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每天一个linux命令。学习的主要参考资料为: 1.《鸟哥的linux私房菜》 2.
- 2017-05-17
- Java equals() 、hashCode()浅析
- java利用itext编辑pdf
- 浮动文字水印效果