简单实现ImageView宽度填满屏幕,高度自适应的两种方式
2016-01-15 15:52
721 查看
两种方式
1.重写View的onMeasure方法参考这里easion_zms的专栏
核心代码
2.设置ImageView的属性:
//宽度填满屏幕
android:layout_width=”match_parent”
android:scaleType=”fitXY”
android:layout_height=”wrap_content”
//保持比例,一定要设置
android:adjustViewBounds=”true”
1.重写View的onMeasure方法参考这里easion_zms的专栏
核心代码
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ Drawable d = getDrawable(); if(d!=null){ // ceil not round - avoid thin vertical gaps along the left/right edges int width = MeasureSpec.getSize(widthMeasureSpec); //高度根据使得图片的宽度充满屏幕计算而得 int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth()); setMeasuredDimension(width, height); }else{ super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }
2.设置ImageView的属性:
//宽度填满屏幕
android:layout_width=”match_parent”
android:scaleType=”fitXY”
android:layout_height=”wrap_content”
//保持比例,一定要设置
android:adjustViewBounds=”true”
<ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="fitXY" />
相关文章推荐
- java 数组讲解
- 闲思
- 静坐常思己过
- block与使用变量
- 封装jquery适配seajs模式
- C#线程同步
- CentOS 6编译安装yum和配置常用的yum源
- Delphi:ADOConnection连接SQLServer自动断网问题解决
- webdriver+python 对三大浏览器的支持
- OpenStack Nova hacking和读书笔记
- Android Studio——android中handler用法总结
- JS的奇怪问题
- CodeForces 366A Dima and Guards
- Dynamics CRM 2016 Series: Overview
- 雅虎股票接口
- spring 定时任务的 执行时间设置规则
- mysql 导入、导出sql
- centos设置开机启动
- hdu1814Peaceful Commission【2-SAT】输出最小解
- 初探Backbone