Android设置控件阴影的三种方法

  

本文实例为大家分享了Android设置控件阴影的方法,供大家参考,具体内容如下
  

  

<强>第一种方式:海拔

  

视图的大小位置都是通过x, y确定的,而现在有了z轴的概念,而这个z值就是视图的高度(高度),而高度决定了阴影(影子)的大小。

  

 Android设置控件阴影的三种方法

  

<强>视图海拔(视图高度)

  

视图的z值由两部分组成,海拔和translationZ(它们都是Android L新引入的属性)。
  eleavation是静态的成员,translationZ是用来做动画。
  Z=海拔+ translationZ

  

在布局中使用* android:海拔*属性去定义,
  在代码中使用视图。setElevation方法去定义,
  设置视图的翻译,可以使用View.setTranslationZ方法,
  新的ViewPropertyAnimator.z和ViewPropertyAnimator.translationZ方法可以设置视图的高程值

  

我们通过设置高度的值也会达到卡片阴影效果

  

 Android设置控件阴影的三种方法

  

<强>第二种方式: CardView

  

今天有空学习了下CardView的使用,既然是使用,不凡使用一个实例操作一下
  

  

CardView是Android5.0的新控件,所以我们需要在依赖中添加支持:
  编译“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卡片内容于边距的间隔

  

,,,,

  

card_view: cardUseCompatPadding设置内边距,V21 +的版本和之前的版本仍旧具有一样的计算方式
  card_view: cardPreventConrerOverlap在V20和之前的版本中添加内边距,这个属性为了防止内容和边角的重叠
  

  

我们看一下今天要实现的效果图:

  

 Android设置控件阴影的三种方法

  

有兴趣的朋友可以尝试使用:viewpage + CardView实现卡片画廊的效果

  

其实CardView的使用相当于加了一个布局使用,其CardView里面内容的实现,还是在布局中设计
  银行卡布局:

  

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android:背景=" # ffffff "   android:填充=16 dp”比;      & lt; android.support.v7.widget.CardView   xmlns:应用=" http://schemas.android.com/apk/res-auto "   android: layout_width=" match_parent "   android: layout_height=" 180 dp”   应用:cardBackgroundColor=" # 099 a8c "   应用:cardCornerRadius=" 10 dp”   应用:cardElevation=" 10 dp”   应用:contentPadding=?6 dp”比;      LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" match_parent "   面向android:="水平"比;      & lt; ImageView   android: layout_width=" 50 dp”   android: layout_height=" 50 dp”   android:背景=癅drawable/icon_01”/比;      LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android: layout_weight=" 1 "   android:取向=按怪薄?   android:填充=? dp”比;      & lt; TextView   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android:文本="中国农业银行”   android:输入textColor=" # ffffff "   android: textSize=" 18 sp "/比;      & lt; TextView   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android:文本="储蓄卡”   android:输入textColor=" # ffffff "   android: textSize=" 16 sp "/比;   & lt; TextView   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android:输入textColor=" # ffffff "   android:重力=" center_vertical "   android: textSize=" 22 sp "   android:文本=" * * * * * * * * * * * * 1234”/比;   & lt;/LinearLayout>      & lt; ImageView   android: layout_width=" 60 dp "   android: layout_height=" 15 dp "   android:背景=癅drawable/icon_02”/比;   & lt;/LinearLayout>   & lt;/android.support.v7.widget.CardView>      & lt;/RelativeLayout>      

Android设置控件阴影的三种方法