转:点击打开链接

Rebound是Facebook推出的一款Android的物理和动画库,于2013年10月在Mobile@Scale大会上正式发布,旨在解决笨重、缓慢的传统移动网络界面。在其Rebound官方主页可以体验基于js版构建的Rebound效果

Rebound不是一款通用物理库,但其弹簧模型在应用程序中引入了现实世界的物理,易于集成,创建的动画能够让人感觉到非常自然,并且可以和Material Design的设计原则呼应,可用于滚动条、切换开关、呼叫等场景下。下面看一下如何在Android上实现和官方网页一样的演示效果:

(PS:完整代码放在博文最后)

1.首先添加Rebound库依赖

Rebound提供了三种方式引入,当然在Android Studio下还是推荐使用Gradle方式。

  • 添加Gradle依赖(推荐)
1
2
3
dependencies {
compile 'com.facebook.rebound:rebound:0.3.6'
}
  • 下载Rebound Jar文件,导入工程

  • 添加Maven依赖

1
2
3
4
5
<dependency>
<groupId>com.facebook.rebound</groupId>
<artifactId>rebound</artifactId>
<version>0.3.6</version>
</dependency>

2.首先创建一个SpringSystem对象

1
SpringSystem mSpringSystem = SpringSystem.create();

3.添加一个“弹簧”到系统

1
Spring mSpring = mSpringSystem.createSpring();

4.添加监听器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mSpring.addListener(this);
//实现SpringListener接口,需要实现下面方法
@Override
public void onSpringUpdate(Spring spring) {
}
@Override
public void onSpringAtRest(Spring spring) {
}
@Override
public void onSpringActivate(Spring spring) {
}
@Override
public void onSpringEndStateChange(Spring spring) {
}

5.设置动画结束值

1
mSpring.setEndValue(1f);

6.在弹簧更新数据是对图片进行对应伸缩

1
2
3
4
5
6
public void onSpringUpdate(Spring spring) {
float value = (float) spring.getCurrentValue();
float scale = 1f - (value * 0.5f);
mImageToAnimate.setScaleX(scale);
mImageToAnimate.setScaleY(scale);
}

通过上面几个步骤可以很方便的实现弹簧阻尼效果的图片伸缩,当然还加入SeekBar用于更新系数,具体实现细节查看完整代码。

效果如下:

完整代码:

布局文件

1
2
3
4
5
6
7
8
9
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_marginTop="100dp"
android:src="@drawable/rebound"
android:layout_centerHorizontal="true" />
<LinearLayout
android:orientation="horizontal"
android:id="@+id/llTension"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="张力系数: "
android:textSize="18sp"
android:layout_gravity="center"
android:layout_marginLeft="50dp"
android:id="@+id/tvTension" />
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/skTension" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_below="@id/llTension"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="阻力系数: "
android:textSize="18sp"
android:layout_marginLeft="50dp"
android:layout_gravity="center"
android:id="@+id/tvFriction" />
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/skFriction" />
</LinearLayout>
</RelativeLayout>

MainActivity.java

1
2
3
4
5
6
7
8
9
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
public class MainActivity extends Activity implements View.OnTouchListener, SpringListener {
private int tension = 40; //张力系数
private int friction = 3; //阻力系数
private ImageView ivScalaImage;
private SpringSystem mSpringSystem;
private Spring mSpring;
private SeekBar skTension,skFriction;
private TextView tvTension,tvFriction;
private SBListener sbListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initWidget();
//创建系统用于循环执行控件弹簧效果
mSpringSystem = SpringSystem.create();
//给系统添加一个“弹簧”
mSpring = mSpringSystem.createSpring();
//添加监听器,监听“弹簧”的形变
mSpring.addListener(this);
//根据张力系数和阻力系数创建一组“弹簧”参数
SpringConfig config = new SpringConfig(tension, friction);
//配置“弹簧”
mSpring.setSpringConfig(config);
}
private void initWidget() {
ivScalaImage = (ImageView) findViewById(R.id.imageView);
ivScalaImage.setOnTouchListener(this);
skTension = (SeekBar) findViewById(R.id.skTension);
skFriction = (SeekBar) findViewById(R.id.skFriction);
sbListener = new SBListener();
skTension.setMax(100);
skFriction.setMax(30);
skTension.setOnSeekBarChangeListener(sbListener);
skFriction.setOnSeekBarChangeListener(sbListener);
tvTension = (TextView) findViewById(R.id.tvTension);
tvFriction = (TextView) findViewById(R.id.tvFriction);
skTension.setProgress(tension);
skFriction.setProgress(friction);
tvTension.setText("张力系数: " + tension);
tvFriction.setText("阻力系数: "+friction);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mSpring.setEndValue(1f);
return true;
case MotionEvent.ACTION_UP:
mSpring.setEndValue(0f);
return true;
}
return false;
}
@Override
public void onSpringUpdate(Spring spring) {
float value = (float) spring.getCurrentValue();
float scale = 1f - (value * 0.5f);
ivScalaImage.setScaleX(scale);
ivScalaImage.setScaleY(scale);
}
@Override
public void onSpringAtRest(Spring spring) {
SpringConfig config = new SpringConfig(tension, friction);
mSpring.setSpringConfig(config);
}
@Override
public void onSpringActivate(Spring spring) {
}
@Override
public void onSpringEndStateChange(Spring spring) {
}
private class SBListener implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (seekBar.getId() == R.id.skTension){
tension = progress;
skTension.setProgress(tension);
tvTension.setText("张力系数: " + tension);
} else if (seekBar.getId() == R.id.skFriction){
friction = progress;
skFriction.setProgress(friction);
tvFriction.setText("阻力系数: "+friction);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mSpring.setAtRest();
}
}
}

