今天再講一個可以提高開發(fā)效率的插件 SelectorChapek,這個插件的主要作用是可以將一個 drawable 文件夾下面的圖像粘舟,自動生成對應(yīng)的 drawable selector烂翰,當然它的條件是文件名要符合安裝要求的規(guī)范即可叫搁。
命名規(guī)則
插件的安裝方式和上次講到的 ButterKnife 一樣涣达,先搜索然后再安裝污筷。下面主要講一下它的使用方法。
1))按照命名規(guī)則命名圖片
2)右鍵點擊 drawable 文件夾桥氏,選擇 Generate Android Selectors
3)自動生成對應(yīng)的 drawable selector 文件
這個就是生成的 selector 文件
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_launcher_normal" android:state_pressed="false"/>
<item android:drawable="@drawable/ic_launcher_pressed" android:state_pressed="true"/>
</selector>
但是在我的 Studio 中温峭,我發(fā)現(xiàn) drawable 文件夾似乎少了,但是多了幾個 mipmap 文件夾字支,而且如果是在 mipmap 文件夾中使用上面的操作來生成 drawable selector 是沒有效果的诚镰。
那么mipmap文件夾是什么呢?
先引用一下官方介紹吧
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().
然后是應(yīng)用場景
If you know that you are going to draw this bitmap at less than 50% of its original size, you may be able to obtain a higher quality by turning this property on. Note that if the renderer respects this hint it might have to allocate extra memory to hold the mipmap levels for this bitmap.
mipmap 和 drawable 使用起來沒有任何區(qū)別祥款,但是用 mipmap 系統(tǒng)會在縮放上提供一定的性能優(yōu)化。
======================================================
今天看到書友提的問題月杉,使用插件生成時報空指針異常
有知情的書友刃跛,請指點一下解解惑。謝謝苛萎!
=========================================================
今天 @touly 提出了幾個插件桨昙,里面有一個效果和 SelectorChapek 一樣,可以自動生成對應(yīng)的 drawable selector腌歉。
Selector Drawable Generator
加入插件后蛙酪,選中圖片,在彈出框中輸入文件名
即可在 drawable 中生產(chǎn)對應(yīng)的文件
在此要謝謝 @touly在評論中提出這些插件