Android 实现Path2.0中绚丽的的旋转菜单

时间:2022-09-24 12:05:53

上图先:

Android 实现Path2.0中绚丽的的旋转菜单Android 实现Path2.0中绚丽的的旋转菜单

那么下面开始吧~

首先,将整个菜单动画分解开来。

1.       一级菜单按钮的旋转动画2个,十字和叉叉状态的转换。

2.       二级菜单按钮的平移动画2个,弹簧效果的in和out

3.       二级菜单按钮的点击效果,放大消失,其他未点击按钮缩小消失。

4.       一级菜单按钮的恢复效果,放大出现

好的 逐一去实现:

首先是一级菜单按钮的旋转动画,这2个动画可以直接在xml中定义,然后load到代码中来,具体代码如下:

rotate_story_add_button_in.xml

<?xml version="1.0" encoding="UTF-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:duration=""
android:fromDegrees="0.0"
android:toDegrees="-225.0"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:fillAfter="true"
android:fillEnabled="true"
/>

rotate_story_add_button_out.xml

<?xml version="1.0" encoding="UTF-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:duration=""
android:fromDegrees="-225.0"
android:toDegrees="0.0"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:fillAfter="true"
android:fillEnabled="true"
/>

这2段没什么好说的,定义好角度即可。

接下来是需要我们在代码中定义的动画部分,这几个动画的部分需要定义一个基类,作为统一的调用接口,这个基类被称作InOutAnimation,继承自AnimationSet,这个基类的主要工作是为view提供in和out两种不同的状态时的动画效果。其子类需要实现2个方法:

 protected abstract void addInAnimation(View aview[]);
protected abstract void addOutAnimation(View aview[]);

从而进行view的入场和离场动画。

下面是InOutAnimation的代码部分:

public abstract class InOutAnimation extends AnimationSet {

          public Direction        direction;

          public enum Direction {
IN, OUT;
} public InOutAnimation(Direction direction, long l, View[] aview) {
super(true);
this.direction = direction;
switch (this.direction) {
case IN:
addInAnimation(aview);
break;
case OUT:
addOutAnimation(aview); break;
}
setDuration(l);
} protected abstract void addInAnimation(View aview[]); protected abstract void addOutAnimation(View aview[]); }

接下来就是重头戏啦,二级菜单按钮的平移动画。

这部分动画看起来可能会比较复杂和神秘,其实不然,当把整个动画过程分解开来的时候,都是最最简单的平移而已,我们要做的只是定义一下平移的起点和终点、开始动画的顺序以及插值(Interpolator),让整个过程看起来很炫。

先说动画的起点和终点吧,起点很简单,就是整个view的左下角,即0,0点,为了效果漂亮一些,我们稍微的将左下角位置定义的有一些偏移,经验上的值是16,-13,这个点的位置看你心情而定咯~ 好 终点就是你想让他在的点上就好了,终点我们将定义到layout中去,为这个2级菜单指定一个margin的值就好。

还是上代码比较直观:

动画如下:

收缩部分:TranslateAnimation(xOffset + -mlp.leftMargin, 0F,yOffset + mlp.bottomMargin, 0F)
扩张部分:TranslateAnimation(0F, xOffset + -mlp.leftMargin, 0F,yOffset + mlp.bottomMargin)

位置定义部分:

例如:

android:layout_marginBottom="142dp"  android:layout_marginLeft="10.667dp"

这个位置大家可以直观的在布局文件中看到,详细的布局文件也将在下面展示。

以上是单独的每一个二级按钮的动画,而组合的动画就是指定了一下开始的时间差以及插值:

这个就是奥妙所在了,OvershootInterpolator AnticipateInterpolator 这2个插值器提供了弹力效果。

整段的代码如下:

ComposerButtonAnimation.java

public class ComposerButtonAnimation extends InOutAnimation {

