码迷,mamicode.com
首页 > 其他好文 > 详细

动态菜单

时间:2016-05-18 19:03:11      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

动态菜单

先上效果图

技术分享

比较简单,主要就是属性动画的使用和坐标角度的小细节。

实现

实现效果:
图标按照路径一路缩放渐变过来即可。

核心代码

    /**
     * Item开启动画
     *
     * @param btnItem
     * @param index
     * @param total
     * @param radius
     */
    private void btnItemStartAnimator(View btnItem, int index, int total, int radius) {
        if (btnItem.getVisibility() != View.VISIBLE) {
            btnItem.setVisibility(View.VISIBLE);
        }
        double degree = Math.toRadians(90) / (total - 1) * index;//Math中根据度数得到弧度值的函数
        int translationX = -(int) (radius * Math.sin(degree));
        int translationY = -(int) (radius * Math.cos(degree));

        AnimatorSet set = new AnimatorSet();
        //实现平移缩放和透明动画
        set.playTogether(
                ObjectAnimator.ofFloat(btnItem, "translationX", 0, translationX),
                ObjectAnimator.ofFloat(btnItem, "translationY", 0, translationY),
                ObjectAnimator.ofFloat(btnItem, "scaleX", 0, 1),
                ObjectAnimator.ofFloat(btnItem, "scaleY", 0, 1),
                ObjectAnimator.ofFloat(btnItem, "alpha", 0, 1)
        );
        set.setInterpolator(new BounceInterpolator());

        set.setDuration(500).start();
    }


   /**
     * Item关闭动画
     *
     * @param btnItem
     * @param index
     * @param total
     * @param radius
     */
    private void btnItemCloseAnimator(View btnItem, int index, int total, int radius) {

        double degree = Math.PI * index / ((total - 1) * 2);
        int translationX = -(int) (radius * Math.sin(degree));
        int translationY = -(int) (radius * Math.cos(degree));
        AnimatorSet set = new AnimatorSet();
        //包含平移、缩放和透明度动画
        set.playTogether(
                ObjectAnimator.ofFloat(btnItem, "translationX", translationX, 0),
                ObjectAnimator.ofFloat(btnItem, "translationY", translationY, 0),
                ObjectAnimator.ofFloat(btnItem, "scaleX", 1f, 0f),
                ObjectAnimator.ofFloat(btnItem, "scaleY", 1f, 0f),
                ObjectAnimator.ofFloat(btnItem, "alpha", 1f, 0f));
        set.setDuration(500).start();

        if (btnItem.getVisibility() == View.VISIBLE) {
            btnItem.setVisibility(View.INVISIBLE);
        }
    }

item开启动画和关闭动画为一个逆过程,体现在x,y距离变化上。

技术分享

x,y的距离开启时距离逐渐增长

        ObjectAnimator.ofFloat(btnItem, "translationX", 0, translationX),
        ObjectAnimator.ofFloat(btnItem, "translationY", 0, translationY),

这里要注意下sin这些弧度的计算,可以使用Math.toRadins(数字)

        double degree = Math.toRadians(90) / (total - 1) * index;//Math中根据度数得到弧度值的函数
        int translationX = -(int) (radius * Math.sin(degree));

或者使用PI=180°来折算

         double degree = Math.PI * index / ((total - 1) * 2);
         int translationX = -(int) (radius * Math.sin(degree));

项目地址传送门

动态菜单

标签:

原文地址:http://blog.csdn.net/xsf50717/article/details/51419258

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!