打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android 设置主题实现点击波纹效果

http://www.jianshu.com/p/cba46422de67


开头先说说大家都知道的Material Design。

这里推荐大苞米的系列博客,介绍的很全面。

http://blog.csdn.net/a396901990/article/category/2634371


Material Design:


Material Design是Google推出的一个全新的设计语言,它的特点就是拟物扁平化。


Material Design包含了很多内容,大致把它分为四部分:

主题和布局——ANDROID L——Material Design详解(主题和布局)

视图和阴影——ANDROID L——Material Design详解(视图和阴影)

UI控件——ANDROID L——Material Design详解(UI控件)

动画——ANDROID L——Material Design详解(动画篇)


首先说一下主题:

  1. <!-- res/values/styles.xml -->  
  2. <resources>  
  3.   <!-- your app's theme inherits from the Material theme -->  
  4.   <style name="AppTheme" parent="android:Theme.Material">  
  5.     <!-- theme customizations -->  
  6.   </style>  
  7. </resources>  

在最新的5.0中,google似乎不推荐使用Material Design主题了,而是由AppCompat代替。

  1. <resources>  
  2.   
  3.     <!-- Base application theme. -->  
  4.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  5.         <!-- Customize your theme here. -->  
  6.         <item name="colorPrimary">@color/colorPrimary</item>  
  7.         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>  
  8.         <item name="colorAccent">@color/colorAccent</item>  
  9.     </style>  
  10.   
  11. </resources>  


自定义状态条和导航条:


material还允许你轻松的自定义状态条和导航条的颜色。

可以使用如下属性参考下方图片

android:statusBarColorWindow.setStatusBarColor





兼容性:


由于Material Theme只可以在Android L Developer Preview中使用。

所以在低版本使用的话就需要为其另设一套主题:

在老版本使用一套主题 res/values/styles.xml,在新版本使用Material主题res/values-v21/styles.xml.


系统自带点击事件的控件一般都具有默认的波纹效果,直接使用即可:

  1. <RelativeLayout  
  2.                 android:layout_width="match_parent"  
  3.                 android:layout_height="wrap_content"  
  4.                 >  
  5.   
  6.                 <Button  
  7.                     android:id="@+id/myBtn"  
  8.                     android:layout_width="match_parent"  
  9.                     android:layout_height="wrap_content"  
  10.                     android:drawablePadding="10dp"  
  11.                     android:gravity="center_vertical"  
  12.                     android:paddingBottom="15dip"  
  13.                     android:paddingLeft="15dip"  
  14.                     android:paddingRight="25dip"  
  15.                     android:paddingTop="15dip"  
  16.                     android:text="Click"  
  17.                     android:textColor="@color/common_black_text"  
  18.                     android:textSize="16sp" />  
  19.             </RelativeLayout>  

怎么为view添加点击波纹效果呢,先了解下面的东西。

触摸反馈:


在Android L5.0中加入了触摸反馈动画。

其中最明显,最具代表性的就是波纹动画,比如当点击按钮时会从点击的位置产生类似于波纹的扩散效果。


波纹效果(Ripple):


当你使用了Material主题后,波纹动画会自动应用在所有的控件上,我们当然可以来设置其属性来调整到我们需要的效果。


可以通过如下代码设置波纹的背景:

android:background="?android:attr/selectableItemBackground"波纹有边界

android:background="?android:attr/selectableItemBackgroundBorderless"波纹超出边界


使用效果如下:

B1是不设任何背景的按钮

B2设置了?android:attr/selectableItemBackground

B3设置了?android:attr/selectableItemBackgroundBorderless




设置颜色


我们也可以通过设置xml属性来调节动画颜色,从而可以适应不同的主题:

android:colorControlHighlight:设置波纹颜色

android:colorAccent:设置checkbox等控件的选中颜色


比如下面这个比较粉嫩的主题,就需要修改动画颜色来匹配(上面已经有介绍):



