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

这篇博客介绍了如何在Android中实现类似IOS Game Center的气泡漂浮动画,通过PropertyValuesHolder结合Keyframe设置多个动画,包括平移和旋转效果。提供了一个名为`bubbleFloat`的静态方法,用于创建并返回气泡漂浮的ObjectAnimator对象,可以自定义动画时长、幅度、重复次数。
摘要由CSDN通过智能技术生成

网上没找到,于是自己写了一个,简单的算法,利用 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(

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 实现漂浮窗提示框的方法如下: 1. 在 AndroidManifest.xml 文件添加 SYSTEM_ALERT_WINDOW 权限: ```xml <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> ``` 2. 创建一个 Service 类,用于显示漂浮窗提示框: ```java public class FloatingWindowService extends Service { private WindowManager mWindowManager; private View mFloatingView; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); // 创建一个 WindowManager 对象 mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); // 创建一个 View 对象,作为漂浮窗提示框的界面 mFloatingView = LayoutInflater.from(this).inflate(R.layout.floating_window, null); // 设置漂浮窗提示框的位置和大小 WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); params.gravity = Gravity.TOP | Gravity.START; params.x = 0; params.y = 100; // 将漂浮窗提示框添加到 WindowManager mWindowManager.addView(mFloatingView, params); } @Override public void onDestroy() { super.onDestroy(); // 在 Service 销毁时,移除漂浮窗提示框 if (mFloatingView != null) { mWindowManager.removeView(mFloatingView); } } } ``` 3. 在布局文件定义漂浮窗提示框的界面,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_notification" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一条漂浮窗提示框" android:textColor="#FFFFFF" /> </LinearLayout> ``` 4. 在 Activity 启动 FloatingWindowService: ```java Intent intent = new Intent(this, FloatingWindowService.class); startService(intent); ``` 这样就可以在 Android 应用实现漂浮窗提示框了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值