215.m1-当服务器给的同类型图片大小不一致的时候的适配

按照宽高的比例进行显示,大部分时候同类型的图片宽高是一致的,但有时候存在某一些图片没有按照规则来,导致显示出现了问题,这个时候需要让宽高按照一定的比例来显示,保持一致性

宽度是精确值,高度 = 宽度/比例

如果宽度是一个精确值,也就是宽度是真实值match_parent,模式对应MeasureSpec.EXACTLY

如果宽度不是一个精确值,wrap_contetn,模式会对应MeasureSpec.atmost

item_subject.xml之前的imageview是单独布局在RealtiveLayout里面的现在在外面包裹了一个自定义的FrameLayout布局,然后在定义的FrameLayout里面测量图片,的宽高,让图片的宽高在一定的比例下面显示出来。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    >
<!-- subject的单独的条目 -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="9dip"
        android:layout_marginRight="9dip"
        android:background="@drawable/list_item_bg" >
        <!-- 使用一个自定义的FrameLayout -->
		<com.ldw.market.view.RatioLayout
		    android:id="@+id/rl_layout"
		    android:layout_width="match_parent"
        	android:layout_height="wrap_content"
		    android:padding="5dp">
		    <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.ldw.market.view.RatioLayout>
        

        <TextView
            android:id="@+id/item_txt"
            style="@style/TitleStyle"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@id/rl_layout"
            android:gravity="center_vertical"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:singleLine="true" />
    </RelativeLayout>

</FrameLayout>

RatioLayout.java自定义的FrameLayout.java

package com.ldw.market.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;

/*
 * 按宽高比例显示图片
 */
public class RatioLayout extends FrameLayout{

	//让这个容器按照一定的宽高比例显示
	private float ratio = 2.43f; // 初始化比例值
	public RatioLayout(Context context) {
		super(context);
	}
	
	public RatioLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	
	public RatioLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	
	// 测量当前布局
	@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
		//如果宽度是一个精确值,也就是宽度是真实值match_parent,模式对应MeasureSpec.EXACTLY
		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);
		}

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值