Android实现移动小球和CircularReveal页面切换动画实例代码

发布时间 - 2026-01-11 03:15:10    点击率:

前言

本文主要给大家介绍了关于Android如何实现移动小球和CircularReveal页面切换动画的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

效果图如下

是在fragment中跳转activity实现的效果,fragment跳fragment,activity跳activity类似~~

实现过程

  • 重写FloatingActionButton的onTouchListener()方法,使小球可以移动,并判断边界
  • 点击fab时记录坐标传到下一个页面,在下一个页面展示动画。
  • 点击后退或者重写onBackPressed()方法,执行动画

重写Fab的onTouchListener()

 floatingActionButton.setOnTouchListener(new View.OnTouchListener() {
  @Override
  public boolean onTouch(View view, MotionEvent ev) {
  switch (ev.getAction()) {
   case MotionEvent.ACTION_DOWN:
   downX = ev.getX();
   downY = ev.getY();
   isClick = true;
   break;
   case MotionEvent.ACTION_MOVE:
   isClick = false;
   moveX = ev.getX();
   moveY = ev.getY();

   int offsetX = (int) (moveX - downX);
   int offsetY = (int) (moveY - downY);

   //这里使用了setTranslation来移动view。。。尝试过layout。不知道为什么fragment切换回来的时候会恢复原位
   floatingActionButton.setTranslationX(floatingActionButton.getTranslationX() + offsetX);
   floatingActionButton.setTranslationY(floatingActionButton.getTranslationY() + offsetY);

   break;
   case MotionEvent.ACTION_UP:
   //用来触发点击事件
   if (isClick) {
    startAct();
    return false;
   }
   //用来判断移动边界

   if (floatingActionButton.getX() < 0) {
    floatingActionButton.setX(0);
   }
   if (floatingActionButton.getX() + floatingActionButton.getWidth() > ScreenUtil.getScreenWidth(getContext())) {
    floatingActionButton.setX(ScreenUtil.getScreenWidth(getContext()) - floatingActionButton.getWidth());
   }
   if (floatingActionButton.getY() < titleHeight) {
    floatingActionButton.setY(0);
   }
   if (floatingActionButton.getY() + floatingActionButton.getHeight() + titleHeight >
    getActivity().findViewById(R.id.activity_main_mainLl).getHeight() - getActivity().findViewById(R.id.fc_rg).getHeight()) {
    floatingActionButton.setY(getBottomY());
   }

   break;
  }
  return true;
  }

  private void startAct() {
  //跳转Activity,传递动画参数
  Intent intent = new Intent(getActivity(), CheckWorkActivity.class);
  intent.putExtra("x", (int) floatingActionButton.getX() + floatingActionButton.getWidth() / 2);
  intent.putExtra("y", (int) floatingActionButton.getY() + floatingActionButton.getHeight() / 2);
  intent.putExtra("start_radius", floatingActionButton.getWidth() / 2);
  intent.putExtra("end_radius", DialogFragment.this.view.getHeight());
  startActivity(intent);
  }
 });

在下一个页面中实现CircleRevel动画

onCrete中调用

 private void initAnimation() {
 //ll为根布局
 final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
 linearLayout.post(new Runnable() {
  @Override
  public void run() {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   Animator animator = ViewAnimationUtils.createCircularReveal(
    linearLayout,// 操作的视图
    getIntent().getIntExtra("x", 0), // 动画的中心点X
    getIntent().getIntExtra("y", 0) + findViewById(R.id.title).getHeight(), // 动画的中心点Y
    getIntent().getIntExtra("start_radius", 0), // 动画半径
    getIntent().getIntExtra("end_radius", 0)  // 动画结束半径
   );
   animator.setInterpolator(new AccelerateInterpolator());
   animator.setDuration(500);
   animator.start();
  }
  }
 });
 }

点击后退或者触发onBackPressed时候调用

 private void endAnim() {
 final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  Animator animator = ViewAnimationUtils.createCircularReveal(
   linearLayout,// 操作的视图
   getIntent().getIntExtra("x", 0),
   getIntent().getIntExtra("y", 0) + findViewById(R.id.title).getHeight(),
   getIntent().getIntExtra("end_radius", 0),
   getIntent().getIntExtra("start_radius", 0)

  );
  animator.setInterpolator(new AccelerateInterpolator());
  animator.setDuration(500);
  animator.addListener(new AnimatorListenerAdapter() {
  @Override
  public void onAnimationEnd(Animator animation) {
   super.onAnimationEnd(animation);
   finish();
  }
  });
  animator.start();
 }
 }

