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 】
相关推荐:
浏览器如何快速切换搜索引擎_在地址栏使用不同搜索引擎【搜索】
香港服务器建站指南:免备案优势与SEO优化技巧全解析
javascript中闭包概念与用法深入理解
Laravel如何实现用户角色和权限系统_Laravel角色权限管理机制
Laravel如何记录日志_Laravel Logging系统配置与自定义日志通道
非常酷的网站设计制作软件,酷培ai教育官方网站?
实现点击下箭头变上箭头来回切换的两种方法【推荐】
太平洋网站制作公司,网络用语太平洋是什么意思?
成都网站制作公司哪家好,四川省职工服务网是做什么用?
制作电商网页,电商供应链怎么做?
Laravel Blade组件怎么用_Laravel可复用视图组件的创建与使用
关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)
网站页面设计需要考虑到这些问题
如何快速搭建高效服务器建站系统?
Laravel怎么在Blade中安全地输出原始HTML内容
如何实现javascript表单验证_正则表达式有哪些实用技巧
Laravel如何使用软删除(Soft Deletes)功能_Eloquent软删除与数据恢复方法
Laravel如何设置自定义的日志文件名_Laravel根据日期或用户ID生成动态日志【技巧】
Laravel怎么实现软删除SoftDeletes_Laravel模型回收站功能与数据恢复【步骤】
详解Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点
手机软键盘弹出时影响布局的解决方法
Laravel用户认证怎么做_Laravel Breeze脚手架快速实现登录注册功能
laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法
简历在线制作网站免费版,如何创建个人简历?
google浏览器怎么清理缓存_谷歌浏览器清除缓存加速详细步骤
Laravel如何使用withoutEvents方法临时禁用模型事件
Laravel怎么生成URL_Laravel路由命名与URL生成函数详解
如何撰写建站申请书?关键要点有哪些?
Python高阶函数应用_函数作为参数说明【指导】
北京企业网站设计制作公司,北京铁路集团官方网站?
详解Android——蓝牙技术 带你实现终端间数据传输
如何获取上海专业网站定制建站电话?
js实现点击每个li节点,都弹出其文本值及修改
php做exe能调用系统命令吗_执行cmd指令实现方式【详解】
如何注册花生壳免费域名并搭建个人网站?
Laravel表单请求验证类怎么用_Laravel Form Request分离验证逻辑教程
如何为不同团队 ID 动态生成多个“认领值班”按钮
javascript读取文本节点方法小结
使用C语言编写圣诞表白程序
Laravel的路由模型绑定怎么用_Laravel Route Model Binding简化控制器逻辑
如何快速生成专业多端适配建站电话?
微博html5版本怎么弄发超话_超话进入入口及发帖格式要求【教程】
如何快速辨别茅台真假?关键步骤解析
高防服务器租用首荐平台,企业级优惠套餐快速部署
大连网站制作费用,大连新青年网站,五年四班里的视频怎样下载啊?
Laravel如何使用Livewire构建动态组件?(入门代码)
HTML透明颜色代码在Angular里怎么设置_Angular透明颜色使用指南【详解】
Laravel观察者模式如何使用_Laravel Model Observer配置
Laravel如何与Docker(Sail)协同开发?(环境搭建教程)
Android仿QQ列表左滑删除操作

