This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
/
RevealAnimUtil.java
94 lines (84 loc) · 3.48 KB
/
RevealAnimUtil.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
package com.xiecc.seeWeather.common.utils;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.AccelerateDecelerateInterpolator;
/**
* Created by HugoXie on 16/7/7.
*
* Email: Hugo3641@gamil.com
* GitHub: https://github.com/xcc3641
* Info: 焦点在 fab 上的 reveal 动画,切换 activity 使用
*/
public class RevealAnimUtil {
public interface OnRevealAnimationListener {
void onRevealHide();
void onRevealShow();
}
// 显示
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void animateRevealShow(final Context context, final View view, final FloatingActionButton fab,
final int startRadius, @ColorRes final int color,
final OnRevealAnimationListener listener) {
// 获得开始焦点 fab 中心
int cx = (fab.getLeft() + fab.getRight()) / 2;
int cy = (fab.getTop() + fab.getBottom()) / 2;
float finalRadius = (float) Math.hypot(view.getWidth(), view.getHeight());
// 设置圆形显示动画
Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, startRadius, finalRadius);
anim.setDuration(500);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.INVISIBLE);
//切换代码在动画开始时执行比较好
listener.onRevealShow();
}
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
view.setBackgroundColor(ContextCompat.getColor(context, color));
view.setVisibility(View.VISIBLE);
}
});
anim.start();
}
// 圆圈凝聚效果
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void animateRevealHide(
final Context context, final View view,
final int finalRadius, @ColorRes final int color,
final OnRevealAnimationListener listener
) {
int cx = (view.getLeft() + view.getRight()) / 2;
int cy = (view.getTop() + view.getBottom()) / 2;
int initialRadius = view.getWidth();
// 与入场动画的区别就是圆圈起始和终止的半径相反
Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, initialRadius, finalRadius);
anim.setDuration(300);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
view.setBackgroundColor(ContextCompat.getColor(context, color));
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
listener.onRevealHide();
view.setVisibility(View.INVISIBLE);
}
});
anim.start();
}
}