自適應(yīng)圖標(biāo)主要用于在Launcher上可以根據(jù)不同的配置顯示不同形狀的圖標(biāo),可以顯示圓形方形等形狀漩符。
Adaptive Icons介紹
對應(yīng)Adaptive Icons的介紹google開發(fā)者和各路翻譯過來的網(wǎng)址很多廊散,這里貼下兩個網(wǎng)址僅供參考河泳。
官方地址
翻譯地址
主要說明應(yīng)用適配Adaptive Icons的注意點(diǎn)和方式腾誉。
1.當(dāng)應(yīng)用targetsdk>=26蚌斩,adaptive icon就會自動生效,即使資源中并沒有指定為adaptive icon堆缘,但實(shí)際上使用adaptive icon滔灶,圖片資源是要重新修改的,如果不改吼肥,雖然自適應(yīng)會生效录平,但效果可能不好。
如何讓應(yīng)用的圖標(biāo)效果更好呢缀皱?
定義一個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實(shí)現(xiàn)方式通過上述xml來定義,我們來看下他的源碼實(shí)現(xiàn)方式极舔。
首先它同BitmapDrawable凤覆,AnimationDrawable 等都是繼承了Drawable,核心功能就是實(shí)現(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的實(shí)現(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();
}
這個方法我們重點(diǎn)關(guān)注一下mMask,這個變量就是代表的圖標(biāo)的形狀。我們可以看到這個值獲取方式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>
這個值代表的是一個矢量圖稽揭,矢量圖的標(biāo)簽M,C,L等基本語法可以到網(wǎng)上搜索下俺附。
也就是默認(rèn)情況下獲取Adaptive Icon默認(rèn)取得就是該形狀的圖標(biāo)。這個應(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繪制上去揪胃。當(dāng)然之前還有一些區(qū)域的縮放等操作璃哟。
還得了解下BitmapShader著色器的使用方法。
文中介紹有問題的喊递,歡迎指正随闪。謝謝。