由于录制的gif过大,导致CSDN部分gif无法显示,可以传送到GitHub查看本篇博客
随着 Material Design设计概念的提出,使得很多的开发过程中对动画和UI的优化越来越重要,其中一个重要的动画
就是Material Deisgn : Material Motion
“Motion provides meaning. Objects are presented to the user without breaking the continuity of experience even as they transform and reorganize. Motion in the world of material design is used to describe spatial relationships, functionality, and intention with beauty and fluidity.”
可以看出Motion是多么的强大,但是在Android机型上使用起来并不是一番风顺
但是归功于Android碎片化,由于不能适配低版本的机型随意很多开发者放弃了这个强大的功能
,但是现在好了有了下面就是开源库 Transitions-Everywhere
,Transitions-Everywhere 向后移植到 Android 4.O ,并且兼容 Android 2.2 +
所以是时候学习转场动画
说了那么多虚无的概率,那转场动画到底是干啥的呢,咱们上一个效果大家就明白了:
其实上面执行的逻辑就是一个Textview的visible设置及时显示和隐藏功能,而且使用起来也很方便代码:
TransitionManager.beginDelayedTransition(llyout, new Slide(Gravity.LEFT));
tv.setVisibility(tv.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
实现这样的效果,只需要一行代码就可以,是不是很强大!那就开始咱们今天的学习
compile "com.andkulikov:transitionseverywhere:1.6.5"
android.transition.*
的替代为 com.transitionseverywhere.*
那有哪些现成的动画供我们使用呢
大家不要着急,下面一一为大家讲解这些动画的使用方法
源码链接
在textview显示很隐藏前调用动画方法,先初始化一个动画Transition
,这里使用的是Slide
动画初始化传入一个滑动方法值,beginDelayedTransition
方法中第一个参数设定
需要动画变量的父view布局对象
// Slide (滑行)
public void onFadeClick(int diction) {
Transition transition = new Slide(diction);
TransitionManager.beginDelayedTransition(llyout, transition);
tv.setVisibility(tv.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}
源码链接地址
通过EasyRecyclerView
实现一个gridview效果的界面,点击item后扩散每个item,当动画开始后设置移除adpter的数据,恢复的时候再讲数据添加回来
注意需要Explode的EpicenterCallback返回一个扩散的原点,这是使用的是点击对象的位置!
/**
* 开始的动画
*
* @param view
*/
public void onStartClick(View view) {
final Rect viewRect = new Rect();
view.getGlobalVisibleRect(viewRect);
TransitionSet set = new TransitionSet()
.addTransition(new Explode().setEpicenterCallback(new Transition.EpicenterCallback() {
@Override
public Rect onGetEpicenter(Transition transition) {
return viewRect;
}
}).excludeTarget(view, true));
TransitionManager.beginDelayedTransition(recyle, set);
open = !open;
if (open) {
btnClose.setVisibility(View.VISIBLE);
for (String str : getData()) {
adapter.remove(str);
}
} else {
btnClose.setVisibility(View.GONE);
adapter.addAll(getData());
}
}
源码链接地址
* 单一的fade动画
Transition transition=new Fade();
transition.setDuration(2000);
TransitionManager.beginDelayedTransition(llyout,transition );
tv.setVisibility(tv.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
Transition transition=new Scale(0.8f);
transition.setDuration(2000);
TransitionManager.beginDelayedTransition(llyout, transition);
tv.setVisibility(tv.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
TransitionSet set = new TransitionSet()
.addTransition(new Scale(0.9f))
.addTransition(new Fade());
set.setDuration(2000);
TransitionManager.beginDelayedTransition(llyout, set);
tv.setVisibility(tv.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
源码链接地址
TransitionManager.beginDelayedTransition(rlyout,
new ChangeBounds().setDuration(1000));
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) btnPath.getLayoutParams();
params.gravity = open ? Gravity.TOP : Gravity.BOTTOM;
btnPath.setLayoutParams(params);
TransitionManager.beginDelayedTransition(llyoutData, new ChangeBounds().setDuration(1000));
llyoutData.removeAllViews();
Collections.shuffle(list);
for (int i = 0; i < list.size(); i++) {
String item = list.get(i);
TextView tv =(TextView) View.inflate(this, R.layout.llyout_item, null);
tv.setText(item);
TransitionManager.setTransitionName(tv, item);
llyoutData.addView(tv);
}
源码链接地址
TransitionManager.beginDelayedTransition(llyout, new Recolor().setDuration(1000));
if (colorTag) {
tv.setTextColor(getResources().getColor(R.color.drak));
tv.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimaryDark)));
} else {
tv.setTextColor(getResources().getColor(R.color.colorPrimary));
tv.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.drak)));
}
TransitionManager.beginDelayedTransition(llyout, new ChangeText().setChangeBehavior(ChangeText
.CHANGE_BEHAVIOR_OUT_IN).setDuration(1000));
txTag = !txTag;
if (txTag) {
tv.setText("文字我改了");
} else {
tv.setText("文字又复原了");
}
TransitionManager.beginDelayedTransition(llyout, new Rotate().setDuration(1000));
roteTag = !roteTag;
if (roteTag) {
img.setRotation(90);
tv.setRotation(180);
} else {
img.setRotation(0);
tv.setRotation(0);
}
配置 Transitions 也非常容易,你可以给一些特殊目标的 View 指定 Transitions,仅仅只有它们才能有动画
github源码地址