灵动菜单
实现思路
把菜单项目放在菜单按钮同一位置, 当点击带单按钮时候, 每个菜单项开始动画, 从原来位置,移动到指定位置
代码
布局文件:
展开菜单:
private void startMenu(){
ObjectAnimator mainAnimator = ObjectAnimator.ofFloat(ivMain,"alpha",1f,0.5f);
float leftCurrentX = ivLeft.getTranslationX();
ObjectAnimator leftAnimator = ObjectAnimator.ofFloat(ivLeft,"translationX",leftCurrentX,-200f);
float rightCurrentX = ivRight.getTranslationX();
ObjectAnimator rightAnimator = ObjectAnimator.ofFloat(ivRight,"translationX",rightCurrentX,200f);
float topCurrentY = ivTop.getTranslationY();
ObjectAnimator topAnimator = ObjectAnimator.ofFloat(ivTop,"translationY",topCurrentY,-200f);
float bottomCurrentY = ivBottom.getTranslationY();
ObjectAnimator bottomAnimator = ObjectAnimator.ofFloat(ivBottom,"translationY",bottomCurrentY,200f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(mainAnimator,leftAnimator,rightAnimator,topAnimator,bottomAnimator);
animatorSet.setDuration(500);
animatorSet.setInterpolator(new BounceInterpolator());
animatorSet.start();
flag = false;
}
关闭菜单:
private void closeMenu(){
ObjectAnimator mainAnimator = ObjectAnimator.ofFloat(ivMain,"alpha",0.5f,1f);
float leftCurrentX = ivLeft.getTranslationX();
ObjectAnimator leftAnimator = ObjectAnimator.ofFloat(ivLeft,"translationX",leftCurrentX, 0f);
float rightCurrentX = ivRight.getTranslationX();
ObjectAnimator rightAnimator = ObjectAnimator.ofFloat(ivRight,"translationX",rightCurrentX,0f);
float topCurrentY = ivTop.getTranslationY();
ObjectAnimator topAnimator = ObjectAnimator.ofFloat(ivTop,"translationY",topCurrentY,0f);
float bottomCurrentY = ivBottom.getTranslationY();
ObjectAnimator bottomAnimator = ObjectAnimator.ofFloat(ivBottom,"translationY",bottomCurrentY,0f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(mainAnimator,leftAnimator,rightAnimator,topAnimator,bottomAnimator);
animatorSet.setDuration(500);
animatorSet.setInterpolator(new BounceInterpolator());
animatorSet.start();
flag = true;
}
对主菜单按钮监听
ivMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (flag) startMenu();
else closeMenu();
}
});
下拉展开动画
实现思路
两个LinearLayout, 一个visibility为显示,另一个为gone. 当点击showVIew时,把隐藏的LinearLayout设置为visibility,对这个过程使用动画.
代码
布局文件:
获取ValueAnimator:
private ValueAnimator createValueAnimator(final View view, int start, int end){
final ValueAnimator animator = ValueAnimator.ofInt(start,end);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int value = (int)valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = value;
view.setLayoutParams(params);
}
});
return animator;
}
展开动画:
private void openAnimation(View view){
view.setVisibility(View.VISIBLE);
ValueAnimator valueAnimator = createValueAnimator(view,0,measureHiddenViewHeight);
valueAnimator.start();
}
关闭动画:
private void closeAnimation(final View view) {
int origHeight = view.getHeight();
ValueAnimator valueAnimator = createValueAnimator(view,origHeight,0);
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
}
});
valueAnimator.start();
}