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

问题

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

解决方案

GlideApp.with(mContext)
                        .asBitmap()
                        .load(item.getContent())
                        .error(R.drawable.default_bg)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(new SimpleTarget() {
                            @Override
                            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition 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)