項目中遇到selector中錯誤使用.9圖的情況
參考:http://www.lxweimin.com/p/a3ee9db15590
當使用selector設置button按壓效果透明度變化時,我們通常會這樣設置
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:alpha="0.3" android:src="@drawable/select_btn" />
</item>
<item android:drawable="@drawable/select_btn" />
</selector>
但是當select_btn是.9圖時,這樣使用會導致按壓效果時,圖片異常。
原因是bitmapdraw不支持.9圖的拉伸,可參考https://blog.csdn.net/a49220824/article/details/53267972提到的現象
如果是繪制,我們會用NinePatch解決,如下:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bubble);
NinePatch ninePatch = new NinePatch(bitmap, bitmap.getNinePatchChunk(), null);
RectF rectF = new RectF(l, t, r, b);
ninePatch.draw(canvas, rectF);
在selector中我們使用nine-patch標簽即可解決此問題
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<nine-patch android:alpha="0.3" android:src="@drawable/search_download_btn" />
</item>
<item android:drawable="@drawable/search_download_btn"/>
</selector>