为view添加波纹效果:

  1. <RelativeLayout  
  2.                 android:id="@+id/user_info_layout"  
  3.                 android:layout_width="match_parent"  
  4.                 android:layout_height="wrap_content"  
  5.                 android:clickable="true"  
  6.                 android:background="?android:attr/selectableItemBackground"  
  7.                 android:layout_marginTop="10dp"  
  8.                 android:paddingBottom="15dip"  
  9.                 android:paddingTop="15dip">  
  10.   
  11.                 <TextView  
  12.                     android:id="@+id/user_info_text"  
  13.                     android:layout_width="wrap_content"  
  14.                     android:layout_height="wrap_content"  
  15.                     android:drawablePadding="10dp"  
  16.                     android:gravity="center_vertical"  
  17.                     android:paddingLeft="15dip"  
  18.                     android:paddingRight="25dip"  
  19.                     android:text="我的资料"  
  20.                     android:textSize="16sp" />  
  21.   
  22.                 <ImageView  
  23.                     android:layout_width="wrap_content"  
  24.                     android:layout_height="wrap_content"  
  25.                     android:layout_alignParentRight="true"  
  26.                     android:layout_centerInParent="true"  
  27.                     android:contentDescription="@null"  
  28.                     android:paddingRight="15dip"  
  29.                      />  
  30.             </RelativeLayout>  



为Textview添加波纹效果:
  1. <LinearLayout  
  2.                 android:layout_width="match_parent"  
  3.                 android:layout_height="68dp"  
  4.                 android:weightSum="4"  
  5.                 android:gravity="center_vertical">  
  6.   
  7.                 <TextView  
  8.                     android:id="@+id/user_unpaid"  
  9.                     android:layout_width="0dp"  
  10.                     android:layout_height="wrap_content"  
  11.                     android:background="?android:attr/selectableItemBackgroundBorderless"  
  12.                     android:drawableTop="@mipmap/ic_user_paid"  
  13.                     android:drawablePadding="5dp"  
  14.                     android:gravity="center"  
  15.                     android:layout_weight="1"  
  16.                     android:text="待付款"  
  17.                     android:textSize="12sp"  
  18.                     android:clickable="true"/>  
  19.                 <TextView  
  20.                     android:id="@+id/user_paid"  
  21.                     android:layout_width="0dp"  
  22.                     android:layout_height="wrap_content"  
  23.                     android:background="?android:attr/selectableItemBackgroundBorderless"  
  24.                     android:drawableTop="@mipmap/ic_user_paid"  
  25.                     android:drawablePadding="5dp"  
  26.                     android:layout_weight="1"  
  27.                     android:gravity="center"  
  28.                     android:text="待发货"  
  29.                     android:textColor="@color/common_black_text"  
  30.                     android:textSize="12sp"  
  31.                     android:clickable="true"/>  
  32.                 <TextView  
  33.                     android:id="@+id/user_unreceived"  
  34.                     android:layout_width="0dp"  
  35.                     android:layout_height="wrap_content"  
  36.                     android:background="?android:attr/selectableItemBackgroundBorderless"  
  37.                     android:drawableTop="@mipmap/ic_user_paid"  
  38.                     android:drawablePadding="5dp"  
  39.                     android:gravity="center"  
  40.                     android:layout_weight="1"  
  41.                     android:text="待收货"  
  42.                     android:textColor="@color/common_black_text"  
  43.                     android:textSize="12sp"  
  44.                     android:clickable="true"/>  
  45.                 <TextView  
  46.                     android:id="@+id/user_completed"  
  47.                     android:layout_width="0dp"  
  48.                     android:layout_height="wrap_content"  
  49.                     android:background="?android:attr/selectableItemBackgroundBorderless"  
  50.                     android:drawableTop="@mipmap/ic_user_paid"  
  51.                     android:drawablePadding="5dp"  
  52.                     android:gravity="center"  
  53.                     android:layout_weight="1"  
  54.                     android:text="已完成"  
  55.                     android:textSize="12sp"  
  56.                     android:clickable="true"/>  
  57.   
  58.             </LinearLayout>  


这样就可以实现波纹效果啦!     
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android:自定义Tab样式
自定义PreferenceActivity——修改Preference样式、加顶部布局
自定义Button形状(圆形、椭圆)
Badge on Android TabHost
仿迅雷之Android版
Android实训案例(一)——计算器的运算逻辑
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服