aandroid prevents the bitmap in the picture from being stretched and makes the imageview self-adaptive, and fills the screen

Method 1 Use wrap_content horizontally or vertically
and then use android:scaleType="fitCenter" to make it fully displayed

image.png

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);

Guess you like

Origin blog.csdn.net/u010042660/article/details/131052714