問(wèn)題回顧
在上一篇博客 Android 進(jìn)階學(xué)習(xí)(二十四) Android 中給View 添加Drawable的思考 的結(jié)尾處 ,我說(shuō)出了一個(gè)問(wèn)題,那就是我自己寫(xiě)的drawable 緩存,緩存的是drawable 本身,這就使得不同的View 在使用相同的drawable時(shí),drawable 不適配的情況,比如 RecyclerView 的item 是一個(gè) 水平自動(dòng)填充的item , 如果上一個(gè)使用這個(gè)drawable 的item 寬度比當(dāng)前這個(gè)item的寬度寬,就會(huì)導(dǎo)致當(dāng)前這個(gè)item 的背景drawable 的寬度超過(guò)了item的寬度,出現(xiàn)不適配的情況,那么這種情況 源碼是如何解決的呢,
@Nullable
Drawable loadDrawable(@NonNull Resources wrapper, @NonNull TypedValue value, int id,
int density, @Nullable Resources.Theme theme)
throws NotFoundException {
...省略部分代碼
if (!mPreloading && useCache) {
這里就是獲取緩存的drawable
final Drawable cachedDrawable = caches.getInstance(key, wrapper, theme);
if (cachedDrawable != null) {
cachedDrawable.setChangingConfigurations(value.changingConfigurations);
return cachedDrawable;
}
}
...省略部分代
}
}
class DrawableCache extends ThemedResourceCache<Drawable.ConstantState> {
@UnsupportedAppUsage
public Drawable getInstance(long key, Resources resources, Resources.Theme theme) {
final Drawable.ConstantState entry = get(key, theme);
if (entry != null) {
每次都是利用 Drawable.ConstantState 重新創(chuàng)建一個(gè)drawable
return entry.newDrawable(resources, theme);
}
return null;
}
}
從源碼這里我們可以看到,即使使用的是同樣的drawable.xml 文件,每個(gè)view 的drawable 都是單獨(dú)創(chuàng)建的,只不過(guò)這個(gè)模板只創(chuàng)建了一次,所以我們的緩存也需要緩存 Drawable.ConstantState ,每次獲取后,重新創(chuàng)建一個(gè)新的Drawable
Drawable 組件化
我們app 在探究組件化的過(guò)程中關(guān)于drawable 的問(wèn)題也遇到了不少, 由于組件化開(kāi)發(fā)的過(guò)程是先有的app,我們?cè)賹?duì)app進(jìn)行組件化拆分,在組件化的初期,很多資源文件都是到處copy 的,雖然我們給組件添加了 resourcePrefix 這個(gè)屬性,為每個(gè)組件的xml 資源命名做了限制,還是造成了不少打最終包成功后樣式對(duì)不上的問(wèn)題,而且隨著項(xiàng)目越來(lái)越大,新寫(xiě)一個(gè)drawable 所耗費(fèi)的時(shí)間,比找到一個(gè)相同屬性的drawable 的xml文件還要快一點(diǎn),這樣就造成了惡性循環(huán),大家就不停的創(chuàng)建新的drawable.xml 文件,找到相同屬性的就更難了...
伴隨著這樣的思考,能不能找到一個(gè)擺脫xml來(lái)設(shè)置背景drawable 的方法就這樣誕生了,但是這個(gè)過(guò)程也遇到了好幾個(gè)問(wèn)題
1.也就是上面我們所說(shuō)的 drawable 緩存的問(wèn)題,通過(guò)緩存 Drawable.ConstantState 每次獲取都重新創(chuàng)建drawable 這個(gè)問(wèn)題已經(jīng)解決了
2.我在最開(kāi)始的時(shí)候?yàn)檎麄€(gè)項(xiàng)目重新定義了一套 DrawableView ,比如 DrawableTextView , DrawableLinearLayout , DrawableRelativeLayout 等等, 在為每個(gè)View 添加屬性的時(shí)候,就產(chǎn)生了一個(gè)問(wèn)題,那就是
<declare-styleable name="DrawableRelativeLayout">
<attr name="rl_zhome_end_color" format="color"/>
<attr name="rl_zhome_bottom_line" format="boolean"/>
</declare-styleable>
<declare-styleable name="DrawableLinearLayout">
<attr name="ll_zhome_end_color" format="color"/>
<attr name="ll_zhome_bottom_line" format="boolean"/>
</declare-styleable>
每一個(gè)View 在使用 xml 添加drawable 屬性的時(shí)候,他們屬性命名是不同的,LinearLayout 的屬性就是 app:ll_zhome_color=""
RelativeLayout 的屬性就是 app:rl_zhome_color="",這樣就導(dǎo)致了不了解這個(gè)機(jī)制的人,如果只是copy屬性,就會(huì)導(dǎo)致使用不了這個(gè)問(wèn)題,那么能不能像 ConstraintLayout 一樣, 所有view 的屬性名都一致,并且android studio 還會(huì)自動(dòng)提示出屬性的名稱呢,
這個(gè)問(wèn)題的解決方案自己想一下其實(shí)也特別簡(jiǎn)單,那就是事先將屬性聲明出來(lái),在自定義View 的 declare-styleable 引用這個(gè)屬性就可以了,舉個(gè)栗子
<resources>
先把這個(gè) 屬性在 resources 標(biāo)簽下聲明出來(lái)
<attr name="zhome_div_color" format="color"/>
<declare-styleable name="DrawableRelativeLayout">
<attr name="zhome_div_color" />
</declare-styleable>
<declare-styleable name="DrawableLinearLayout">
<attr name="zhome_div_color" />
</declare-styleable>
<resources>
這樣就可以了,雖然在獲取屬性的時(shí)候是根據(jù)不同的View 來(lái)獲取的,但是在編寫(xiě) layout.xml 的過(guò)程中, 屬性的命名是一致的,
<com.tsm.tsmbottomsheetdialog.drawable.ZHomeLinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center_vertical"
android:paddingHorizontal="16dp"
android:orientation="horizontal"
app:zhome_div_bottom_line="true"
app:zhome_div_bottom_line_padding="16dp">
<com.tsm.tsmbottomsheetdialog.drawable.ZHomeTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/color_ct1_85"
android:text="ZHomeLinearLayout 下劃線"
app:zhome_div_bottom_line="true"
app:zhome_div_bottom_line_padding="16dp"/>
</com.tsm.tsmbottomsheetdialog.drawable.ZHomeLinearLayout>
插個(gè)圖下面就是我們?cè)谑褂眠^(guò)程中經(jīng)常遇到的屬性,我把它們每個(gè)屬性都標(biāo)注了出來(lái),方便使用的人來(lái)查看并使用相關(guān)的屬性,
<!-- 選中方式 nor 沒(méi)有 select->state_Selected checked->state_Checked enable->state_Enable pressed->state_Pressed -->
<attr name="zhome_div_selector">
<enum name="nor" value="0" />
<enum name="select" value="1" />
<enum name="checked" value="2" />
<enum name="enable" value="3" />
<enum name="pressed" value="4" />
</attr>
<!-- 設(shè)置描邊 描邊寬度 虛線等屬性選中與未選中狀態(tài)公用 -->
<!-- 未選中 描邊顏色-->
<attr name="zhome_div_stroken_color" format="color"/>
<!-- 選中 描邊顏色-->
<attr name="zhome_div_select_state_stroken_color" format="color"/>
<!-- 未選中與選中 描邊寬度-->
<attr name="zhome_div_stroken_width" format="dimension"/>
<!-- 未選中與選中 間隔-->
<attr name="zhome_div_stroken_dashgap" format="dimension"/>
<!-- 未選中與選中 虛線寬度-->
<attr name="zhome_div_stroken_dashwidth" format="dimension"/>
<!-- 背景色-->
<!-- 未選中 顏色-->
<attr name="zhome_div_color" format="color"/>
<!-- 選中 顏色-->
<attr name="zhome_div_select_state_color" format="color"/>
<!-- 圓角 選中圓角與未選中圓角相同-->
<attr name="zhome_div_radius" format="dimension"/>
<!-- 左上圓角-->
<attr name="zhome_div_top_left_radius" format="dimension"/>
<!-- 右上圓角-->
<attr name="zhome_div_top_right_radius" format="dimension"/>
<!-- 左下圓角-->
<attr name="zhome_div_bottom_left_radius" format="dimension"/>
<!-- 右下圓角-->
<attr name="zhome_div_bottom_right_radius" format="dimension"/>
<!-- 頂部圓角 左上 右上-->
<attr name="zhome_div_top_radius" format="dimension"/>
<!-- 底部圓角 左下 右下-->
<attr name="zhome_div_bottom_radius" format="dimension"/>
<!-- 漸變色-->
<!-- 漸變色角度-->
<attr name="zhome_div_angle" format="integer"/>
<!-- 漸變色開(kāi)始顏色-->
<attr name="zhome_div_start_color" format="color"/>
<!-- 漸變色中間顏色-->
<attr name="zhome_div_center_color" format="color"/>
<!-- 漸變色結(jié)束顏色-->
<attr name="zhome_div_end_color" format="color"/>
<!-- 底部橫線-->
<attr name="zhome_div_bottom_line" format="boolean"/>
<!-- 底部橫線邊距 部分UI 可能會(huì)出現(xiàn)底部橫線距離兩端存在一定間距-->
<attr name="zhome_div_bottom_line_padding" format="dimension"/>
<!-- 水波紋效果 由于有版本限制,這里使用前景色實(shí)現(xiàn)-->
<attr name="zhome_div_ripple" format="boolean"/>
<!-- 點(diǎn)擊縮放效果-->
<attr name="zhome_div_pressed_scale" format="boolean"/>
到了這里整個(gè)過(guò)程就告一段落了,這個(gè)一套drawable 在我們的組件里面已經(jīng)試用了很長(zhǎng)一段時(shí)間了,并沒(méi)有出現(xiàn)什么問(wèn)題,下面我方一下Github 地址 TsmBottomSheetDialog,這里就能找到相關(guān)代碼