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