Android实现气泡漂浮动画,类似IOS Game Center中气泡动画

网上没找到,于是自己写了一个,简单的算法,利用 PropertyValuesHolder实现多个动画的集合,不多说,直接上代码吧,非常好理解:

/**
     * 气泡漂浮动画
     * @param view
     * @param duration  动画运行时间
     * @param offset    动画运行幅度
     * @param repeatCount   动画运行次数
     * @return
     */
    public static ObjectAnimator bubbleFloat(View view, int duration, int offset, int repeatCount) {
        float path = (float) (Math.sqrt(3)/2*offset);
        PropertyValuesHolder translateX = PropertyValuesHolder.ofKeyframe(View.TRANSLATION_X,
                Keyframe.ofFloat(0f, 0),
                Keyframe.ofFloat(1/12f, offset / 2),
                Keyframe.ofFloat(2/12f, path),
                Keyframe.ofFloat(3/12f, offset),
                Keyframe.ofFloat(4/12f, path),
                Keyframe.ofFloat(5/12f, offset / 2),
                Keyframe.ofFloat(6/12f, 0),
                Keyframe.ofFloat(7/12f, -offset / 2),
                Keyframe.ofFloat(8/12f, -path),
                Keyframe.ofFloat(9/12f, -offset),
                Keyframe.ofFloat(10/12f, -path),
                Keyframe.ofFloat(11/12f, -offset/2),
                Keyframe.ofFloat(1f, 0)
        );

        PropertyValuesHolder translateY = PropertyValuesHolder.ofKeyframe(View.TRANSLATION_Y,
                Keyframe.ofFloat(0f, 0),
                Keyframe.ofFloat(1/12f, offset-path),
                Keyframe.ofFloat(2/12f, offset/2),
                Keyframe.ofFloat(3/12f, offset),
                Keyframe.ofFloat(4/12f, offset*3/2),
                Keyframe.ofFloat(5/12f, offset+path),
                Keyframe.ofFloat(6/12f, offset*2),
                Keyframe.ofFloat(7/12f, offset+path),
                Keyframe.ofFloat(8/12f, offset*3/2),
                Keyframe.ofFloat(9/12f, offset),
                Keyframe.ofFloat(10/12f, offset/2),
                Keyframe.ofFloat(11/12f, offset-path),
                Keyframe.ofFloat(1f, 0)
        );

        PropertyValuesHolder rotateX = PropertyValuesHolder.ofKeyframe(View.ROTATION_X,
                Keyframe.ofFloat(0f, 0),
                Keyframe.ofFloat(1/12f, offset / 2),
                Keyframe.ofFloat(2/12f, path),
                Keyframe.ofFloat(3/12f, offset),
                Keyframe.ofFloat(4/12f, path),
                Keyframe.ofFloat(5/12f, offset / 2),
                Keyframe.ofFloat(6/12f, 0),
                Keyframe.ofFloat(7/12f, -offset / 2),
                Keyframe.ofFloat(8/12f, -path),
                Keyframe.ofFloat(9/12f, -offset),
                Keyframe.ofFloat(10/12f, -path),
                Keyframe.ofFloat(11/12f, -offset/2),
                Keyframe.ofFloat(1f, 0)
        );

        PropertyValuesHolder rotateY = PropertyValuesHolder.ofKeyframe(View.ROTATION_Y,
                Keyframe.ofFloat(0f, 0),
                Keyframe.ofFloat(1/12f, offset / 2),
                Keyframe.ofFloat(2/12f, path),
                Keyframe.ofFloat(3/12f, offset),
                Keyframe.ofFloat(4/12f, path),
                Keyframe.ofFloat(5/12f, offset / 2),
                Keyframe.ofFloat(6/12f, 0),
                Keyframe.ofFloat(7/12f, -offset / 2),
                Keyframe.ofFloat(8/12f, -path),
                Keyframe.ofFloat(9/12f, -offset),
                Keyframe.ofFloat(10/12f, -path),
                Keyframe.ofFloat(11/12f, -offset/2),
                Keyframe.ofFloat(1f, 0)
        );

        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, translateX, translateY, rotateX, rotateY).
                setDuration(duration);
        animator.setRepeatCount(repeatCount);
        animator.setInterpolator(new LinearInterpolator());
        return animator;
    }

使用方法:

bubbleFloat(view, 3000, 10, -1);


Have a nice day!

转载于:https://my.oschina.net/u/2265357/blog/599101

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 Android动画框架来实现从小变大气泡动画效果。下面是一个简单的示例代码: 1. 首先,在您的 XML 布局文件添加一个显示气泡的 View,如下所示: ``` <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/bubble" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/bubble" android:layout_centerInParent="true" /> </RelativeLayout> ``` 2. 接下来,使用 ScaleAnimation 类来创建一个缩放动画,使气泡从小变大。以下是示例代码: ``` ImageView bubble = findViewById(R.id.bubble); Animation anim = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); anim.setDuration(500); bubble.startAnimation(anim); ``` 在这个示例代码,我们使用了 ScaleAnimation 类来创建一个从 0 到 1 的缩放动画,同时指定了气泡心点为其自身的心点。动画的持续时间为 500 毫秒。 3. 最后,为了让气泡从小变大,您需要在 Activity 或 Fragment 的 onCreate() 方法调用上述动画代码。以下是示例代码: ``` public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView bubble = findViewById(R.id.bubble); Animation anim = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); anim.setDuration(500); bubble.startAnimation(anim); } } ``` 这样,当您运行应用程序时,您应该会看到一个从小变大的气泡动画效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值