recycleView使用之2 :横向现实图片之图片是正方形

来源:互联网 发布:被掩埋的巨人 知乎 编辑:程序博客网 时间:2024/05/21 21:44

今天大神要求,recycleView的横向显示三张图片需要显示的是正方形的,因为前边已经贴出代码关于recyview的正常使用。

解决问题的猜想1 : 在现实的图片条目上想方法:将图片本身的宽高做成适配屏幕宽高,这样就不会因为屏幕的问题出现显示不一样的情况。

首先,就是自定义控件的onMeasure()方法的使用,这个方法可以定义控件的宽高等属性的。

import android.content.Context;import android.util.AttributeSet;import android.widget.RelativeLayout;public class MyRelativeView extends RelativeLayout {    public MyRelativeView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public MyRelativeView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyRelativeView(Context context) {        super(context);    }    @SuppressWarnings("unused")    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));        int childWidthSize = getMeasuredWidth();        int childHeightSize = getMeasuredHeight();        heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    }}

用他做根节点,里面放一个ImagView

<?xml version="1.0" encoding="utf-8"?><com.omi.utils.MyRelativeView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@drawable/dialogshape">    <ImageView        android:id="@+id/image_compile_recycle"        android:layout_width="match_parent"        android:layout_height="match_parent" /></com.omi.utils.MyRelativeView>
 加载图片的时候,整个图片是正方形了,可是如果图片本身就是不规则的或者就是长方形的,怎么办? 

继续猜想,拉伸整个item条目?不能,整个图片宽高已经是填充满item的屏幕了。


 参考了一个前辈的方法:

http://blog.csdn.net/chen825919148/article/details/8845889

里面很详细:

Android:scaleType="CENTER"  按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示

                                   CENTER_CROP  按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽) 

                                   CENTER_INSIDE  将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽 

                                   FIT_CENTER     把图片按比例扩大/缩小到View的宽度,居中显示

                                   IT_START, FIT_END在图片缩放效果上与FIT_CENTER一样,只是显示的位置不同,FIT_START是置于顶部,FIT_CENTER居                                    中,FIT_END置于底部。

                                   FIT_XY             不按比例缩放图片,目标是把图片塞满整个View。


0 0
原创粉丝点击