介绍Facebook的rebound动画库相关推荐

  1. ceph 数据库_Facebook打开了动画库,Ceph在Red Hat找到了新家,等等

    ceph 数据库 开源新闻让您阅读愉快. 2014年4月26日至5月2日 在本周的开源新闻摘要中,我们介绍了开源Facebook动画库Pop,Red Hat对Ceph的收购等等. 您在本周还阅读了哪些 ...

  2. 让动画不再僵硬:Facebook Rebound Android动画库介绍

    official site:http://facebook.github.io/rebound github : https://github.com/facebook/rebound Rebound ...

  3. Facebook Rebound 弹性动画库 源码分析

    Rebound源码分析 让动画不再僵硬:Facebook Rebound Android动画库介绍一文中介绍了rebound这个库. 对于想体验一下rebound的效果,又懒得clone和编译代码的, ...

  4. Android基础-Facebook Rebound 弹性动画库 源码分析

    Facebook Rebound 弹性动画库 源码分析 设计的时候老是闲动画太生硬,于是找到了这个弹性动画.这个弹性动画是facebook开源的,Rebound项目地址:https://github. ...

  5. Facebook开源动画库 POP-POPBasicAnimation运用

    动画在APP开发过程中还是经常出现,将花几天的时间对Facebook开源动画库 POP进行简单的学习:本文主要针对的是POPBasicAnimation运用:实例源代码已经上传至gitHub,地址:h ...

  6. 前端能力模型-动画类型及动画库的介绍

    一.背景: 合适的动画不仅更能吸引人们的眼球,也能让你的应用体验更为流畅,而将动画的效果做到极致,才能让用户感到使用你的应用是一种享受,而不是觉得生硬和枯燥. 二.动画技术分类: 按技术类型来进行分类 ...

  7. AE 动画直接变原生代码:Airbnb 发布开源动画库 Lottie

    原文 Airbnb 发布的 Lottie 是一个面向 iOS.Android 和 React Native 的开源动画库. 简单来说,就是可以直接利用 AE 导出的 JSON 动画文件,将其解析为原生 ...

  8. Rebound-Android的弹簧动画库

    简介 官方网站 github Rebound是facebook出品的一个弹簧动画库,与之对应的IOS版本有一个pop动画库,也是非常的强大给力.Facebook真是互联网企业中的楷模,开源了很多的实用 ...

  9. 追踪app崩溃率、事件响应链、Run Loop、线程和进程、数据表的优化、动画库、Restful架构、SDWebImage的原理...

    1.如何追踪app崩溃率,如何解决线上闪退 当 iOS设备上的App应用闪退时,操作系统会生成一个crash日志,保存在设备上.crash日志上有很多有用的信息,比如每个正在执行线程的完整堆栈 跟踪信 ...

最新文章

  1. PHP随手记1--内置函数date
  2. MySQL外键设置中的的 Cascade、NO ACTION、Restrict、SET NULL
  3. 扯一扯 之 面试经历
  4. Linux之bash脚本编程---选择执行
  5. boost::hana::zip_with用法的测试程序
  6. Linux 中vim编辑器学习笔记
  7. python中 1.34e3_Python快速编程入门——第2章 Python基础语法
  8. Nodejs基础01
  9. 软件工程师工作内容和从业要求
  10. win7安装python3.6_Win7 64位下python3.6.5安装配置图文教程
  11. 弯管机编程软件电脑版_乐高Wedo2.0电脑版下载
  12. e考证通电脑上怎么用
  13. 科学道德与学风-2021雨课堂答案-第4章
  14. python定义数组长度_python数组长度
  15. 微信停止为苹果服务器,苹果手机终于解决了微信延迟
  16. 一个屌丝程序猿的人生(八)
  17. 计算机word缩小表格,word排版 文件等比例缩小 word表格等比例缩小
  18. CPU架构与指令集的关系
  19. Word 2016 for Mac(文档和文字处理软件)激活教程
  20. 使用 pip 快速安装 OpenCV

热门文章

  1. 【C++】面试题目,整理自牛客网
  2. for语句(循环结构)
  3. Go 开源项目推荐:一个简单的 Go 练手项目
  4. 电商!商品关键词查询搜索排名与优化
  5. Glade的简单使用说明+例子(一)
  6. 工作中需要将多张CAD图纸共同转换成DWF格式如何操作?
  7. TensorFlow实战:TensorFlow中的CNN
  8. 高中人民教育出版社信息技术必修1 p63评定体重指数等级试题
  9. iphone有什么软件测试信号,教你如何精确测试你的iPhone信号强度
  10. 串行通信与并行通信的区别