原来PATH的菜单效果如此简单。布局+TranslateAnimation搞定

来源:互联网 发布:淘宝超市怎么开 编辑:程序博客网 时间:2024/06/02 12:15
本帖最后由 shenallan 于 2012-2-29 16:05 编辑

提取了下PATH的菜单的那种动画效果。先看贴图
1.png
2012-

源码: PathMenu.zip(1.3 MB, 下载次数: 1139)
 


效果APK: PathMenu.apk(466.77 KB, 下载次数: 163)


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

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

  10. animation.setFillAfter(true);animation.setDuration(durationMillis);
  11. animation.setStartOffset((i * 100)
  12. / (-1 + viewgroup.getChildCount()));
  13. animation.setInterpolator(new OvershootInterpolator(2F));
  14. inoutimagebutton.startAnimation(animation);

  15. }
  16. }
复制代码
发生移动动画出现。隐藏即为
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);
}
如下图
IMG_20120224_132333.jpg
2012-2-24 13:24 上传
下载附件(1.49 MB)

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


感谢小熊屁屁不辞辛劳的努力,添加了按钮item点击放大的动画
代码 PathMenu.zip(1.3 MB, 下载次数: 1054)

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