美文网首页自定义控件
Android RecycerView 中根据图片大小自适应控件

Android RecycerView 中根据图片大小自适应控件

作者: 小于先森 | 来源:发表于2018-11-15 18:57 被阅读21次

问题

recyclerView中 item有ImageView,ImageView大小根据图片大小而改变大小

解决方案

GlideApp.with(mContext)
                        .asBitmap()
                        .load(item.getContent())
                        .error(R.drawable.default_bg)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                int widht = resource.getWidth();
                                int height = resource.getHeight();
                                if(widht>pic_max_width){
                                    float multiple = ((float) widht)/pic_max_width+0.5f;
                                    widht = (int) (widht/multiple);
                                    height = (int) (height/multiple);
                                }
                                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) img.getLayoutParams();
                                params.width = widht;
                                params.height = height;
                                img.setLayoutParams(params);
                                img.setImageBitmap(resource);
                            }
                        });

pic_max_width:

pic_max_width = DisplayUtils.getScreenWidth(this)-DisplayUtils.dip2px(this,50);

问题解决 50是ImageView的左右margin

相关文章

网友评论

    本文标题:Android RecycerView 中根据图片大小自适应控件

    本文链接:https://www.haomeiwen.com/subject/yxhufqtx.html