Android自定义控件(四) 自定义ImageView动态设置ImageView的高度
来源:互联网 发布:51模拟器官网mac 编辑:程序博客网 时间:2024/05/16 09:25
我们经常会遇到动态设置ImageView的高度的情况,今天给大家分享下怎么使用自定义ImageView去动态设置ImageView的高度
1.自定义ImageView动态设置ImageView的高度(高度比以自定义属性方式给出)
<span style="font-family:FangSong_GB2312;">package com.example.imageview.ui.view;import android.content.Context;import android.util.AttributeSet;import android.widget.ImageView;/** * 功能:能够根据一个指定的宽高比(ratio)和自己的宽度,动态设置自己的高度 * @author smartbetter * */public class RatioImageView extends ImageView{private float ratio = 0f;//宽高比public RatioImageView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public RatioImageView(Context context, AttributeSet attrs) {super(context, attrs);//获取自定义属性的值,赋值ratioratio = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res/com.example.imageview", "ratio", 0f);}public RatioImageView(Context context) {super(context);}/** * 设置ImageView的宽高比 * @param ratio */public void setRatio(float ratio){this.ratio = ratio;}/** * onMeasure是measure方法引起的回调,而measure方法是父VIew在测量子View会调用子的View的measure方法 * 所以widthMeasureSpec(宽度测量规则)和heightMeasureSpec(高度测量规则)是父VIew在调用子View的measure方法时计算好的 * MeasureSpec: 测量规则类,由size和mode2个因素组成: * size: 就是指定的大小值 * mode: MeasureSpec.AT_MOST : 对应的是warp_content; * MeasureSpec.EXACTLY : 对应的是具体的dp值,match_parent * MeasureSpec.UNSPECIFIED: 未定义的,一般用adapter的view的测量中 */@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//1.从widthMeasureSpec中反向获取父VIew计算好的sizeint width = MeasureSpec.getSize(widthMeasureSpec);//LogUtil.e(this, "width: "+width);//2.根据宽高比和width,计算出对应的heightif(ratio!=0){float height = width/ratio;//3.重新组建heightMeasureSpec,传递给super.onMeasureheightMeasureSpec = MeasureSpec.makeMeasureSpec((int) height,MeasureSpec.EXACTLY);}super.onMeasure(widthMeasureSpec, heightMeasureSpec);}}</span>
2.给自定义ImageView增加自定义属性
自定义属性在“android自定义控件(一) 自定义组合控件”中已经使用过,在此顺便复习下
(1)在\res\values\attrs.xml中声明自定义属性
<span style="font-family:FangSong_GB2312;"><?xml version="1.0" encoding="utf-8"?><resources> <!-- 声明RatioImageView的属性 --> <declare-styleable name="RatioImageView"> <attr name="ratio" format="float"></attr> <!-- float : 数据类型 --> </declare-styleable> </resources></span>
(2)在布局文件\res\layout\activity_main.xml中使用
首先声明命名空间
<span style="font-family:FangSong_GB2312;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:example="http://schemas.android.com/apk/res/com.example.imageview" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" ></span>
然后就可以使用自定义属性了
<span style="font-family:FangSong_GB2312;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:example="http://schemas.android.com/apk/res/com.example.imageview" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <!-- example:ratio="2.5" 是不能加f的 --> <com.example.imageview.ui.view.RatioImageView android:layout_width="match_parent" android:id="@+id/iv_image" android:scaleType="fitXY" example:ratio="2.5" android:layout_height="180dp"/></LinearLayout></span>
16 1
- Android自定义控件(四) 自定义ImageView动态设置ImageView的高度
- android中自定义ImageView的高度自适应
- android自定义带倒影的ImageView控件
- Android自定义控件-不同形状的ImageView
- android 自定义圆形imageview控件
- 自定义类似ImageView的控件
- 自定义控件 GifImageView 可以播放gif动态图的ImageView
- 自定义ImageView放大镜控件
- 自定义圆形ImageView控件
- 自定义ImageView控件
- 自定义控件------圆形Imageview
- 自定义ImageView控件
- 【组合控件】android自定义控件之带文字的ImageView
- 动态设置ImageView的宽度和高度
- android 自定义圆形的ImageView
- 自定义动态设置check图片的ImageView取代checkBox
- Android自定义控件之圆形进度条ImageView
- Android自定义控件——圆形ImageView
- H.264中的P-Skip宏块和B-Skip宏块简介
- 用Java实现各种排序(2)---希尔排序
- HDU Buildings
- 整理php防注入和XSS攻击通用过滤
- Abstract class VS Interface2
- Android自定义控件(四) 自定义ImageView动态设置ImageView的高度
- Mybatis 返回主键
- 利用shell脚本操作mysql数据库
- Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签
- 头文件的包含顺序
- Python在线笔试琐碎
- 类模板的定义及其使用
- Spring事务管理
- 简单就是美——由模式谈面向对象的基本原则之单一职责原则