之前看Material Design的时候跟着写的几个效果,是关于CoordinatorLayout里面的一些控件动画的使用,记录一下使用方法
FloatingActionButton跟随Snackbar移动
布局代码:
java代码
final FloatingActionButton fab_1= (FloatingActionButton) findViewById(R.id.fab_1);
fab_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(fab_1,"哈哈",Snackbar.LENGTH_SHORT).show();
}
});
Toolbar跟随RecyclerView消失
布局代码
java代码
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("哈哈");
制造toolbar的折叠效果
布局代码
java代码
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("Title");
第四种:制造视差效果
布局代码
java代码
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.maincollapsing);
collapsingToolbar.setTitle("Title");
FloatingActionBar随RecyclerView的消失和出现
布局代码
java代码 ScrollAwareFABBehavior
package com.example.tyhj.pull;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||
super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,
nestedScrollAxes);
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child,
View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
dyUnconsumed);
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
child.hide();
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
child.show();
}
}
}
java代码 Activity
CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.maincollapsing);
collapsingToolbar.setTitle("Title");
其中ScrollAwareFABBehavior
是一个自定义的类,此处只是一个默认的消失和出现的动画,通过这个类应该可以自己设计出自己想要的各种控件的效果,用法是在xml的FloatingActionBar(其他应该也是可以)中添加如下代码:
app:layout_behavior="com.example.tyhj.pull.ScrollAwareFABBehavior"
如果这个自定义类用下面的代码,自定义了两个动画效果,会实现滑入和滑出的效果,好看一些:
package com.example.tyhj.pull;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
public class ScrollAwareFABBehavior2 extends FloatingActionButton.Behavior {
private static final android.view.animation.Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
private boolean mIsAnimatingOut = false;
public ScrollAwareFABBehavior2(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||
super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,
nestedScrollAxes);
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child,
View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
dyUnconsumed);
if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) {
animateOut(child);
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
animateIn(child);
}
}
private void animateOut(final FloatingActionButton button) {
ViewCompat.animate(button).translationY(500)
.setInterpolator(INTERPOLATOR).withLayer()
.setListener(new ViewPropertyAnimatorListener() {
@Override
public void onAnimationStart(View view) {
ScrollAwareFABBehavior2.this.mIsAnimatingOut = true;
}
@Override
public void onAnimationEnd(View view) {
ScrollAwareFABBehavior2.this.mIsAnimatingOut = false;
view.setVisibility(View.GONE);
}
@Override
public void onAnimationCancel(View view) {
ScrollAwareFABBehavior2.this.mIsAnimatingOut = false;
}
}).start();
}
private void animateIn(FloatingActionButton button) {
button.setVisibility(View.VISIBLE);
ViewCompat.animate(button).translationY(0)
.setInterpolator(INTERPOLATOR).withLayer().setListener(null)
.start();
}
}
项目地址:https://github.com/tyhjh/Coordinator-Layout