Android 仿微信朋友圈不同數量圖片排列方式

前段時間做了不同數量的圖片展示,給大家分享一下,先上個效果圖:

                    

代碼比較簡單,相信大家都看得懂:
 

public class DynamicImageAdapter extends CommonAdapter<String> {
    private int itemWidth;
    private int itemHeight;
    private boolean isOne;

    /**
     * 這是recyclerview中嵌套的recyclerview的adapter,每次都會重新 new Adapter
     *
     * @param context
     * @param layoutId
     * @param datas
     */
    public DynamicImageAdapter(Context context, int layoutId, List<String> datas) {
        super(context, layoutId, datas);
        this.originImageUrl = originImageUrl;
        if (getItemCount() == 2 || getItemCount() == 4) {
            itemWidth = DeviceUtil.getScreenWidth(context) * 3 / 8;
            itemHeight = itemWidth;
            isOne = false;
        } else if (getItemCount() > 2) {
            itemWidth = ViewGroup.LayoutParams.MATCH_PARENT;
            itemHeight = ViewGroup.LayoutParams.MATCH_PARENT;
            isOne = false;
        } else {
            itemWidth = DeviceUtil.getScreenWidth(mContext) * 3 / 5;
            itemHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
            isOne = true;
        }
    }

    @Override
    protected void convert(ViewHolder holder, String url, int position) {
        ImageView imageView = holder.getView(R.id.iv_image);
        MSquareRelativeLayout mSquareRelativeLayout = holder.getView(R.id.square_layout);
        if (isOne) {
            mSquareRelativeLayout.setWap(true);
            imageView.setAdjustViewBounds(true);
            Glide.with(mContext)
                    .asBitmap()
                    .load(Config.COS_PIC_PREFIX + url)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            boolean isLong = false;
                            float scale = resource.getWidth() * 1.0f / resource.getHeight();
                            if (scale <= 0.4) {//超長圖
                                itemWidth = DeviceUtil.getScreenWidth(mContext) * 1 / 5;
                                isLong = true;
                            } else if (scale > 0.4 && scale < 1) { //標準長方形
                                itemWidth = DeviceUtil.getScreenWidth(mContext) * 2 / 5;
                            } else if (scale == 1) {//標準正方形
                                itemWidth = DeviceUtil.getScreenWidth(mContext) * 3 / 5;
                            } else if (scale > 1 && scale < 3) {//標準寬圖
                                itemWidth = DeviceUtil.getScreenWidth(mContext) * 3 / 5;
                            } else if (scale >= 3) {//超寬圖
                                itemWidth = DeviceUtil.getScreenWidth(mContext) * 4 / 5;
                            }
                            display(imageView, holder.getView(R.id.iv_img_tab), resource, isLong);
                        }
                    });
        } else {
            mSquareRelativeLayout.setWap(false);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setAdjustViewBounds(false);
            int targetMaxSize = DeviceUtil.getScreenWidth(mContext) * 3 / 8;
            Glide.with(mContext)
                    .asBitmap()
                    .load(Config.COS_PIC_PREFIX + url)
                    .into(new SimpleTarget<Bitmap>(targetMaxSize, targetMaxSize) {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            boolean isLong = false;
                            float scale = resource.getWidth() * 1.0f / resource.getHeight();
                            if (scale <= 0.4) {//超長圖
                                isLong = true;
                            }
                            display(imageView, holder.getView(R.id.iv_img_tab), resource,  isLong);
                        }
                    });
        }

    }

    private void display(ImageView imageView, ImageView ivImgTab, Bitmap resource,  boolean isLong) {
        ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
        layoutParams.width = itemWidth;
        layoutParams.height = itemHeight;
        imageView.setImageBitmap(resource);
        if (isLong) {
            ivImgTab.setVisibility(View.VISIBLE);
            ImageHelp.load(mContext, ivImgTab, R.drawable.ic_img_tab_long);
        } else {
            ivImgTab.setVisibility(View.GONE);
        }
    }
}

 

MSquareRelativeLayout其實就是一個寬高相等的RelativeLayout:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (isWap) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        } else {
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        }
    }

有不解之處,大家可以私信我哦~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章