          public static final int                  DURATION       = ;
private static final int xOffset = ;
private static final int yOffset = -; public ComposerButtonAnimation(Direction direction, long l, View view) {
super(direction, l, new View[] { view });
} public static void startAnimations(ViewGroup viewgroup,
InOutAnimation.Direction direction) {
switch (direction) {
case IN:
startAnimationsIn(viewgroup);
break;
case OUT:
startAnimationsOut(viewgroup);
break;
}
} private static void startAnimationsIn(ViewGroup viewgroup) {
for (int i = ; i < viewgroup.getChildCount(); i++) {
if (viewgroup.getChildAt(i) instanceof InOutImageButton) {
InOutImageButton inoutimagebutton = (InOutImageButton) viewgroup
.getChildAt(i);
ComposerButtonAnimation animation = new ComposerButtonAnimation(
InOutAnimation.Direction.IN, DURATION, inoutimagebutton);
animation.setStartOffset((i * )
/ (- + viewgroup.getChildCount()));
animation.setInterpolator(new OvershootInterpolator(2F));
inoutimagebutton.startAnimation(animation);
}
}
} private static void startAnimationsOut(ViewGroup viewgroup) {
for (int i = ; i < viewgroup.getChildCount(); i++) {
if (viewgroup.getChildAt(i) instanceof InOutImageButton) {
InOutImageButton inoutimagebutton = (InOutImageButton) viewgroup
.getChildAt(i);
ComposerButtonAnimation animation = new ComposerButtonAnimation(
InOutAnimation.Direction.OUT, DURATION,
inoutimagebutton);
animation.setStartOffset(( * ((- + viewgroup
.getChildCount()) - i))
/ (- + viewgroup.getChildCount()));
animation.setInterpolator(new AnticipateInterpolator(2F));
inoutimagebutton.startAnimation(animation);
}
}
} @Override
protected void addInAnimation(View[] aview) {
MarginLayoutParams mlp = (MarginLayoutParams) aview[]
.getLayoutParams();
addAnimation(new TranslateAnimation(xOffset + -mlp.leftMargin, 0F,
yOffset + mlp.bottomMargin, 0F));
} @Override
protected void addOutAnimation(View[] aview) {
MarginLayoutParams mlp = (MarginLayoutParams) aview[]
.getLayoutParams();
addAnimation(new TranslateAnimation(0F, xOffset + -mlp.leftMargin, 0F,
yOffset + mlp.bottomMargin));
}
}

剩下的增大出现、增大消失及缩小消失都是scale和alpha的组合动画

例如增大出现为:

 addAnimation(new ScaleAnimation(0F, 1F, 0F, 1F, , 0.5F, , 0.5F));
addAnimation(new AlphaAnimation(0F, 1F));

整段的代码如下:

public class ComposerButtonGrowAnimationIn extends InOutAnimation {

          public ComposerButtonGrowAnimationIn(int i) {
super(InOutAnimation.Direction.IN, i, new View[]);
} @Override
protected void addInAnimation(View[] aview) {
addAnimation(new ScaleAnimation(0F, 1F, 0F, 1F, , 0.5F, , 0.5F));
addAnimation(new AlphaAnimation(0F, 1F)); } @Override
protected void addOutAnimation(View[] aview) {} } public class ComposerButtonGrowAnimationOut extends InOutAnimation { public ComposerButtonGrowAnimationOut(int i) {
super(InOutAnimation.Direction.OUT, i, new View[]);
} @Override
protected void addInAnimation(View[] aview) {} @Override
protected void addOutAnimation(View[] aview) {
addAnimation(new ScaleAnimation(1F, 5F, 1F, 5F, , 0.5F, , 0.5F));
addAnimation(new AlphaAnimation(1F, 0F));
} } public class ComposerButtonShrinkAnimationOut extends InOutAnimation { public ComposerButtonShrinkAnimationOut(int i) {
super(InOutAnimation.Direction.OUT, i, new View[]);
} @Override
protected void addInAnimation(View[] aview) { } @Override
protected void addOutAnimation(View[] aview) {
addAnimation(new ScaleAnimation(1F, 0F, 1F, 0F, , 0.5F, , 0.5F));
addAnimation(new AlphaAnimation(1F, 0F));
} }

接下来我们需要为这些控件做一下扩展,以便其可以再动画完成后显示或消失。

很简单

public class InOutImageButton extends ImageButton {

          private Animation    animation;

          public InOutImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} public InOutImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
} public InOutImageButton(Context context) {
super(context);
} @Override
protected void onAnimationEnd() {
super.onAnimationEnd();
if ((this.animation instanceof InOutAnimation)) {
setVisibility(((InOutAnimation) this.animation).direction != InOutAnimation.Direction.OUT ? View.VISIBLE
: View.GONE);
}
} @Override
protected void onAnimationStart() {
super.onAnimationStart();
if ((this.animation instanceof InOutAnimation))
setVisibility(View.VISIBLE);
} @Override
public void startAnimation(Animation animation) {
super.startAnimation(animation);
this.animation = animation;
getRootView().postInvalidate();
}
}

那么到这里基本上就已经搞定了所有的事情了,剩下点没做的事就是把这些动画效果设置给对应的控件了.

