Android 控件設(shè)置陰影效果
本文主要記錄Android控件設(shè)置陰影
- 給控件設(shè)置陰影,會(huì)使得界面元素更好看一寫,google 給我們提供了一個(gè)現(xiàn)成的控CardView,可以將CardView看做是FrameLayout在自身之上添加了圓角和陰影效果
- 本文是使用給控件設(shè)置背景實(shí)現(xiàn)陰影
- 在res/drawable 下新建一個(gè) Drawable Resource File
- 使用 layer-list 圖層 就是說可以多個(gè)圖層一層一層蓋上去
- 新建一個(gè)漸變的圖層 item作為背景圖層,主要是 gradient,shape是用來定義形狀的,corners設(shè)置角度,gradient定義該形狀里面為漸變色填充,startColor起始顏色,endColor結(jié)束顏色,angle表示方向角度。當(dāng)angle=0時(shí),漸變色是從左向右。 然后逆時(shí)針方向轉(zhuǎn),當(dāng)angle=90時(shí)為從下往上
<item > <shape android:shape="rectangle" > <gradient android:angle="90" android:endColor="#dad9d9" android:startColor="#03a430" /> <corners android:radius="10dp" /> </shape> </item>

新建一個(gè)圖層,作為頂層圖層, 陰影實(shí)現(xiàn)的原理,就是頂層的小顯示一點(diǎn),露出一部分下面的圖層,這個(gè)就需要設(shè)置Item的 left, top, right, bottom屬性,這幾個(gè)參數(shù)的設(shè)置就類似于設(shè)置margin, 這樣就可以出來陰影效果了,當(dāng)然為了效果,我的參數(shù)設(shè)置的有點(diǎn)夸張,只設(shè)置了右邊和下邊的陰影,solid 設(shè)置填充, 還有stroke 設(shè)置邊框,設(shè)置了顏色,設(shè)置了寬度就可以看見邊框效果了
<item > <shape android:shape="rectangle" > <gradient android:angle="90" android:endColor="#dad9d9" android:startColor="#03a430" /> <corners android:radius="10dp" /> </shape> </item> <item android:right="20dp" android:bottom="40dp"> <shape android:shape="rectangle" > <solid android:color="#FFFFFF"/> <corners android:radius="10dp" /> </shape> </item>

頂層的圖層上,還是可以添加觸摸的變化效果,即手指觸碰到該控件時(shí),控件背景色變化
<item android:right="2dp" android:bottom="6dp"> <selector> <item android:state_focused="false" android:state_pressed="true" > <shape android:shape="rectangle" > <solid android:color="@color/E5"/> <corners android:radius="10dp" /> </shape> </item> <item> <shape android:shape="rectangle" > <solid android:color="@color/white"/> <corners android:radius="10dp" /> </shape> </item> </selector> </item>
使用:android:background=”@drawable/你的陰影xml文件”

<LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/vcam_entry_border" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="@dimen/common_measure_10dp" android:textColor="@color/black_100" android:text="啦啦啦啦,我有陰影"/> </LinearLayout>
附 : 完整的陰影xml 代碼
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 陰影部分 最下面一層 --> <item > <shape android:shape="rectangle" > <gradient android:angle="90" android:endColor="#777777" android:startColor="#C5C5C5" /> <corners android:radius="10dp" /> </shape> </item> <!-- 背景部分 --> <!-- 形象的表達(dá):bottom left ... 類似于設(shè)置 margin --> <item android:right="2dp" android:bottom="6dp"> <selector> <item android:state_focused="false" android:state_pressed="true" > <shape android:shape="rectangle" > <solid android:color="@color/E5"/> <corners android:radius="10dp" /> </shape> </item> <item> <shape android:shape="rectangle" > <solid android:color="@color/white"/> <corners android:radius="10dp" /> </shape> </item> </selector> </item> </layer-list>
總結(jié)
以上所述是小編給大家介紹的Android 控件設(shè)置陰影效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android自定義View之自定義評(píng)價(jià)打分控件RatingBar實(shí)現(xiàn)自定義星星大小和間距
- Android中讓圖片自適應(yīng)控件的大小的方法
- android 設(shè)置控件的顏色字體的方法
- Android動(dòng)態(tài)添加設(shè)置布局與控件的方法
- Android中ImageView.src設(shè)置圖片拉伸、填滿控件的方法
- Android設(shè)置控件陰影的三種方法
- android 添加隨意拖動(dòng)的桌面懸浮窗口
- Android彈出窗口實(shí)現(xiàn)方法
- Android自定義彈出窗口PopupWindow使用技巧
- Android編程處理窗口控件大小,形狀,像素等UI元素工具類
相關(guān)文章
Android添加ButterKnife時(shí)報(bào)錯(cuò)Error:(2, 0) Cannot add extension wit
今天小編就為大家分享一篇關(guān)于Android添加ButterKnife時(shí)報(bào)錯(cuò)Error:(2, 0) Cannot add extension with name 'android'的解決辦法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
andoid打包短信發(fā)送到gmail郵箱實(shí)現(xiàn)代碼
andriod短信整合備份發(fā)送到gmail郵箱,需要在andoid手機(jī)配置好gmail郵箱,下面是具體的實(shí)現(xiàn)代碼,感興趣的朋友可以參考下哈2013-06-06
Android動(dòng)畫 實(shí)現(xiàn)開關(guān)按鈕動(dòng)畫(屬性動(dòng)畫之平移動(dòng)畫)實(shí)例代碼
這篇文章主要介紹了Android動(dòng)畫 實(shí)現(xiàn)開關(guān)按鈕動(dòng)畫(屬性動(dòng)畫之平移動(dòng)畫)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android XmlPullParser 方式解析 Xml 文檔
這篇文章主要介紹了Android XmlPullParser 方式解析 Xml 文檔的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android Studio實(shí)現(xiàn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
android中px、sp與dp之間進(jìn)行轉(zhuǎn)換詳解
android中在xml布局中我們可以使用dp和px都可以,但是在代碼中,很多方法只提供了設(shè)置px的方法,這時(shí)候就需要用到dp和px相互切換了,下面這篇文章主要給大家介紹了關(guān)于android中px、sp與dp之間進(jìn)行轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2022-08-08
Android AsyncTask的優(yōu)缺點(diǎn)詳解
本文主要介紹了Android AsyncTask的優(yōu)缺點(diǎn),具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02
Kotlin使用TransitionDrawable實(shí)現(xiàn)顏色漸變效果流程講解
這篇文章主要介紹了Kotlin使用TransitionDrawable實(shí)現(xiàn)顏色漸變效果,這里,我們通過TransitionDrawable顯示顏色漸變效果,包括背景顏色的變化,以及圖片與圖片的漸變效果2023-02-02

