自定义图片比例适配控件 ProportionImageView
来源:互联网 发布:sql server报价 编辑:程序博客网 时间:2024/05/17 01:43
这个控件实现起来很简单,来源是我的项目经理辉哥 他的博客地址 地址 交流群 546270670 欢迎大家
直接上代码
/** * * ============================================================ * * project name : TiantianFangFu * * copyright ZENG HUI (c) 2015 * * author : HUI * * version : 1.0 * * date created : On June, 2015 * * description : 自己设置view宽高比(一般会知道宽), 不需要去适配了 * * revision history : * * ============================================================ * */public class ProportionImageView extends ImageView {// 用float可能更好private float mWidthPro;private float mHeightPro;public ProportionImageView(Context context) {this(context, null);}public ProportionImageView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ProportionImageView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initAttribute(context, attrs);}private void initAttribute(Context context, AttributeSet attrs) {TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.ProportionImageView);mWidthPro = array.getFloat(R.styleable.ProportionImageView_width_proportion, mWidthPro);mHeightPro = array.getFloat(R.styleable.ProportionImageView_height_proportion, mHeightPro);array.recycle();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// 先测量一下super.onMeasure(widthMeasureSpec, heightMeasureSpec);// 用户没有设置宽高比就不处理if (mWidthPro == 0 || mHeightPro == 0) {return;}// 拿到宽int width = getMeasuredWidth();int height = (int) (width * (mHeightPro / mWidthPro));// 宽和高按比例设置setMeasuredDimension(width, height);}}
<!-- 宽高比的图片 -->
<declare-styleable name="ProportionImageView">
<!-- 宽度比例 float -->
<attr name="width_proportion" format="float" />
<!-- 高度比例 float -->
<attr name="height_proportion" format="float" />
</declare-styleable>
<declare-styleable name="ProportionImageView">
<!-- 宽度比例 float -->
<attr name="width_proportion" format="float" />
<!-- 高度比例 float -->
<attr name="height_proportion" format="float" />
</declare-styleable>
使用
<com.hc.baibianjiajia.ui.ProportionImageView android:id="@+id/note_iv" android:layout_width="match_parent" android:layout_height="wrap_content" hui:width_proportion="2" hui:height_proportion="1" android:scaleType="centerCrop" android:layout_marginBottom="10dp" />
hui:width_proportion="2" 宽 2 hui:height_proportion="1" 高 1
0 0
- 自定义图片比例适配控件 ProportionImageView
- Android自定义控件:图片比例适配,解决图片白边(详解View中onMeasure方法)
- 图片适配自定义控件
- Android自定义控件:imageview重写onMeasure方法实现图片按指定比例显示,拉伸永不变形,解决屏幕适配问题
- android 自定义比例裁剪图片(拍照 相册)方式 自定义比例
- 自定义控件:等比例显示控件RatioLayout
- 自定义控件之图片适配RatioLayout
- 图片适配 压缩比例 (纯代码)
- 自定义宽高比例的布局控件
- 自定义控件(一) 一个比例饼状图
- 自定义控件【按照宽高比例显示】
- 控件自适应图片长宽比例
- 图片剪裁——自定义比例
- android自定义可任意调节比例的控件
- Android自定义控件之矩形View(长宽比例限制)
- 自定义控件--图片按钮
- 自定义圆形图片控件
- 自定义圆形图片控件
- mysql学习之连接查询
- 1006. Tree Traversals - Hard Version (35)解题报告
- 程序员面试金典——解题总结: 9.17中等难题 17.1编写一个函数,不用临时变量,直接交换两个数
- r语言中时间函数处理
- 面向对象
- 自定义图片比例适配控件 ProportionImageView
- 第三章——构建模块
- 仪表盘和图形编辑器 Grafana
- 安装WinXp时遇到蓝屏(BAD_POLL_CALLER)
- 递归问题深入到堆栈小结
- 自己写的一个超简单的伪登录。。。
- Unity&Shader案例篇—声纳光波效果
- STM32 AD参考及参照电压
- B-tree的构造及相关操作