Material Design设计的基本原则是“motion provides meaning”,并在那里适用是给视觉反馈给用户,当他们与我们的应用程序交互的一个重要领域。这样做的标准方法一直使用StateListDrawable一个控制的外观配合其状态(即启用,禁用,压 ​​等),这已经可用,因为API 1.使用虽然有用,但这种不符合“motion provides meaning”的原则,因为该控件的外观固定出场之间跳跃。StateListAnimator在API 21Material Design一起推出,是一个非常简单的方法来可视状态之间平滑过渡。在这个系列中,我们将看看如何使用StateListAnimator充分发挥其潜力。

在我们深入,我们很快就回顾一下StateListDrawable因为有一些共性,我们继续将变得清晰。让我们先从一个简单的布局:

[代码]xml代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"
  tools:context="com.stylingandroid.statelistanimator.MainActivity">
  
  <TextView
    android:id="@+id/textView"
    style="@style/Widget.TextView.Boxed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:foreground="@drawable/foreground_selector"
    android:text="@string/standard_state_list" />
  
</LinearLayout>

我们必须应用到一个标准样式的TextView其设置背景绘制,一些填充,小仰角,并使其可点击这样的TextView有一个按下状态。我不会在这里显示的风格,但它在源代码中,如果你想看看。

我们从布局做的关键是应用前景绘制这是一个StateListDrawable

[代码]xml代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/transparentAccent"
    android:state_pressed="true">
    <shape>
      <solid android:color="@color/transparentAccent" />
    </shape>
  </item>
  
  <item>
    <shape>
      <solid android:color="@android:color/transparent" />
    </shape>
  </item>
</selector>

这就是魔术发生 - 在这种情况下,微透明绿色 - 我们定义它有一个透明的填充,这将适用于彩色按下状态默认状态。如果我们运行这一点,我们可以看到标准的行为:

这是因为它明确规定了一些视觉反馈功能的TextView已经被挖掘,但它不是很鼓舞人心,肯定不会有任何动作。

那么,如何才能提高对吗?好了材料设计规范建议,按钮在材料一样的方式来表现和上升,以满足您的手指,所以我们可以通过改变高度,但是这样做本身并没有带给我们的运动实现这一点-它只是再跳一次。此外,StateListDrawable是不是查看,所以我们不能在标高改变视图从那里。这是StateListAnimator进来它使我们能够应用更改视图状态的变化,但使我们能够在经营的动画师。查看

[代码]xml代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true">
    <objectAnimator
      android:duration="@android:integer/config_shortAnimTime"
      android:propertyName="translationZ"
      android:valueTo="4dp"
      android:valueType="floatType" />
  </item>
  
  <item>
    <objectAnimator
      android:duration="@android:integer/config_shortAnimTime"
      android:propertyName="translationZ"
      android:valueTo="0dp"
      android:valueType="floatType" />
  </item>
</selector>

这个结构类似于一个StateListDrawable在于它具有包含每个状态的项的选择,但它是每一个的内容item是不同的-我们现在使用objectAnimator和前面那样代替的形状。这些都是标准的objectAnimators,让我们的动画视图的性能-在这种情况下,我们将动画translationZ将改变高程。一个重要的事情是,我们没有指定valueFrom在任动画属性。其结果是,起始值将是目前translationZ的价值

接下来我们需要添加第二个TextView的,而应用此:

[代码]xml代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"
  tools:context="com.stylingandroid.statelistanimator.MainActivity">
  
  <TextView
    android:id="@+id/textView"
    style="@style/Widget.TextView.Boxed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:foreground="@drawable/foreground_selector"
    android:text="@string/standard_state_list" />
  
  <TextView
    android:id="@+id/textView2"
    style="@style/Widget.TextView.Boxed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="@string/state_list_animator"
    android:stateListAnimator="@animator/selector_animator" />
  
</LinearLayout>

这里最重要的是,我们不应用此为前景,有一个名为特定属性stateListAnimator,我们需要使用应用此。如果我们现在运行这个,我们可以看到的TextView似乎上升到满足我们的触摸:

有一件事值得加入是我们可以将多个动画,但包裹他们一套内部。为了说明这一点,假设我们要进一步建议的TextView的上升使得b,因为它上升它稍大。我们可以通过增加实现这个objectAnimators动画的scaleXscaleY该属性的TextView

[代码]xml代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true">
    <set>
      <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="scaleX"
        android:valueTo="1.025"
        android:valueType="floatType" />
      <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="scaleY"
        android:valueTo="1.025"
        android:valueType="floatType" />
      <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="translationZ"
        android:valueTo="4dp"
        android:valueType="floatType" />
    </set>
  </item>
  
  <item>
    <set>
      <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="scaleX"
        android:valueTo="1.0"
        android:valueType="floatType" />
      <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="scaleY"
        android:valueTo="1.0"
        android:valueType="floatType" />
      <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="translationZ"
        android:valueTo="0dp"
        android:valueType="floatType" />
    </set>
  </item>
  
</selector>

这巧妙地增强了上升的影响:

这里使用属性动画师应该给一个提示,灵活而强大的如何StateListAnimator的,并且可以使用这个非常有用的技术来创造更多的非常酷的效果。

