Android5.0中多種水波紋效果的實(shí)現(xiàn)代碼
水波紋效果已經(jīng)不是什么稀罕的東西了,用過(guò)5.0新控件的小伙伴都知道這個(gè)效果,可是如果使用一個(gè)TextView或者Button或者其它普通控件的話(huà),你是否知道如何給它設(shè)置水波紋效果呢?OK,我們今天就來(lái)看看這個(gè)水波紋效果的實(shí)現(xiàn)。水波紋效果的實(shí)現(xiàn)有系統(tǒng)自帶屬性可以實(shí)現(xiàn),我們也可以自定義實(shí)現(xiàn)效果。
1.系統(tǒng)自帶水波紋實(shí)現(xiàn)方式 有界水波紋
水波紋效果大致上可以分為兩種,一種是有界的,一種無(wú)界,我們先來(lái)看看有界水波紋效果:
效果:

代碼:
<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。這種是有界水波紋,就是水波紋會(huì)在TextView所在區(qū)域進(jìn)行繪制。
無(wú)界水波紋

代碼:
<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!"/>
所謂的無(wú)界并非完全無(wú)界,而是以控件寬高中最大的數(shù)值作為水波紋效果所在正方形的邊界進(jìn)行繪制。OK,這兩種都是系統(tǒng)自帶的水波紋效果,如果我們想要自定義又該怎么做呢?
2.自定義水波紋實(shí)現(xiàn)方式無(wú)界水波紋
自定義這個(gè)效果其實(shí)也很簡(jiǎn)單,需要在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,大家看到這是無(wú)界水波紋。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>
有界水波紋需要我們?cè)趓ipple節(jié)點(diǎn)中定義item,item的id要為系統(tǒng)id mask,然后還要定義drawable,drawable中的顏色并沒(méi)有什么卵用,水波紋的顏色是由ripple節(jié)點(diǎn)中的顏色來(lái)控制的,看看顯示效果:

帶圖片形狀的水波紋
有的時(shí)候如果你希望水波紋不是長(zhǎng)條形,又該如何呢?有兩種解決方案,一種是使用圖片,還有就是自定義shape,我們先來(lái)看看使用圖片:
<?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ī)器人,我們來(lái)看看顯示效果:

大家看到,這個(gè)時(shí)候的水波紋效果就是這個(gè)小機(jī)器人這張圖片中非透明像素點(diǎn)所在的區(qū)域了。
自繪形狀的水波紋
自繪shape,來(lái)看一個(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>
顯示效果:

這種方式我們?cè)趕hape中定義的顏色只是用來(lái)劃定水波紋顯示區(qū)域,于視圖顯示上并沒(méi)有什么用。如果你想讓控件一開(kāi)始就顯示shape中定義的顏色,可以這樣來(lái)定義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,那么可能有小伙伴會(huì)想到我是否可以在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中水波紋效果的使用。
參考資料:
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android實(shí)現(xiàn)圓角邊框?qū)υ?huà)框的方法
在最近公司的Android項(xiàng)目,客戶(hù)畫(huà)好了界面,需求是一個(gè)圓角邊框的對(duì)話(huà)框效果,通過(guò)查找一些資料找到了解決方法,現(xiàn)在將方法總結(jié)后分享給大家,有需要的朋友們可以參考借鑒,下面來(lái)跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01
解決AMD無(wú)法使用Android studio問(wèn)題
這篇文章主要介紹了AMD無(wú)法使用Android studio解決方法,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
android dialog背景模糊化效果實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了android dialog背景模糊化效果的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android開(kāi)發(fā)獲取手機(jī)Mac地址適配所有Android版本
這篇文章主要介紹了Android開(kāi)發(fā)獲取手機(jī)Mac地址適配所有Android版本,需要的朋友可以參考下2020-03-03
Android中通過(guò)RxJava進(jìn)行響應(yīng)式程序設(shè)計(jì)的入門(mén)指南
響應(yīng)式編程在Android中的運(yùn)用是非常犀利的,比如在異常處理和調(diào)度器方面,這里我們將從生命周期等方面來(lái)講解Android中通過(guò)RxJava進(jìn)行響應(yīng)式程序設(shè)計(jì)的入門(mén)指南:2016-06-06