还有一个重要的地方是修改两个activity的theme

 <style name="AppThemeCircleRevel" parent="Theme.AppCompat.Light.NoActionBar">
 <!-- Customize your theme here. -->
 <item name="colorPrimary">@color/colorPrimary</item>
 <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
 <item name="colorAccent">@color/blue</item>

 <item name="android:windowAnimationStyle">@null</item>
 <item name="android:windowBackground">@android:color/transparent</item>
 <item name="android:windowIsTranslucent">true</item>
 <item name="android:colorBackgroundCacheHint">@null</item>
 </style>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。


# android  # 小球随机移动  # 页面切换动画  # circularreveal  # Android绘制跟随手指移动的小球  # Android自定义圆形View实现小球跟随手指移动效果  # Android实现拖动小球跟随手指移动效果  # Android自定义控件实现随手指移动的小球  # Android实现随手指移动小球  # 重写  # 中心点  # 跳转  # 是在  # 相关内容  # 说了  # 不多  # 有一定  # 给大家  # 还有一个  # 这篇文章  # 谢谢大家  # 不知道为什么  # 如何实现  # 使用了  # 有疑问  # switch  # break  # moveY  # false 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel如何使用Service Provider服务提供者_Laravel依赖注入与容器绑定【深度】  Edge浏览器提示“由你的组织管理”怎么解决_去除浏览器托管提示【修复】  简单实现jsp分页  企业网站制作这些问题要关注  android nfc常用标签读取总结  Laravel如何使用软删除(Soft Deletes)功能_Eloquent软删除与数据恢复方法  Bootstrap整体框架之JavaScript插件架构  轻松掌握MySQL函数中的last_insert_id()  Laravel如何实现全文搜索_Laravel Scout集成Algolia或Meilisearch教程  电商网站制作多少钱一个,电子商务公司的网站制作费用计入什么科目?  Laravel如何连接多个数据库_Laravel多数据库连接配置与切换教程  Zeus浏览器网页版官网入口 宙斯浏览器官网在线通道  如何快速查询域名建站关键信息?  猎豹浏览器开发者工具怎么打开 猎豹浏览器F12调试工具使用【前端必备】  Python企业级消息系统教程_KafkaRabbitMQ高并发应用  Laravel模型关联查询教程_Laravel Eloquent一对多关联写法  Laravel Artisan命令怎么自定义_创建自己的Laravel命令行工具完全指南  百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏  武汉网站设计制作公司,武汉有哪些比较大的同城网站或论坛,就是里面都是武汉人的?  高端网站建设与定制开发一站式解决方案 中企动力  如何快速上传自定义模板至建站之星?  如何获取PHP WAP自助建站系统源码?  网站制作报价单模板图片,小松挖机官方网站报价?  Laravel如何为API生成Swagger或OpenAPI文档  微信小程序 canvas开发实例及注意事项  如何在景安服务器上快速搭建个人网站?  Laravel怎么上传文件_Laravel图片上传及存储配置  如何安全更换建站之星模板并保留数据?  Laravel storage目录权限问题_Laravel文件写入权限设置  Windows10电脑怎么查看硬盘通电时间_Win10使用工具检测磁盘健康  大学网站设计制作软件有哪些,如何将网站制作成自己app?  如何用低价快速搭建高质量网站?  公司门户网站制作公司有哪些,怎样使用wordpress制作一个企业网站?  Win11怎么修改DNS服务器 Win11设置DNS加速网络【指南】  Laravel怎么实现观察者模式Observer_Laravel模型事件监听与解耦开发【指南】  微信小程序 scroll-view组件实现列表页实例代码  如何用PHP工具快速搭建高效网站?  利用vue写todolist单页应用  百度输入法ai面板怎么关 百度输入法ai面板隐藏技巧  七夕网站制作视频,七夕大促活动怎么报名?  Laravel怎么发送邮件_Laravel Mail类SMTP配置教程  Laravel集合Collection怎么用_Laravel集合常用函数详解  Laravel DB事务怎么使用_Laravel数据库事务回滚操作  如何快速登录WAP自助建站平台?  Laravel中的withCount方法怎么高效统计关联模型数量  JS弹性运动实现方法分析  品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?  javascript中的数组方法有哪些_如何利用数组方法简化数据处理  北京网站制作的公司有哪些,北京白云观官方网站?  Swift开发中switch语句值绑定模式