发掘StateListAnimator的全部潜能
- 原文地址:https://blog.stylingandroid.com/statelistanimator/
- 原文作者:Leave a reply
- 译文出自:安卓巴士
- 译者: MrlLee
- 校对者: 李世铿
Material Design设计的基本原则是“motion provides meaning”,并在那里适用是给视觉反馈给用户,当他们与我们的应用程序交互的一个重要领域。这样做的标准方法一直使用StateListDrawable一个控制的外观配合其状态(即启用,禁用,压 等),这已经可用,因为API 1.使用虽然有用,但这种不符合“motion provides meaning”的原则,因为该控件的外观固定出场之间跳跃。StateListAnimator在API 21Material Design一起推出,是一个非常简单的方法来可视状态之间平滑过渡。在这个系列中,我们将看看如何使用StateListAnimator充分发挥其潜力。
[代码]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动画的scaleX和scaleY该属性的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的全部潜能的更多相关文章
- 利用 ReSharper 自定义代码中的错误模式,在代码审查之前就发现并修改错误
多人协作开发的项目总会遇到代码编写风格上的差异.一般工具都能帮我们将常见的差异统一起来——例如 if 的换行:但也有一些不那么通用,但项目中却经常会出现的写法也需要统一. 例如将单元测试中的 Asse ...
- Keymob带你玩转新广告法下的移动营销
2015年9月1日新广告法正式实施,对广告代言人.广告类别.广告语等都做了一系列新规定,堪称有史以来最严广告法.随着新广告法的实施,以往一些庸俗.夸张的广告也逐渐和大众说再见了. 2015年 “互联网 ...
- POJ 3322 Bloxorz I
首先呢 这个题目的名字好啊 ORZ啊 如果看不懂题意的话 请戳这里 玩儿几盘就懂了[微笑] http://www.albinoblacksheep.com/games/bloxorz 就是这个神奇的木 ...
- 第七天Scrum冲刺博客
1.会议照片 2.项目进展 团队成员 昨日计划任务 今日计划任务 梁天龙 学习课程页面 建议页面 黄岳康 定义个人课程 登陆页面 吴哲翰 完成页面的与后端的沟通交流 继续保持确认功能齐全 ...
- 模糊测试——强制发掘安全漏洞的利器(Jolt 大奖精选丛书)
模糊测试——强制发掘安全漏洞的利器(Jolt 大奖精选丛书) [美]Sutton, M.Greene, A.Amini, P. 著 段念赵勇译 ISBN 978-7-121-21083-9 2013年 ...
- 谈谈如何在面试中发掘程序猿的核心竞争力zz
早两天看了知乎日报的这篇文章<什么是程序员的核心竞争力?>,caoz讲的几点是让我感同身受.这让我联想起了给程序猿的面试,其实也就是通过短暂的接触来发掘程序猿的核心竞争力.接下来我就谈谈我 ...
- 发掘ListBox的潜力(三):显示即时提示(Tips)
ListBox显示即时提示(Tips) Listbox内容太长时超出Listbox宽度的部分将无法显示,一种解决方法是让Listbox产生横向滚动条,滚动显示内容(见前面的<发掘ListBox的 ...
- 发掘ListBox的潜力(一):自动调整横向滚动条宽度
<自绘ListBox的两种效果>一文帖出之后,从反馈信息来看,大家对这种小技巧还是很认同.接下来我将继续围绕ListBox写一系列的文章,进一步发掘ListBox的潜力,其中包括:自动调整 ...
- 使用CNN(convolutional neural nets)关键的一点是检测到的面部教程(四):学习率,学习潜能,dropout
第七部分 让 学习率 和 学习潜能 随时间的变化 光训练就花了一个小时的时间.等结果并非一个令人心情愉快的事情.这一部分.我们将讨论将两个技巧结合让网络训练的更快! 直觉上的解决的方法是,開始训练时取 ...
随机推荐
- HDU 1939 HE IS OFFSIDE
He is offside! Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 思梦PHP-阿里大鱼手机验证码
小伙伴是否做PC网站的时候,是否遇到过注册用户需要使用短信验证的功能呢?或者找回密码,以及验证用户的信息等等功能!今天思梦PHP就为大家带来ThinkPHP整合阿里大鱼短信验证的功能! 首先,我们要明 ...
- 【bzoj2770】YY的Treap 权值线段树
题目描述 志向远大的YY小朋友在学完快速排序之后决定学习平衡树,左思右想再加上SY的教唆,YY决定学习Treap.友爱教教父SY如砍瓜切菜般教会了YY小朋友Treap(一种平衡树,通过对每个节点随机分 ...
- P3531 [POI2012]LIT-Letters
题目描述 Little Johnny has a very long surname. Yet he is not the only such person in his milieu. As it ...
- getprop 获取android系统属性
Android属性系统 property_get/property_set (很透彻)http://www.blogjava.net/MEYE/articles/359773.html getpro ...
- mysql 查询结果创建表
用 SELECT 的结果创建表 关系数据库的一个重要概念是,任何数据都表示为行和列组成的表,而每条 SELECT 语句的结果也都是一个行和列组成的表.在许多情况下,来自 SELECT 的“表”仅是一个 ...
- css3文件树
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 百度经验:Win10查看已存储WiFi密码的两种方法
方法一:网络和共享中心查询 具体步骤可以参考:Win10查看WIFI密码的方法 方法二:命令提示符查询 1.右键单击开始按钮,选择“命令提示符(管理员)” 2.输入如下命令(下图①号命令): nets ...
- iOS_自定义毛玻璃效果
http://www.2cto.com/kf/201408/329969.html 最终效果图: 关键代码: UIImage分类代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...
- (二十二)函数fseek() 用法
fseek 函数名: fseek功 能: 重定位流上的文件指针用 法: int fseek(FILE *stream, long offset, int fromwhere);描 述: 函数设置文件指 ...