【问题标题】:How to fit png/jpg images on any screen without decreasing quality?如何在不降低质量的情况下在任何屏幕上调整 png/jpg 图像?
【发布时间】:2026-02-18 09:20:03
【问题描述】:

我知道这有点棘手,并且仍在思考是否可能,但是当我使用 vector Drawable 时,我想让我的图像在不降低任何 Android 设备上的图像质量的情况下进行调整非常方便,但有时向量的大小不是内存效率,所以我不想使用它们。虽然我想知道是否有任何方法可以调整简单的 PNGJPEG 文件与 Android 中的分辨率和屏幕尺寸无关?

如果有人能给我方法,那将是很大的帮助!!

【问题讨论】:

  • 我想知道您是否可以使用作为 ImageView 属性提供的 scaleType 来完成您的工作。有多种 scaleType 可用,例如 center、centerCrop、centerInside、fitCenter、fitEnd、fitStart、fitXY、matrix。
  • 建议使用PNG文件,查看*.com/a/37207973/2826147
  • 好的,我已经尝试过了,但是通过制作这个文件夹并放置不同的文件并不是真正的解决方案@AmitVaghela
  • 这将解决您的这个问题

标签: android image imageview android-imageview


【解决方案1】:

使用调整图像视图(自定义图像视图)

  public class ResizableImageView extends ImageView {
    public ResizableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ResizableImageView(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable d = getDrawable();
        // get drawable from imageview
        if (d == null) {
            super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
            return;
        }

        int imageHeight = d.getIntrinsicHeight();
        int imageWidth = d.getIntrinsicWidth();
        // get height and width of the drawable
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        // get width and height extracts the size from the supplied measure specification.
        float imageRatio = 0.0F;
        if (imageHeight > 0) {
            imageRatio = imageWidth / imageHeight;
        }
        float sizeRatio = 0.0F;
        if (heightSize > 0) {
            sizeRatio = widthSize / heightSize;
        }

        int width;
        int height;
        if (imageRatio >= sizeRatio) {
            // set width to maximum allowed
            width = widthSize;
            // scale height
            height = width * imageHeight / imageWidth;
        } else {
            // set height to maximum allowed
            height = heightSize;
            // scale width
            width = height * imageWidth / imageHeight;
        }

        setMeasuredDimension(width, height);
        // This method must be called to store the measured width and measured height. Failing to do so will trigger an exception at measurement time
    }
}

【讨论】:

  • 您确定它适用于所有设备和分辨率的所有密度吗?您可以通过将 cmets 放入其中使其更易于理解吗?请这将是很大的帮助@ChiragArora
  • 请查看,如果您想了解更多,请查看以下链接ryadel.com/en/…
  • 好的,非常有帮助@Chirag
最近更新 更多