仿红包打开动效

“有一次,老师给学生们出了一个问题:“如果把你在这里学到的一切知识都变成一只小鸟,你会怎么样?”学生们纷纷想了很久,终于一个学生举手回答:“我会把它放飞!”老师很赞赏地点了点头,然后问其他学生:“你们呢?”接着,另一个学生回答:“我会把它吃掉!”老师听到后非常惊讶:“你为什么要吃掉这只小鸟?”学生笑着说:“因为知识就是力量,吃掉它就能得到力量!”

红包打开特性,先上效果:动画很简单,就是红包的上下两部分采用平移动画,实现一个open的样子,因为动画实现后要保留实现后的状态,所以使用了属性动画(ValueAnimator)来实现。移动的高度随布局计算。

属性动画(Property Animation)包括ValueAnimator和ObjectAnimator

ValueAnimator 是 属性动画的基类,它是对一个值进行平滑的过渡

public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
    ObjectAnimator anim = new ObjectAnimator(target, propertyName);
    anim.setFloatValues(values);
    return anim;
}

动画的类型支持:

缩放(scaleX、scaleY):缩放和移动相似,也分为沿x、y轴来放缩。沿x轴缩放使用scaleX,沿y轴缩放使用scaleY
后面的参数表示倍数,1f表示原来的大小,以此推类:2f表示两倍、3f表示三倍
如:1f-> 2f,放大成原来的两倍;2f-> 1f,从两倍变为原样、

旋转(rotation):下个度数大于上个度数,顺时针旋转;下个度数小于上个度数,逆时针旋转。

平移(translationX、translationY):这里的移动分为沿x、y轴移动,沿x轴时使用translationX,沿y轴移动使用translationY
translationX:下个位置大于上个上个位置时,向右移动,反之向左移动;
translationY:下个位置大于上个上个位置时,向下移动,反之向上移动

透明度(alpha):0~1f

具体实现:(如果直接getTop()或者getHeight()始终为0,则需要在onGlobalLayout方法中获取。)

 mlTopLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    mlTopLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    mlTopLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
                // get width and height of the view

                float top =mlTopLayout.getTop();
                ObjectAnimator.ofFloat(mlTopLayout, "translationY", 0, -top)
                        .setDuration(mDuration).start();
            }
        });
       mlBottomLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    mlBottomLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    mlBottomLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
                double bottom = (rlParentLayout.getHeight() - mlBottomLayout.getBottom());
                ObjectAnimator.ofFloat(mlBottomLayout, "translationY", 0,
                        (float) bottom).setDuration(mDuration).start();
            }
        });
        mlMoney.postDelayed(new Runnable() {
            @Override
            public void run() {
                mlMoney.setVisibility(View.VISIBLE);

            }
        }, 1000);
  • 18
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现仿微信打开红包旋转动画,可以通过以下步骤实现: 1. 创建一个红包图标 2. 点击红包图标后,弹出一个对话框 3. 在对话框中,创建一个ImageView,用来展示旋转的动画 4. 创建一个RotateAnimation对象,设置旋转角度和旋转时长 5. 将RotateAnimation对象设置给ImageView,启动旋转动画 6. 在动画结束后,关闭对话框,显示红包已经被打开的状态 下面是一个简单的示例代码,可以用作参考: ```java // 创建一个红包图标 ImageView redPacketIcon = new ImageView(context); redPacketIcon.setImageResource(R.drawable.red_packet_icon); // 点击红包图标后,弹出一个对话框 redPacketIcon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 创建对话框 final Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.red_packet_dialog); // 在对话框中,创建一个ImageView,用来展示旋转的动画 final ImageView rotateView = (ImageView) dialog.findViewById(R.id.rotate_view); // 创建一个RotateAnimation对象,设置旋转角度和旋转时长 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(2000); // 旋转2秒 // 将RotateAnimation对象设置给ImageView,启动旋转动画 rotateView.startAnimation(rotateAnimation); // 在动画结束后,关闭对话框,显示红包已经被打开的状态 rotateAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { dialog.dismiss(); Toast.makeText(context, "红包已经被打开", Toast.LENGTH_SHORT).show(); } @Override public void onAnimationRepeat(Animation animation) { } }); dialog.show(); } }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值