android ImageView 宽度设定,高度自适应
来源:互联网 发布:人工智能协会官网 编辑:程序博客网 时间:2024/04/28 18:23
android ImageView 宽度设定,高度自适应
此文章纯属转载记录下来防止以后找不到了
原文链接http://www.cnblogs.com/bcbr/articles/4268276.html
最近碰到一个需求,要求是在不知道图片宽度和高度的情况下,让图片在指定宽度内充满,同时高度自适应,在网络上查找了一下,也有很多解决方法,后来针对自己的应用,选择了一个修改较小的方案,最后证明效果还是蛮不错的,记录在这里,希望能帮助到有同样需求的人。
好了,言归正传
首先,需要给你的ImageView布局加上android:adjustViewBounds="true"
<ImageView android:id="@+id/test_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_launcher" />
然后,在代码里设置ImageView.最大宽度和最大高度,因为adjustViewBounds属性只有在设置了最大高度和最大宽度后才会起作用
int screenWidth = getScreenWidth(this);
ViewGroup.LayoutParams lp = testImage.getLayoutParams();
lp.width = screenWidth;
lp.height = LayoutParams.WRAP_CONTENT;
testImage.setLayoutParams(lp);
testImage.setMaxWidth(screenWidth);
testImage.setMaxHeight(screenWidth * 5); 这里其实可以根据需求而定,我这里测试为最大宽度的5倍
ok,接下来,再按照常规方法加载图片就会得倒预期的效果了,需要的同学可以试试,good luck.
下面是我的应用效果,有想看的可以自己去找下
http://android.myapp.com/myapp/detail.htm?apkName=com.puerlink.igo
- android ImageView 宽度设定,高度自适应
- android ImageView 宽度设定,高度自适应
- android ImageView 宽度设定,高度自适应
- android ImageView 宽度设定,高度自适应
- Android基础UI之ImageView宽度设定,高度自适应
- Android ImageView设置宽度,高度自适应
- Android 自定义imageview图片高度固定大小宽度按比例自适应
- Android 自定义imageview 图片宽度固定大小高度按比例自适应
- Android 自定义imageview 图片高度固定大小宽度按比例自适应
- android imageview中 scaletype 与 宽度固定高度自适应
- 【Android】自定义ImageView高度自适应,宽度成比例缩放
- Android ImageView高度自适应
- ImageView宽度填充屏幕,高度自适应
- ImageView宽度填满屏幕,高度自适应
- 宽度固定,高度自适应的imageview
- ImageView宽度填满屏幕,高度自适应
- ImageView宽度填满屏幕,高度自适应
- Android中ImageView自适应高度
- ConcurrentHashMap 源码分析
- Android 广播
- 76 超级台阶
- QT5.5.0在Windows7(64bit)下 出现QApplication: No such file or directory 问题的解决办法
- Jvav反射机制
- android ImageView 宽度设定,高度自适应
- 安装Git
- hihoCoder - 1122 - 二分图最大匹配之匈牙利算法
- ZooKeeper Distributed模式
- java 中的 ==和equals
- POJ 2586 Y2K Accounting Bug(贪心)
- Android视图状态及重绘流程分析
- 约瑟夫环的C++实现
- GCD 定时器