安卓drawable和mipmap

在[http://www.imooc.com/qadetail/70235]里提到了根据谷歌官方的内容,于是自己查了一下。

在第④点中说明了

mipmap用于放启动图标,

drawable用于放各种资源,

和网上好多人纠正的一样,记录一下。

①在“Android 4.3 APIs”(https://developer.android.com/about/versions/android-4.3.html)

写到

Mipmapping for drawables
Using a mipmap as the source for your bitmap or drawable is a simple way to provide a quality image and various image scales, which can be particularly useful if you expect your image to be scaled during an animation.


Android 4.2 (API level 17) added support for mipmaps in the Bitmap class—Android swaps the mip images in your Bitmap when you've supplied a mipmap source and have enabled setHasMipMap(). Now in Android 4.3, you can enable mipmaps for a BitmapDrawable object as well, by providing a mipmap asset and setting the android:mipMap attribute in a bitmap resource file or by calling hasMipMap().


②在“Add Bitmap Images with Image Asset Studio”(https://developer.android.com/studio/write/image-asset-studio.html#mipmap)

Managing Launcher Icons as mipmap Resources
Different home screen launcher apps on different devices show app launcher icons at various resolutions. When app resource optimization techniques remove resources for unused screen densities, launcher icons can wind up looking fuzzy because the launcher app has to upscale a lower-resolution icon for display. To avoid these display issues, apps should use the mipmap/ resource folders for launcher icons. The Android system preserves these resources regardless of density stripping, and ensures that launcher apps can pick icons with the best resolution for display.


Make sure launcher apps show a high-resolution icon for your app by moving all densities of your launcher icons to density-specific res/mipmap/ folders (for example res/mipmap-mdpi/ and res/mipmap-xxxhdpi/). The mipmap/ folders replace the drawable/ folders for launcher icons. For xxhpdi launcher icons, be sure to add the higher resolution xxxhdpi versions of the icons to enhance the visual experience of the icons on higher resolution devices.


Note: Even if you build a single APK for all devices, it is still best practice to move your launcher icons to the mipmap/ folders.

③在“Using configuration qualifiers”(https://developer.android.com/guide/practices/screens_support.html#qualifiers)

For example, the following application resource directories provide different layout designs for different screen sizes and different drawables. Use the mipmap/ folders for launcher icons.


res/layout/my_layout.xml              // layout for normal screen size ("default")
res/layout-large/my_layout.xml        // layout for large screen size
res/layout-xlarge/my_layout.xml       // layout for extra-large screen size
res/layout-xlarge-land/my_layout.xml  // layout for extra-large in landscape orientation


res/drawable-mdpi/graphic.png         // bitmap for medium-density
res/drawable-hdpi/graphic.png         // bitmap for high-density
res/drawable-xhdpi/graphic.png        // bitmap for extra-high-density
res/drawable-xxhdpi/graphic.png       // bitmap for extra-extra-high-density


res/mipmap-mdpi/my_icon.png         // launcher icon for medium-density
res/mipmap-hdpi/my_icon.png         // launcher icon for high-density
res/mipmap-xhdpi/my_icon.png        // launcher icon for extra-high-density
res/mipmap-xxhdpi/my_icon.png       // launcher icon for extra-extra-high-density
res/mipmap-xxxhdpi/my_icon.png      // launcher icon for extra-extra-extra-high-density
For more information about how to use alternative resources and a complete list of configuration qualifiers (not just for screen configurations), see Providing Alternative Resources.

④在“Providing Resources”(https://developer.android.com/guide/topics/resources/providing-resources.html#ResourceTypes)

As you can see in this example, the res/ directory contains all the resources (in subdirectories): an image resource, two layout resources, mipmap/ directories for launcher icons, and a string resource file. The resource directory names are important and are described in table 1.

drawable/
Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource subtypes:
Bitmap files
Nine-Patches (re-sizable bitmaps)
State lists
Shapes
Animation drawables
Other drawables
See Drawable Resources.




mipmap/
Drawable files for different launcher icon densities. For more information on managing launcher icons with mipmap/ folders, see Managing Projects Overview.


安卓开发中,我们可以通过代码实现将图片下载到mipmap中。 首先,我们需要创建一个AsyncTask类用于在后台下载图片,该类需要三个泛型参数,分别是参数类型,进度类型和结果类型。 接着,在AsyncTask类的doInBackground方法中,我们可以使用HttpURLConnection或OkHttpClient等网络库发送网络请求,将图片下载到本地。 最后,我们可以在AsyncTask类的onPostExecute方法中获取下载的结果,并将图片保存到mipmap中。 具体步骤如下: 1. 创建一个AsyncTask类,用于在后台下载图片。 ``` class DownloadTask extends AsyncTask<String, Void, Bitmap> { @Override protected Bitmap doInBackground(String... urls) { String imageUrl = urls[0]; Bitmap bitmap = null; try { URL url = new URL(imageUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.connect(); InputStream inputStream = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } @Override protected void onPostExecute(Bitmap result) { if(result != null){ addToMipmap(result); } } } ``` 2. 在AsyncTask类的doInBackground方法中,使用HttpURLConnection或OkHttpClient等网络库发送网络请求,将图片下载到本地。 ``` URL url = new URL(imageUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.connect(); InputStream inputStream = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream); inputStream.close(); ``` 3. 在AsyncTask类的onPostExecute方法中,获取下载的结果,并将图片保存到mipmap中。 ``` private void addToMipmap(Bitmap bitmap) { try { Resources res = getResources(); String imageName = "downloaded_image"; int drawableId = res.getIdentifier(imageName, "mipmap", getPackageName()); Drawable drawable = new BitmapDrawable(res, bitmap); ((BitmapDrawable) drawable).setGravity(Gravity.CENTER); if (drawableId != 0) { res.getDrawable(drawableId); } } catch (Exception e) { e.printStackTrace(); } } ``` 在addToMipmap方法中,我们通过调用getResources方法获取资源对象,使用getIdentifier方法获取mipmap中的资源ID,然后将下载的图片转化成Drawable对象,并将其保存到mipmap中。 最后,在Activity中,我们可以调用DownloadTask类的execute方法,启动图片下载任务。 ``` new DownloadTask().execute(imageUrl); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值