按照宽高的比例进行显示,大部分时候同类型的图片宽高是一致的,但有时候存在某一些图片没有按照规则来,导致显示出现了问题,这个时候需要让宽高按照一定的比例来显示,保持一致性
宽度是精确值,高度 = 宽度/比例
如果宽度是一个精确值,也就是宽度是真实值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);
}
}
本文介绍了一种通过自定义FrameLayout实现图片按特定宽高比显示的方法。该方法能够确保图片在不同尺寸屏幕上的显示效果一致,同时保持图片的原始比例不变。

被折叠的 条评论
为什么被折叠?



