自定义按比例显示的ImageView

本文介绍了如何创建一个自定义的ImageView,使其能根据服务端提供的图片比例进行显示。首先,从服务端获取图片比例,然后通过计算宽高比来调整视图尺寸。接着,详细说明了创建构造方法、布局中使用自定义属性以及在attrs.xml中定义自定义属性的过程。在onMeasure方法中,根据宽度计算出高度,并确保测量出精确的高度值,以实现按比例的显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,实现效果

  • 按比例显示的ImageView  

 

 

2,实现逻辑

【1】去看服务端给你的图片比例是多少,

  • 宽度/高度   计算宽高比。

  • 高:宽 = 1:2.43    

【1】创建构造方法

public class RatioImageView extends ImageView {}

【2】布局中使用

  <com.heima.googlemarket.ui.view.RatioImageView

            android:id="@+id/iv_image"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

【3】创建自定义属性

  • Values  创attrs.xml文件

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <declare-styleable name="RatioImageView">

        <attr name="ratio" format="float"/>

    </declare-styleable>

</resources>
  • 控件布局中使用

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:orientation="vertical"

    android:padding = "5dp"

    android:layout_width="match_parent"

    android:layout_height="wrap_content">

        <com.xiaoshuai.googlemarket.ui.view.RatioImageView

            android:id="@+id/iv_image"

            app:ratio="2.43"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

       

</LinearLayout>

 

int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  •  

//参数1命名空间,参数2自定义属性的属性名,参数3默认值

ratio = attrs.getAttributeFloatValue(NAMESPACE, "ratio", 0.0f);
  • 在onMeasure获取宽度,他一定是一个具体的值,match_parent+5dp是父布局的match_parent,父布局的match_parent 

 

  • 宽度除除以比例等到高度的精确值

int heightSize = (int)(widthSize/ratio+0.5f);
  • 生成具体的32位数高度值进行测量

    

 heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize,MeasureSpec.EXACTLY);

 

【3】全部代码

/**

* Created by HASEE on 2019/4/6.

* 让此控件的宽高和服务器端返回图片的宽高比例一致

* 高:宽 = 1:2.43

*/

public class RatioImageView extends ImageView {

    private static final String TAG = "RatioImageView";

    public static String NAMESPACE = "http://schemas.android.com/apk/res-auto";

    //从自定义属性中获取到的属性值

    private final float ratio;

    public RatioImageView(Context context, AttributeSet attrs) {

        super(context, attrs);

        ratio = attrs.getAttributeFloatValue(NAMESPACE, "ratio", 0.0f);

        Log.i(TAG,"======================ratio = "+ratio);

    }





    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        //widthMeasureSpec  32位2机制数字    2位   30位

        //00 01 10 11  4种

        //30位         指定宽度大小

        //MeasureSpec.AT_MOST       至多

        //MeasureSpec.EXACTLY       精确值

        //MeasureSpec.UNSPECIFIED   ListView





        //1.获取自定义控件的宽度具体值

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        //2.获取高度值精确值

        int heightSize = (int)(widthSize/ratio+0.5f);

        //3.用精确模式搭配精确值,生成高度的32位数

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize,MeasureSpec.EXACTLY);





        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兴帅_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值