Android設(shè)置控件陰影的三種方法
本文實例為大家分享了Android設(shè)置控件陰影的方法,供大家參考,具體內(nèi)容如下
第一種方式:elevation
View的大小位置都是通過x,y確定的,而現(xiàn)在有了z軸的概念,而這個z值就是View的高度(elevation),而高度決定了陰影(shadow)的大小。

View Elevation(視圖高度)
View的z值由兩部分組成,elevation和translationZ(它們都是Android L新引入的屬性)。
eleavation是靜態(tài)的成員,translationZ是用來做動畫。
Z = elevation + translationZ
在layout中使用* android:elevation*屬性去定義
在代碼中使用 View.setElevation 方法去定義
設(shè)置視圖的translation,可以使用View.setTranslationZ方法
新的ViewPropertyAnimator.z和ViewPropertyAnimator.translationZ方法可以設(shè)置視圖的elevation值
我們通過設(shè)置elevation的值也會達(dá)到卡片陰影效果

第二種方式:CardView
今天有空學(xué)習(xí)了下CardView的使用,既然是使用,不凡使用一個實例操作一下
CardView是Android5.0的新控件,所以我們需要在dependencies中添加支持:
compile 'com.android.support:cardview-v7:26.0.0'
CardView是繼承FrameLayout的一個布局控件,從源碼可以看出CardView支持的屬性有:
card_view:cardElevation 陰影的大小
card_view:cardMaxElevation 陰影最大高度
card_view:cardBackgroundColor 卡片的背景色
card_view:cardCornerRadius 卡片的圓角大小
card_view:contentPadding 卡片內(nèi)容于邊距的間隔
card_view:contentPaddingBottom
card_view:contentPaddingTop
card_view:contentPaddingLeft
card_view:contentPaddingRight
card_view:contentPaddingStart
card_view:contentPaddingEnd
card_view:cardUseCompatPadding 設(shè)置內(nèi)邊距,V21+的版本和之前的版本仍舊具有一樣的計算方式
card_view:cardPreventConrerOverlap 在V20和之前的版本中添加內(nèi)邊距,這個屬性為了防止內(nèi)容和邊角的重疊
我們看一下今天要實現(xiàn)的效果圖:

有興趣的朋友可以嘗試使用ViewPager+CardView實現(xiàn)卡片畫廊的效果
其實CardView的使用相當(dāng)于加了一個布局使用,其CardView里面內(nèi)容的實現(xiàn),還是在布局中設(shè)計
銀行卡布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:padding="16dp">
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="180dp"
app:cardBackgroundColor="#099A8C"
app:cardCornerRadius="10dp"
app:cardElevation="10dp"
app:contentPadding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/icon_01" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中國農(nóng)業(yè)銀行"
android:textColor="#ffffff"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="儲蓄卡"
android:textColor="#ffffff"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#ffffff"
android:gravity="center_vertical"
android:textSize="22sp"
android:text="**** **** **** 1234"/>
</LinearLayout>
<ImageView
android:layout_width="60dp"
android:layout_height="15dp"
android:background="@drawable/icon_02" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
特別注意的是:使用CardView的屬性時,記得加上命名空間的聲明
xmlns:app="http://schemas.android.com/apk/res-auto
第三種方式:最強(qiáng)按鈕通過Color來進(jìn)行設(shè)置
自認(rèn)為這是按鈕最好看的效果,還自帶按下效果,設(shè)置也非常簡單,秒殺一切陰影效果,我們先來看下他的效果
未按下效果

按下效果

**其實這種效果非常簡單,就是定義了一個顏色。對就是一個顏色就可以達(dá)到這種效果
那這個顏色要怎么定義才能達(dá)到這種效果呢**
比如上圖的按鈕顏色是粉紅色,顏色代碼 #f692bf,我們只需要在前面加上#ff,最后這樣#ff692bf 就可以達(dá)到這種效果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio中配置OpenCV庫開發(fā)環(huán)境的教程
這篇文章主要介紹了Android Studio中配置OpenCV庫開發(fā)環(huán)境的教程,OpenCV有Java接口,因而也經(jīng)常被用來做安卓開發(fā),需要的朋友可以參考下2016-05-05
Android中區(qū)別Drawable Bitmap Canvas Paint
本文主要介紹Android中Drawable Bitmap Canvas Paint 之間的區(qū)別,這里對這幾個概念做出詳細(xì)介紹,開發(fā)Android游戲的朋友可以參考下2016-07-07
android nfc常用標(biāo)簽讀取總結(jié)
NFC(Near Field Communication,近場通信)是一種數(shù)據(jù)傳輸技術(shù)這篇文章主要介紹了android nfc常用標(biāo)簽讀取總結(jié),有興趣的可以了解一下。2016-12-12
Android編程仿Iphone拖動相片特效Gallery的簡單應(yīng)用示例
這篇文章主要介紹了Android編程仿Iphone拖動相片特效Gallery的簡單應(yīng)用,結(jié)合實例形式分析了Android圖形拖動特效的實現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
android 添加按(power鍵)電源鍵結(jié)束通話(掛斷電話)
首先我們發(fā)現(xiàn)現(xiàn)在我們所用的android智能手機(jī)大部分都有當(dāng)你在打電話時按power鍵來掛斷電話,一般都是在設(shè)置中2013-01-01
Flutter實現(xiàn)webview與原生組件組合滑動的示例代碼
這篇文章主要介紹了Flutter實現(xiàn)webview與原生組件組合滑動的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

