80行代码搞定菜单展开动画

其实实现动画效果是非常简单的,下面就使用ObjectAnimator来实现一个点击按钮向下展开菜单项的动画。


制作布局文件

首先我们要把我们的图片素材全部放到到一个帧布局中,将菜单键放在最上面。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
    <ImageView
        android:id="@+id/imageView_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/b"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        />

    <ImageView
        android:id="@+id/imageView_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/c"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        />

    <ImageView
        android:id="@+id/imageView_d"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/d"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        />

    <ImageView
        android:id="@+id/imageView_e"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/e"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        />

    <ImageView
        android:id="@+id/imageView_f"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/f"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        />

    <ImageView
        android:id="@+id/imageView_g"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/g"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        />
    <ImageView
        android:id="@+id/imageView_h"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/h"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        />
    <ImageView
        android:id="@+id/imageView_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/a"
        android:paddingLeft="2dp"
        android:paddingTop="2dp"
        />
</FrameLayout>

那么接下来要做的就是把这几个菜单项目b~h依次展开。


制作动画

接下来就要分别用一个int数组来保存我们的图片id,和一个list保存我们的imageview,在onCreate方法中用for循环将id与view对象依次关联起来。给触发按钮注册监听事件。然后调用startAnim 方法。

private void startAnim()
    {
        for (int i = 1; i < resId.length ; i++)
        {
            ObjectAnimator animator=ObjectAnimator.ofFloat(imageViewList.get(i),"translationY",0F,i*150);
            animator.setDuration(500);
            animator.setStartDelay(i*300);
            animator.setInterpolator(new BounceInterpolator());
            animator.start();
        }
        flag=false;
    }

这里的flag时标志你展开还是关闭,如果为false,调用closeAnim 方法

private void closeAnim()
    {
        for (int i = resId.length-1; i >0 ; i--)
        {
            ObjectAnimator animator=ObjectAnimator.ofFloat(imageViewList.get(i),"translationY",i*150,0F);
            animator.setDuration(500);
            animator.setStartDelay(i*300);
            animator.setInterpolator(new BounceInterpolator());


            animator.start();
        }
    }

效果如下:


这里写图片描述

这里写图片描述
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,554评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,612评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 文|水管爆了 我与爱情只有一墙之隔 他们赠我阳光雨露 殷切的我却只有蔷薇 他常常扮演着猛虎 我只能紧握带血的蔷薇 ...
    一日蜉蝣君阅读 344评论 1 1
  • 中华文化博大精深,我们也一定可以从中找到或优雅或沧桑的词句来作为日常的感叹词。
    雪舞天寒月阅读 177评论 0 0