自適應(yīng)圖標主要用于在Launcher上可以根據(jù)不同的配置顯示不同形狀的圖標,可以顯示圓形方形等形狀。
Adaptive Icons介紹
對應(yīng)Adaptive Icons的介紹google開發(fā)者和各路翻譯過來的網(wǎng)址很多,這里貼下兩個網(wǎng)址僅供參考。
官方地址
翻譯地址
主要說明應(yīng)用適配Adaptive Icons的注意點和方式。
1.當應(yīng)用targetsdk>=26,adaptive icon就會自動生效,即使資源中并沒有指定為adaptive icon,但實際上使用adaptive icon,圖片資源是要重新修改的,如果不改,雖然自適應(yīng)會生效,但效果可能不好。
如何讓應(yīng)用的圖標效果更好呢?
定義一個xml作為drawable
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
background是背景圖片,foreground是前景圖片
也可以這樣:
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_contacts_launcher_background"/>
<foreground android:drawable="@mipmap/ic_contacts_launcher_foreground"/>
</adaptive-icon>
background可以使用color定義。
2.如果應(yīng)用的targetsdk<26,想用adaptive icon的話,就需要使用上述的xml,可以在mipmap-anydpi-v26文件夾中配置。
3.兩張圖層大小都必須為 108 x 108 dp。圖層中心 72 x 72 dp 范圍為可視范圍。系統(tǒng)會保留四周外的 36dp 范圍用于生成有趣的視覺效果(如視差效果和跳動)。
AdaptiveIconDrawable代碼走讀
Adaptive Icon實現(xiàn)方式通過上述xml來定義,我們來看下他的源碼實現(xiàn)方式。
首先它同BitmapDrawable,AnimationDrawable 等都是繼承了Drawable,核心功能就是實現(xiàn)drawable的draw方法。
首先看下它的構(gòu)造方法:
/**
* Constructor used to dynamically create this drawable.
*
* @param backgroundDrawable drawable that should be rendered in the background
* @param foregroundDrawable drawable that should be rendered in the foreground
*/
public AdaptiveIconDrawable(Drawable backgroundDrawable,
Drawable foregroundDrawable) {
this((LayerState)null, null);
if (backgroundDrawable != null) {
addLayer(BACKGROUND_ID, createChildDrawable(backgroundDrawable));
}
if (foregroundDrawable != null) {
addLayer(FOREGROUND_ID, createChildDrawable(foregroundDrawable));
}
}
這個方法里面獲取前景圖片和背景圖片。
我們再看下this的實現(xiàn)方法
/**
* The one constructor to rule them all. This is called by all public
* constructors to set the state and initialize local properties.
*/
AdaptiveIconDrawable(@Nullable LayerState state, @Nullable Resources res) {
mLayerState = createConstantState(state, res);
if (sMask == null) {
sMask = PathParser.createPathFromPathData(
Resources.getSystem().getString(R.string.config_icon_mask));
}
mMask = PathParser.createPathFromPathData(
Resources.getSystem().getString(R.string.config_icon_mask));
mMaskMatrix = new Matrix();
mCanvas = new Canvas();
mTransparentRegion = new Region();
}
這個方法我們重點關(guān)注一下mMask,這個變量就是代表的圖標的形狀。我們可以看到這個值獲取方式Resources.getSystem().getString(R.string.config_icon_mask)
查看這個config_icon_mask的值
<!-- Specifies the path that is used by AdaptiveIconDrawable class to crop launcher icons. -->
<string name="config_icon_mask" translatable="false">"M50,0L92,0C96.42,0 100,4.58 100 8L100,92C100, 96.42 96.42 100 92 100L8 100C4.58, 100 0 96.42 0 92L0 8 C 0 4.42 4.42 0 8 0L50 0Z"</string>
這個值代表的是一個矢量圖,矢量圖的標簽M,C,L等基本語法可以到網(wǎng)上搜索下。
也就是默認情況下獲取Adaptive Icon默認取得就是該形狀的圖標。這個應(yīng)該是個圓形的樣式。
然后我們再看下AdaptiveIconDrawable的draw方法,具體
private void updateMaskBoundsInternal(Rect b) {
mMaskMatrix.setScale(b.width() / MASK_SIZE, b.height() / MASK_SIZE);
sMask.transform(mMaskMatrix, mMask);
if (mMaskBitmap == null || mMaskBitmap.getWidth() != b.width() ||
mMaskBitmap.getHeight() != b.height()) {
mMaskBitmap = Bitmap.createBitmap(b.width(), b.height(), Bitmap.Config.ALPHA_8);
mLayersBitmap = Bitmap.createBitmap(b.width(), b.height(), Bitmap.Config.ARGB_8888);
}
// mMaskBitmap bound [0, w] x [0, h]
mCanvas.setBitmap(mMaskBitmap);
mPaint.setShader(null);
mCanvas.drawPath(mMask, mPaint);
// mMask bound [left, top, right, bottom]
mMaskMatrix.postTranslate(b.left, b.top);
mMask.reset();
sMask.transform(mMaskMatrix, mMask);
// reset everything that depends on the view bounds
mTransparentRegion.setEmpty();
mLayersShader = null;
}
@Override
public void draw(Canvas canvas) {
if (mLayersBitmap == null) {
return;
}
if (mLayersShader == null) {
mCanvas.setBitmap(mLayersBitmap);
mCanvas.drawColor(Color.BLACK);
for (int i = 0; i < mLayerState.N_CHILDREN; i++) {
if (mLayerState.mChildren[i] == null) {
continue;
}
final Drawable dr = mLayerState.mChildren[i].mDrawable;
if (dr != null) {
dr.draw(mCanvas);
}
}
mLayersShader = new BitmapShader(mLayersBitmap, TileMode.CLAMP, TileMode.CLAMP);
mPaint.setShader(mLayersShader);
}
if (mMaskBitmap != null) {
Rect bounds = getBounds();
canvas.drawBitmap(mMaskBitmap, bounds.left, bounds.top, mPaint);
}
}
意思就是將兩張圖層drawable先繪制上去,再根據(jù)getBounds區(qū)域?qū)MaskBitmap繪制上去。當然之前還有一些區(qū)域的縮放等操作。
還得了解下BitmapShader著色器的使用方法。
文中介紹有問題的,歡迎指正。謝謝。