Android系统ApiDemo中发散动画核心代码

今天就不做解说了,日后再来完善

private void showMenuBar() {
        //设置动画时间
        int duration = 5000;
        //动画距离,屏幕宽度的60%
        float distance = getScreenWidth()*0.6f;//432

        //相邻ImageView运动角度式22.5度
        float angle1 = (float)(22.5f*Math.PI/180);
        float angle2 = (float)(45f*Math.PI/180);
        float angle3 = (float)(67.5f*Math.PI/180);

        //icon1
        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("TranslationX", 0f, -distance);
        //icon2
        PropertyValuesHolder p21 = PropertyValuesHolder.ofFloat("TranslationX", 0f, -(float)(distance*Math.cos(angle1)));
        PropertyValuesHolder p22 = PropertyValuesHolder.ofFloat("TranslationY", 0f, -(float)(distance*Math.sin(angle1)));
        //icon3
        PropertyValuesHolder p31 = PropertyValuesHolder.ofFloat("TranslationX", 0f, -(float)(distance*Math.cos(angle2)));
        PropertyValuesHolder p32 = PropertyValuesHolder.ofFloat("TranslationY", 0f, -(float)(distance*Math.sin(angle2)));
        //icon4
        PropertyValuesHolder p41 = PropertyValuesHolder.ofFloat("TranslationX", 0f, -(float)(distance*Math.cos(angle3)));
        PropertyValuesHolder p42 = PropertyValuesHolder.ofFloat("TranslationY", 0f, -(float)(distance*Math.sin(angle3)));
        //icon5
        PropertyValuesHolder p5 = PropertyValuesHolder.ofFloat("TranslationY", 0f, -distance);

        ObjectAnimator animator1 = ObjectAnimator.ofPropertyValuesHolder(mainBar1, p1).setDuration(duration);
        ObjectAnimator animator2 = ObjectAnimator.ofPropertyValuesHolder(mainBar2, p21, p22).setDuration(duration);
        ObjectAnimator animator3 = ObjectAnimator.ofPropertyValuesHolder(mainBar3, p31,p32).setDuration(duration);
        ObjectAnimator animator4 = ObjectAnimator.ofPropertyValuesHolder(mainBar4, p41,p42).setDuration(duration);
        ObjectAnimator animator5 = ObjectAnimator.ofPropertyValuesHolder(mainBar5, p5).setDuration(duration);

        //添加自由落体效果插值器
        animator1.setInterpolator(new BounceInterpolator());
        animator2.setInterpolator(new BounceInterpolator());
        animator3.setInterpolator(new BounceInterpolator());
        animator4.setInterpolator(new BounceInterpolator());
        animator5.setInterpolator(new BounceInterpolator());

        //启动动画
        animator1.start();
        animator2.start();
        animator3.start();
        animator4.start();
        animator5.start();
    }

    /**
     * 竖屏时获取屏幕宽度,横屏时,获取高度
     * @return
     */
    public int getScreenWidth(){
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        int x = outMetrics.widthPixels;
        int y = outMetrics.heightPixels;
        return x>y?y:x;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值