发掘StateListAnimator的全部潜能的更多相关文章

  1. 利用 ReSharper 自定义代码中的错误模式,在代码审查之前就发现并修改错误

    多人协作开发的项目总会遇到代码编写风格上的差异.一般工具都能帮我们将常见的差异统一起来——例如 if 的换行:但也有一些不那么通用,但项目中却经常会出现的写法也需要统一. 例如将单元测试中的 Asse ...

  2. Keymob带你玩转新广告法下的移动营销

    2015年9月1日新广告法正式实施,对广告代言人.广告类别.广告语等都做了一系列新规定,堪称有史以来最严广告法.随着新广告法的实施,以往一些庸俗.夸张的广告也逐渐和大众说再见了. 2015年 “互联网 ...

  3. POJ 3322 Bloxorz I

    首先呢 这个题目的名字好啊 ORZ啊 如果看不懂题意的话 请戳这里 玩儿几盘就懂了[微笑] http://www.albinoblacksheep.com/games/bloxorz 就是这个神奇的木 ...

  4. 第七天Scrum冲刺博客

    1.会议照片 2.项目进展 团队成员 昨日计划任务 今日计划任务 梁天龙  学习课程页面  建议页面 黄岳康  定义个人课程  登陆页面 吴哲翰  完成页面的与后端的沟通交流  继续保持确认功能齐全 ...

  5. 模糊测试——强制发掘安全漏洞的利器(Jolt 大奖精选丛书)

    模糊测试——强制发掘安全漏洞的利器(Jolt 大奖精选丛书) [美]Sutton, M.Greene, A.Amini, P. 著 段念赵勇译 ISBN 978-7-121-21083-9 2013年 ...

  6. 谈谈如何在面试中发掘程序猿的核心竞争力zz

    早两天看了知乎日报的这篇文章<什么是程序员的核心竞争力?>,caoz讲的几点是让我感同身受.这让我联想起了给程序猿的面试,其实也就是通过短暂的接触来发掘程序猿的核心竞争力.接下来我就谈谈我 ...

  7. 发掘ListBox的潜力(三):显示即时提示(Tips)

    ListBox显示即时提示(Tips) Listbox内容太长时超出Listbox宽度的部分将无法显示,一种解决方法是让Listbox产生横向滚动条,滚动显示内容(见前面的<发掘ListBox的 ...

  8. 发掘ListBox的潜力(一):自动调整横向滚动条宽度

    <自绘ListBox的两种效果>一文帖出之后,从反馈信息来看,大家对这种小技巧还是很认同.接下来我将继续围绕ListBox写一系列的文章,进一步发掘ListBox的潜力,其中包括:自动调整 ...

  9. 使用CNN(convolutional neural nets)关键的一点是检测到的面部教程(四):学习率,学习潜能,dropout

    第七部分 让 学习率 和 学习潜能 随时间的变化 光训练就花了一个小时的时间.等结果并非一个令人心情愉快的事情.这一部分.我们将讨论将两个技巧结合让网络训练的更快! 直觉上的解决的方法是,開始训练时取 ...

随机推荐

  1. win7下的nginx小demo

    一直大概知道nginx怎么玩,但是不看文档又蒙蔽.在这记录一下,以后好查看 下载tomcat,改index.jsp http://tomcat.apache.org/download-80.cgi t ...

  2. FOJ Problem 1015 土地划分

    Problem 1015 土地划分 Accept: 823    Submit: 1956Time Limit: 1000 mSec    Memory Limit : 32768 KB  Probl ...

  3. 重复造轮子系列--内存池(C语言)

    这个代码是我上个公司工作项目的里面内存管理(基于伙伴算法)的一个简化又简化的版本. 因为没有内存边界检查: 因为没有内存使用统计: 因为没有考虑线程安全: 因为没有内存分配操作的具体文件位置信息: 因 ...

  4. Thread 线程池

    Thread 线程池: 当使用多个较短存活期的线程有利时,运用线程池技术可以发挥作用.运用这一技术时,不是为每个任务创建一个全新的线程,而可以从线程池中抽出线程,并分配给任务.当线程完成任务后,再把它 ...

  5. BI商业智能培训系列——(二)SSIS入门

    简介: SSIS,Microsoft SQL Server Integration Services.Integration意为"整合"."一体化".上篇博客中 ...

  6. J2EE的十三种技术——JNDI

    背景: 上一篇博客中介绍了J2EE的十三种技术之一--JDBC,主要用于提供了统一访问多种数据库的方式.这篇文章我们继续介绍J2EE的技术--JNDI. JNDI: Java Naming and D ...

  7. 【DNS】- 域名解析中A记录、CNAME、MX记录、NS记录的区别和联系

    1. A记录 又称IP指向,用户可以在此设置子域名并指向到自己的目标主机地址上,从而实现通过域名找到服务器.说明:·指向的目标主机地址类型只能使用IP地址: 附加说明: 1) 泛域名解析即将该域名所有 ...

  8. mvvm实现

    https://segmentfault.com/a/1190000006599500 http://blog.csdn.net/pur_e/article/details/53066275

  9. 从零开始--Spring项目整合(1)使用maven框架搭建项目

    这些年一直在用spring的框架搭建项目,现在开始我们从零开始利用Spring框架来搭建项目,目前我能想到有Spring.SpringMVC.SpringJDBC.Mybatis.WebSockt.R ...

  10. 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the C…

    题目描述 Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths tha ...