简书链接:aandroid让图片内的bitmap不会被拉伸又让imageview自适应以及铺满屏幕
文章字数:108,阅读全文大约需要1分钟
方法1 水平或者垂直 使用wrap_content
然后使用 android:scaleType=”fitCenter”让其完整显示

image.png

保持正方形

1
2
3
4
5
6
7
8
9
10
11
12
13
getBinding().ivPrintInfo.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
int height = getBinding().ivPrintInfo.getHeight(); // 保持比例
ViewGroup.LayoutParams params = getBinding().ivPrintInfo.getLayoutParams();
params.width = height;
getBinding().ivPrintInfo.setLayoutParams(params);
// 移除监听器,避免重复触发
getBinding().ivPrintInfo.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
});

刚好铺满屏幕的方法

1
2
3
4
5
6
7
8
9
binding.ivPreviewContainer.setVisibility(View.VISIBLE);
PrintLabelModel printLabelModel = list.get(0);
binding.ivZoom.setLayoutParams(new FrameLayout.LayoutParams(printLabelModel.imgWidth, printLabelModel.imgHeight, Gravity.CENTER));
int widthPixels = SuperContext.getInstance().getResources().getDisplayMetrics().widthPixels;
int scale = widthPixels / printLabelModel.imgWidth;
binding.ivZoom.setImageDrawable(binding.ivPrintInfo.getDrawable());
binding.ivZoom.setScaleX(scale);
binding.ivZoom.setScaleY(scale);