【问题标题】:how to scale image according to screen resolution?如何根据屏幕分辨率缩放图像?
【发布时间】:2011-05-06 11:19:40
【问题描述】:

朋友们,

我正在使用以下代码来调整图像大小

 private Bitmap resizeImage( final Bitmap image) {
        Bitmap resizedImage = null;
        if(image != null)
        {
        int maxHeight = 80; //actual image height coming from internet
        int maxWidth = 150; //actual image width coming from internet

        int imageHeight = image.getHeight();
        if ( imageHeight > maxHeight )
        imageHeight = maxHeight;
        int imageWidth = (imageHeight*image.getWidth()) / image.getHeight();
        if ( imageWidth > maxWidth ) {
        imageWidth = maxWidth;
        imageHeight = (imageWidth*image.getHeight()) / image.getWidth();
        }
        resizedImage = Bitmap.createScaledBitmap( image, imageWidth, imageHeight, true);
        }
        return resizedImage;
        }

来自互联网。 现在它在高分辨率屏幕上工作正常,但在小屏幕上它没有任何人指导我应该如何根据屏幕分辨率显示图像?

【问题讨论】:

    标签: android image-scaling


    【解决方案1】:
    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();
    

    所以当你hv屏幕宽度n高度时,你可以根据屏幕分辨率显示图像。

    【讨论】:

    • 这可能是你的问题,但是问这个问题的人已经知道下一步该做什么。这里你去 resizedImage = Bitmap.createScaledBitmap(image, imageWidth, imageHeight, true);
    • getWidth() 和 getHeight() 现在已弃用(API 13+)。使用以下内容:Point size = new Point(); display.getSize(size); int screenWidth = size.x; int screenHeight = size.y;
    【解决方案2】:

    创建三种类型的drawable文件夹drawable-hdpi、drawable-mdpi、drawable-ldpi。将具有相同名称的图像的差异分辨率放在这些文件夹中。应用程序将映射自身。另一种方法是创建 9-patch 图像。 9 补丁图像根据分辨率自行调整。而且您在创建布局时必须小心。您必须正确设置布局。屏幕高度和宽度是设置图像在屏幕的哪个部分所必需的。

    谢谢 迪帕克

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 2016-07-27
      • 2016-04-02
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多