Android5.0中多種水波紋效果的實(shí)現(xiàn)代碼
水波紋效果已經(jīng)不是什么稀罕的東西了,用過5.0新控件的小伙伴都知道這個(gè)效果,可是如果使用一個(gè)TextView或者Button或者其它普通控件的話,你是否知道如何給它設(shè)置水波紋效果呢?OK,我們今天就來看看這個(gè)水波紋效果的實(shí)現(xiàn)。水波紋效果的實(shí)現(xiàn)有系統(tǒng)自帶屬性可以實(shí)現(xiàn),我們也可以自定義實(shí)現(xiàn)效果。
1.系統(tǒng)自帶水波紋實(shí)現(xiàn)方式 有界水波紋
水波紋效果大致上可以分為兩種,一種是有界的,一種無界,我們先來看看有界水波紋效果:
效果:
代碼:
<TextView android:layout_width="match_parent" android:layout_height="56dp" android:layout_centerInParent="true" android:layout_marginTop="36dp" android:background="?android:attr/selectableItemBackground" android:clickable="true" android:gravity="center" android:text="Hello World!"/>
只需要給TextView設(shè)置背景即可,背景內(nèi)容就為系統(tǒng)自帶的selecttableItemBackground。這種是有界水波紋,就是水波紋會在TextView所在區(qū)域進(jìn)行繪制。
無界水波紋
代碼:
<TextView android:layout_width="match_parent" android:layout_height="56dp" android:layout_centerInParent="true" android:layout_marginTop="36dp" android:background="?android:attr/selectableItemBackgroundBorderless" android:clickable="true" android:gravity="center" android:text="Hello World!"/>
所謂的無界并非完全無界,而是以控件寬高中最大的數(shù)值作為水波紋效果所在正方形的邊界進(jìn)行繪制。OK,這兩種都是系統(tǒng)自帶的水波紋效果,如果我們想要自定義又該怎么做呢?
2.自定義水波紋實(shí)現(xiàn)方式無界水波紋
自定義這個(gè)效果其實(shí)也很簡單,需要在drawable文件夾中定義ripple節(jié)點(diǎn),再設(shè)置上顏色就可以了:
<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorAccent"> </ripple>
在布局文件中將之引用為控件的背景:
<TextView android:layout_width="match_parent" android:layout_height="56dp" android:layout_centerInParent="true" android:layout_marginTop="36dp" android:background="@drawable/nomaskripple" android:clickable="true" android:gravity="center" android:text="Hello World!"/>
顯示效果如下:
OK,大家看到這是無界水波紋。OK,如果想定義有界水波紋又該如何呢?
有界水波紋
<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorPrimary"> <item android:id="@android:id/mask" android:drawable="@color/colorAccent"/> </ripple>
有界水波紋需要我們在ripple節(jié)點(diǎn)中定義item,item的id要為系統(tǒng)id mask,然后還要定義drawable,drawable中的顏色并沒有什么卵用,水波紋的顏色是由ripple節(jié)點(diǎn)中的顏色來控制的,看看顯示效果:
帶圖片形狀的水波紋
有的時(shí)候如果你希望水波紋不是長條形,又該如何呢?有兩種解決方案,一種是使用圖片,還有就是自定義shape,我們先來看看使用圖片:
<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorAccent"> <item android:id="@android:id/mask" android:drawable="@drawable/ic_launcher"/> </ripple>
我這里使用了系統(tǒng)自帶的小機(jī)器人,我們來看看顯示效果:
大家看到,這個(gè)時(shí)候的水波紋效果就是這個(gè)小機(jī)器人這張圖片中非透明像素點(diǎn)所在的區(qū)域了。
自繪形狀的水波紋
自繪shape,來看一個(gè)圓角矩形:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp"/> <solid android:color="@color/colorPrimary"/> </shape>
在ripple中引用該矩形:
<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorAccent"> <item android:id="@android:id/mask" android:drawable="@drawable/custom_shape"/> </ripple>
顯示效果:
這種方式我們在shape中定義的顏色只是用來劃定水波紋顯示區(qū)域,于視圖顯示上并沒有什么用。如果你想讓控件一開始就顯示shape中定義的顏色,可以這樣來定義ripple:
<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorAccent"> <!--<item--> <!--android:id="@android:id/mask"--> <!--android:drawable="@drawable/custom_shape"/>--> <item> <shape android:shape="rectangle"> <corners android:radius="10dp"/> <solid android:color="@color/colorPrimary"/> </shape> </item> </ripple>
顯示效果如下:
大家看到,我可以在item中定義shape,那么可能有小伙伴會想到我是否可以在item中定義selector呢?當(dāng)然可以。
帶selector效果的水波紋
代碼:
<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorAccent"> <item> <selector> <item android:state_pressed="true" android:drawable="@drawable/ic_launcher"/> <item android:state_pressed="false" android:drawable="@drawable/bg"/> </selector> </item> </ripple>
顯示效果:
Ok,這就是5.0中水波紋效果的使用。
參考資料:
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Android 模擬新聞APP顯示界面滑動優(yōu)化實(shí)例代碼
所謂滑動優(yōu)化就是滑動時(shí)不加載圖片,停止才加載,第一次進(jìn)入時(shí)手動加載。下面通過本文給大家介紹android 模擬新聞app顯示界面滑動優(yōu)化實(shí)例代碼,需要的朋友可以參考下2017-03-03Android 三種延遲操作的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 延遲操作的實(shí)現(xiàn)方法的相關(guān)資料,這里提供了三種實(shí)現(xiàn)方法,希望能幫助到大家,需要的朋友可以參考下2017-08-08Android Google AutoService框架使用詳解
AutoService是Google開發(fā)一個(gè)自動生成SPI清單文件的框架。看過一些基于APT的三方框架源碼的讀者應(yīng)該有所了解。比如Arouter、EventBus等等2022-11-11Android Studio如何查看源碼并調(diào)試的方法步驟
這篇文章主要介紹了Android Studio如何查看源碼并調(diào)試的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Android 判斷網(wǎng)絡(luò)狀態(tài)對音頻靜音的實(shí)現(xiàn)方法
最近小編做項(xiàng)目遇到這樣的需求,需要根據(jù)當(dāng)前場景讓app變的智能,讓app根據(jù)使用者當(dāng)前網(wǎng)絡(luò)狀態(tài),自動記性靜音等操作,具體怎么實(shí)現(xiàn)呢?下面小編給大家分享實(shí)例代碼,需要的朋友參考下吧2018-10-10Android App開發(fā)中自定義View和ViewGroup的實(shí)例教程
這篇文章主要介紹了Android App開發(fā)中自定義View和ViewGroup的實(shí)例教程,分別介紹了進(jìn)度條和圖片上傳并排列的例子,效果很好很強(qiáng)大,需要的朋友可以參考下2016-05-05flutter 怎么實(shí)現(xiàn)app整體灰色效果
Flutter 是 Google 開源的 UI 工具包,幫助開發(fā)者通過一套代碼庫高效構(gòu)建多平臺精美應(yīng)用,支持移動、Web、桌面和嵌入式平臺。這篇文章給大家介紹flutter 怎么實(shí)現(xiàn)app整體灰色效果,感興趣的朋友一起看看吧2020-04-04當(dāng)ListView有Header時(shí) onItemClick里的position不正確的原因
這篇文章主要介紹了當(dāng)ListView有Header時(shí) onItemClick里的position不正確的原因的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07