前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >13.按比例显示图片、自定义属性、测量

13.按比例显示图片、自定义属性、测量

作者头像
六月的雨
发布2018-05-14 12:14:56
9480
发布2018-05-14 12:14:56
举报

有时候服务器返回的图片有可能宽高是不一样的,所以需要按照一定宽高比例去显示,修改专题界面

自定义属性

<resources>
 <declare-styleable name="com.itheima.googleplay.view.RatioLayout">
 <attr  name="ratio"  format="float"></attr>
 </declare-styleable>
</resources>

RatioLayout

public class RatioLayout extends FrameLayout {
 // 按照宽高比例去显示
 private float ratio = 2.43f; // 比例值,不要去写死,这样只需要调下方法根据自己需要去修改
 public void setRatio(float ratio) {
 this.ratio = ratio;
 }
 public RatioLayout(Context context) {
 super(context);
 }
 public RatioLayout(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 // 参数1 命名控件 参数2 属性的名字 参数3 默认的值
 float ratio = attrs.getAttributeFloatValue(
 "http://schemas.android.com/apk/res/com.itheima.googleplay",
 "ratio", 2.43f);
		setRatio(ratio);
 }
 public RatioLayout(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
 }
 // 测量当前布局
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 // widthMeasureSpec 宽度的规则 包含了两部分 模式 值
 int widthMode = MeasureSpec.getMode(widthMeasureSpec); // 模式
 int widthSize = MeasureSpec.getSize(widthMeasureSpec);// 宽度大小
 int width = widthSize - getPaddingLeft() - getPaddingRight();// 去掉左右两边的padding
 int heightMode = MeasureSpec.getMode(heightMeasureSpec); // 模式
 int heightSize = MeasureSpec.getSize(heightMeasureSpec);// 高度大小
 int height = heightSize - getPaddingTop() - getPaddingBottom();// 去掉上下两边的padding
 if (widthMode == MeasureSpec.EXACTLY
 && heightMode != MeasureSpec.EXACTLY) {
 // 修正一下 高度的值 让高度=宽度/比例
			height = (int) (width / ratio + 0.5f); // 保证4舍五入
 } else if (widthMode != MeasureSpec.EXACTLY
 && heightMode == MeasureSpec.EXACTLY) {
 // 由于高度是精确的值 ,宽度随着高度的变化而变化
			width = (int) ((height * ratio) + 0.5f);
 }
 // 重新制作了新的规则
		widthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY,
				width + getPaddingLeft() + getPaddingRight());
		heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY,
				height + getPaddingTop() + getPaddingBottom());
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 }
}

布局:把imageview包裹住就可以不用修改imageview了,它根据它的父容器去显示

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.googleplay"
 android:layout_width="match_parent"
android:layout_height="match_parent">
 <com.itheima.googleplay.view.RatioLayout
            android:id="@+id/rl_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp" 
            itheima:ratio="2.43">
 <ImageView
                android:id="@+id/item_icon"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_default" />
 </com.itheima.googleplay.view.RatioLayout>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-11-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档