Android中的图片资源包括扩展名为.png、.jpg、.gif等的图片,
应用图标资源存放在以mipmap开头的文件夹中,界面中使用的图片资源存放在以drawable开头的文件夹中。
根据设备屏幕密度的不同,Android会自动匹配不同文件夹中的图片资源。
密度范围值 | 以mipmap开头的文件夹 | 以drawable开头的文件夹 |
120~160dpi | mipmap_mdpi | drawable_mdpi |
160~240dpi | mipmap_hdpi | drawable_hdpi |
240~320dpi | mipmap_xdpi | drawable_xdpi |
320~480dpi | mipmap_xxdpi | drawable_xxdpi |
480~640dpi | mipmap_xxxdpi | drawable_xxxdpi |
想要调用两种文件夹中的资源文件,调用方式有两种,一种是通过java代码来调用,另一种是在xml布局文件中调用。
(1)通过Java代码调用图片资源
在Activity中可以通过getResources().getDrawable()方法调用图片资源,实例代码如下:
//调用以mipmap开头的文件夹中的资源文件
getResources().getDrawable(R.mipmap.ic_launcher);
//调用以drawable开头的文件夹中的资源文件
getResources().getDrawable(R.drawable.icon);
(2)在XML布局文件中调用图片资源
在XML布局文件中调用图片资源文件的实例代码如下:
//调用以mipmap开头的文件夹中的资源文件
@mipmap/ic_launcher
//调用以drawable开头的文件夹中的资源文件
@drawable/icon