Android(安卓)RecycerView 中根据图片大小自适应控件大小的实现
16lz
2021-01-24
问题
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<? 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(安卓)Studio Error:Connection timed out: connect.解决
- 解决Error:Android(安卓)Dex: com.android.dex.DexIndexOverflow
- 【Android笔记】Android统计文件夹大小,删除文件夹下的所有文件方
- Android开发常见异常和解决方案(二)
- 使用bitmap缩略图,解决图片大小超过预算的VM的问题
- android按屏幕大小动态确定控件位置及大小
- Android(安卓)Studio编译使用了Maven的工程卡住的解决方案
- Android(安卓)DownloadManager 使用
- Android界面加载完成后自动弹出软键盘的解决方案