</pre>在有点时候,我们要要显示的图片与屏幕的尺寸或我们给的布局的尺寸不匹配的时候,如图片的宽度不够,或者是图片的高度不够。我们可以给图片在加上一层布局,在布局上做手脚。<p></p><p>1自定义布局:/** </p><pre name="code" class="java">/**
* @author Administrator
*当显示一组图片时,有某张图片不符合尺寸时,必须使其统一化,使用layout包裹图片 可以定制layout的大小
*
*/
public class ImageViewLayout extends FrameLayout {
float ratio = 2.43f;
public void setRatio(float ratio) {
this.ratio = ratio;
}
public ImageViewLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
float ratio=attrs.getAttributeFloatValue("http://schemas.android.com/apk/res/com.niebiao.googleplay", "ratio", 2.43f);
setRatio(ratio);
}
public ImageViewLayout(Context context, AttributeSet attrs) {
this(context, attrs,0);
// TODO Auto-generated constructor stub
}
public ImageViewLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize=MeasureSpec.getSize(widthMeasureSpec);
int widthMode=MeasureSpec.getMode(widthMeasureSpec);
int width=widthSize-getPaddingLeft()-getPaddingRight();
int heightSize=MeasureSpec.getSize(heightMeasureSpec);
int heightMode=MeasureSpec.getMode(heightMeasureSpec);
int height=heightSize-getPaddingTop()-getPaddingBottom();
if (widthMode==MeasureSpec.EXACTLY&&heightMode!=MeasureSpec.EXACTLY) {
//如果宽度是精确值,高度不是精确值 , 修正高度
height=(int) (width/ratio+0.5f);
}
if (widthMode!=MeasureSpec.EXACTLY&&heightMode==MeasureSpec.EXACTLY) {
width=(int) (width*ratio+0.5f);
}
widthMeasureSpec=MeasureSpec.makeMeasureSpec(width+getPaddingLeft()+getPaddingRight(), MeasureSpec.EXACTLY);
heightMeasureSpec=MeasureSpec.makeMeasureSpec(height+getPaddingTop()+getPaddingBottom(), MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
2 在xml文件中使用:
<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" >
<com.niebiao.googleplay.ui.ImageViewLayout
android:id="@+id/item_imageviewLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
niebiao:ratio="2.43" >
<ImageView
android:id="@+id/item_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_default" />
</com.niebiao.googleplay.ui.ImageViewLayout>
<TextView
android:id="@+id/item_txt"
style="@style/TitleStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@id/item_imageviewLayout"
android:gravity="center_vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:singleLine="true" />
</RelativeLayout>
在自定义的布局里,我们做的最重要的一件事情就是重新定制测量的规则,让布局的尺寸按照我们的想法来测量和分配,修正了我们想要的高度或宽度;
<span style="white-space:pre"> </span>int widthSize=MeasureSpec.getSize(widthMeasureSpec);
int widthMode=MeasureSpec.getMode(widthMeasureSpec);
int width=widthSize-getPaddingLeft()-getPaddingRight(); //获取真正宽度
int heightSize=MeasureSpec.getSize(heightMeasureSpec);
int heightMode=MeasureSpec.getMode(heightMeasureSpec);
int height=heightSize-getPaddingTop()-getPaddingBottom(); //获取真正高度
if (widthMode==MeasureSpec.EXACTLY&&heightMode!=MeasureSpec.EXACTLY) { //修正高度 当然是可以修正的高度或宽度,如果都是定义死的,就没办法修正
//如果宽度是精确值,高度不是精确值 , 修正高度
height=(int) (width/ratio+0.5f);
}
if (widthMode!=MeasureSpec.EXACTLY&&heightMode==MeasureSpec.EXACTLY) { <span style="white-space:pre"> </span> //修正宽度
width=(int) (width*ratio+0.5f);
}
widthMeasureSpec=MeasureSpec.makeMeasureSpec(width+getPaddingLeft()+getPaddingRight(), MeasureSpec.EXACTLY); //让修正的尺寸起作用
heightMeasureSpec=MeasureSpec.makeMeasureSpec(height+getPaddingTop()+getPaddingBottom(), MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec); //执行性规则
</pre><pre name="code" class="java">当然,为了更好的使用我们定义的规则,我们让高度和宽度的比例值可以在xml文件中可以设置,这就要自定义属性:
1 自定义属性:
在values目录里创建attrs.xml文件
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.niebiao.googleplay.ui.ImageViewLayout">
<attr name="ratio" format="float"></attr>
</declare-styleable>
</resources>
在布局文件中使用:
1 加命名空间:
<pre name="code" class="html">xmlns:niebiao="http://schemas.android.com/apk/res/com.niebiao.googleplay" 自定义空间名 :niebiao 空间地址 <pre name="code" class="html">"http://schemas.android.com/apk/res/+包名
2 使用
<pre name="code" class="html"> niebiao:ratio="2.43"
3 在java代码中使用
</pre><pre name="code" class="java"><span style="white-space:pre"> </span>float ratio = 2.43f;
public void setRatio(float ratio) {
this.ratio = ratio;
}
public ImageViewLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
float ratio=attrs.getAttributeFloatValue("http://schemas.android.com/apk/res/com.niebiao.googleplay", "ratio", 2.43f); //命名空间 自定义属性的名字 自定义属性默认值
setRatio(ratio); //使自定义属性起作用
}