recyclerview实现瀑布流图片自适应高度

在这里插入图片描述
第一种方法在适配器的item布局中imageview控件下添加属性就可实现自适应高度

android:adjustViewBounds="true"

第二种自定义imageview实现
1::Recyclerview配置适配器

  StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
       CircleAdapter mCircleAdapter = new CircleAdapter(getActivity());
        recyclerview.setLayoutManager(gridLayoutManager);
        recyclerview.setAdapter(mCircleAdapter);

2:自定义view实现高度自适应

/**
 * date:2020/9/8 0008
 * author:wsm (Administrator)
 * funcation:实现图片高度自适应
 */
public class ResizableImageView extends ImageView {

    public ResizableImageView(Context context) {
        super(context);
    }

    public ResizableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        Drawable d = getDrawable();

        if(d!=null){
            // ceil not round - avoid thin vertical gaps along the left/right edges
            int width = MeasureSpec.getSize(widthMeasureSpec);
            //高度根据使得图片的宽度充满屏幕计算而得
            int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
            setMeasuredDimension(width, height);
        }else{
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

}

3:布局使用

<?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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

     <。。。.view.ResizableImageView
        android:id="@+id/list_img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY" />
 </LinearLayout>

Glide.with(mActivity).load(mList.get(position).getImgs()).into(holder.mRecomdimg);

图片实现圆角

效果就实现了

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值