Path布局+TranslateAnimation

提取了下PATH的菜单的那种动画效果。先看贴图,呵呵

原理:
点击红色加号触发事件:

public static void startAnimationsIn(ViewGroup viewgroup,int durationMillis) {
 
  for (int i = 0; i < viewgroup.getChildCount(); i++) {
    ImageButton inoutimagebutton = (ImageButton) viewgroup.getChildAt(i);
    inoutimagebutton.setVisibility(0);
 
    MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton.getLayoutParams();
    Animation animation = new TranslateAnimation(mlp.rightMargin-xOffset,0F,yOffset + mlp.bottomMargin, 0F);
 
//这个地方就是相对和绝对位置的区别,其实还有4个参数没有显示出来,但是她会根据单位自动识别。原句应该是: 
//new TranslateAnimation(Animation.ABSOLUTE,mlp.rightMargin - xOffset, Animation.RELATIVE_TO_SELF,0F,Animation.ABSOLUTE, yOffset + mlp.bottomMargin, Animation.RELATIVE_TO_SELF,0F);
 
    
    animation.setFillAfter(true);animation.setDuration(durationMillis);
    animation.setStartOffset((i * 100)
       / (-1 + viewgroup.getChildCount()));
     animation.setInterpolator(new OvershootInterpolator(2F));
     inoutimagebutton.startAnimation(animation);
   } 
}
 

发生移动动画出现。隐藏即为:

Animation animation = new TranslateAnimation(0F,mlp.rightMargin-xOffset, 0F,yOffset + mlp.bottomMargin);
 

其中xOffset yOffset由布局中首尾item距离屏幕边距的距离

private static int xOffset  = 15;
 private static int yOffset  = -13;
 public static void initOffset(Context context){//由布局文件
   xOffset  = (int) (10.667 *context.getResources().getDisplayMetrics().density);
   yOffset  = -(int) (8.667 *context.getResources().getDisplayMetrics().density);
 }

值得一提的是 interpolator的使用,PATH中使用了OvershootInterpolator以及AnticipateInterpolator。
interpolator 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果可以 accelerated(加速),decelerated(减速),repeated(重复),bounced(弹跳)等。
AccelerateDecelerateInterpolator 在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator  在动画开始的地方速率改变比较慢,然后开始加速
AnticipateInterpolator 开始的时候向后然后向前甩
AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值
BounceInterpolator   动画结束的时候弹起
CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的地方快然后慢
LinearInterpolator   以常量速率改变
OvershootInterpolator    向前甩一定值后再回到原来位置


另修改:添加了动画结束后将六个按钮焦点去掉的语句,防止阻挡到下面那一层的事件。添加了按钮item点击放大的动画。

原文地址:https://www.cnblogs.com/vus520/p/2561974.html