来自MipMap文件夹的.PNG图像不能在平板电脑上缩放

时间:2016-02-10 20:18:46

标签: android android-studio png mipmaps

我最近看到Google现在允许mipmap文件夹中的引用而不是常规图像资源的drawable。根据我的理解,mipmap文件夹以前只用于图标,但现在能够渲染高质量的图像。

我有一个简历创建者应用程序,我的简历示例是.PNG文件。我正在尝试确保文件在相应的平板电脑尺寸上正确显示。我已将图像文件分别添加到mipmap-hdpi,mipmap-mdpi,mipmap-xhdpi,mipmap-xxhdpi和mipmap-xxxhdpi文件夹中。不幸的是,这些图像可以在较小的手机和7英寸平板电脑上进行扩展,但我似乎无法在10英寸平板电脑上进行扩展。以下是7英寸平板电脑和10英寸平板电脑的渲染截图:

7“平板电脑:

enter image description here

10“平板电脑(Google Nexus 10仿真器):

enter image description here

我还包含了我的文件夹:

enter image description here

2 个答案:

答案 0 :(得分:1)

图像无法自动缩放。你必须告诉他们。

但最好的答案更多是关于屏幕尺寸和像素密度。如果您查看“drawables”文件夹,还有一系列其他文件夹,全部以大,中,小,xlarge等命名。您应该为每个适当的屏幕大小设置一个图像。设备将选择自动使用的最佳设备。

这意味着如果您的1024px平板电脑上的图像看起来不错。您应该在800px的较小文件夹中具有相同的图像。等等...

答案 1 :(得分:1)

在阅读了与durbnpoisn的讨论后,我发现它与我有同样的问题。有时您的应用程序无法在可绘制文件夹中获得正确的图像。为此,您可以通过编程方式执行此操作。以下是示例代码:

DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float widthDpi = metrics.xdpi;
float heightDpi = metrics.ydpi;
float widthInches = widthPixels / widthDpi;
float heightInches = heightPixels / heightDpi;
double diagonalInches = Math.sqrt((widthInches * widthInches) + (heightInches * heightInches)); // this code returns the actual screen size (eg. 7,8,9,10 inches in double format ex: 7.932189832)
diagonalInches = (double) Math.round(diagonalInches * 10) / 10; // round up to first decimal places



if (diagonalInches >= 7 && diagonalInches <=8){
    //set image here
} else {
    //set image here
}