代码:这里

Android 实现Path2.0中绚丽的的旋转菜单的更多相关文章

  1. Path2&period;0中绚丽的的旋转菜单

    我们看一下实现的效果图: 在上图中,我将菜单弹出的效果设置成直线型,最终的弹出或汇总点在下面的红色按钮中. 它的实现原理是设置动画的同时并利用动画中的插入器(interpolator)来实现弹力.主要 ...

  2. Android RxJava 2&period;0中backpressure&lpar;背压&rpar;概念的理解

    英文原文:https://github.com/ReactiveX/RxJava/wiki/Backpressure Backpressure(背压.反压力) 在rxjava中会经常遇到一种情况就是被 ...

  3. Android Studio 3&period;0 新特性

    最新Android Studio版本是Android Studio 3.0,本文提供了所有新功能和更改的摘要. 所有这些功能都可以在最新的金丝雀版本中发布,但beta测试版本可能尚未提供. 核心IDE ...

  4. Android Studio 3&period;0 下载 使用新功能介绍

    谷歌2017发布会更新了挺多内容的,而且也发布了AndroidStudio3.0预览版,一些功能先睹为快.(英语一般,有些翻译不太好) 下载地址 https://developer.android.g ...

  5. android圆形旋转菜单,而对于移动转换功能支持

    LZ该公司最近接手一个项目,需要写一个圆形旋转菜单,和菜单之间的移动换位支持,我本来以为这样的demo如若互联网是非常.想想你妈妈也帮不了我,空旋转,但它不能改变位置,所以LZ我们只能靠自己摸索. 最 ...

  6. android 4&period;0 中出错 java&period;lang&period;UnsupportedOperationException

    在android4.0中  画图的时候使用: canvas.clipPath(path, Region.Op.XOR); 报错 java.lang.UnsupportedOperationExcept ...

  7. Android 7&period;0 中 ContentProvider 实现原理

    欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:汪毅雄 导语: 本文描述了ContentProvider发布者和调用者这两在Framework层是如何实现的. 作为Android的四大 ...

  8. Android 6&period;0 中的 Wifi 连接

    Android 6.0 中的 Wifi 连接 这几天在写一个软件,结果被其中的 wifi 连接问题困扰了 3 天. 先描述下需求: usb 接口接了一根 usb2serial,通过这个接口接收命令 当 ...

  9. 在Android 5&period;0中使用JobScheduler

    在Android 5.0中使用JobScheduler 原文链接 : using-the-jobscheduler-api-on-android-lollipop 译者 : Mr.Simple 校对者 ...

随机推荐

  1. IRandomAccessStream&comma; IBuffer&comma; Stream&comma; byte&lbrack;&rsqb; 之间相互转换

    /* * 用于实现 IRandomAccessStream, IBuffer, Stream, byte[] 之间相互转换的帮助类 */ using System;using System.IO;us ...

  2. django(一)

    Django 自称是"最适合开发有限期的完美WEB框架".本文参考<Django web开发指南>,快速搭建一个blog 出来,在中间涉及诸多知识点,这里不会详细说明, ...

  3. 控制器View的加载和内存警告流程图

    控制器View的加载 内存警告

  4. vim操作集合

    Vim命令合集 命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filena ...

  5. Day05&lowbar;JAVA语言基础第五天

    1.函数(掌握) 1.概念(掌握) 定义在类中,有特定功能的一小段程序 2.格式(掌握) 修饰符 返回类型 函数名(参数类型 形式参数,...){ 函数体: return 返回值; } 解释: A 修 ...

  6. 使用WinSetupFromUSB来U盘安装WINDOWS2003

    今天用UltraISO制作WINDOWS2003的U盘的安装启动,在安装系统的时候发现错误提示“INF file txtsetup.sif is corrupt or missing .status ...

  7. NodeJs 开源

    iwebpp.io - 运行P2P Node.js web 服务,穿透防火墙,NAT https://github.com/InstantWebP2P/iwebpp.io pm 是一个轻量级的Node ...

  8. HDU 5867 Water problem

    处理出1-99的,之后的加上多少hundred和and即可.整百和一千的时候注意一下. #pragma comment(linker, "/STACK:1024000000,10240000 ...

  9. 小甲鱼Python第八讲课后习题

    0. 下面的循环会打印多少次"I Love FishC"?

  10. C&num; Restful 启用 Session

    虽然很多人说不建议启用,但我就是想启用. [ServiceContract(SessionMode=SessionMode.Allowed)] public interface IBIService ...