Method 1 Use wrap_content horizontally or vertically
and then use android:scaleType="fitCenter" to make it fully displayed
keep square
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;
}
});
The method that just fills the screen
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);