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

作者:超神的菠萝 时间:2023-03-03 03:45:50 

前言

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

效果图如下

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>

来源:http://www.jianshu.com/p/3c4fd56c7984

标签:android,小球移动,circularreveal
0
投稿

猜你喜欢

  • Spring Boot JPA Repository之existsBy查询方法失效的解决

    2023-01-27 18:36:10
  • Java程序员面试中的多线程问题总结

    2021-12-12 07:48:33
  • c#获取本机的IP地址的代码

    2021-08-21 08:37:07
  • SpringBoot集成SpringSecurity和JWT做登陆鉴权的实现

    2023-01-29 09:34:57
  • c#和javascript函数相互调用示例分享

    2023-10-08 21:33:52
  • Springboot项目快速实现过滤器功能

    2023-08-11 18:30:02
  • spring项目中切面及AOP的使用方法

    2021-12-01 21:11:29
  • 详解Java volatile 内存屏障底层原理语义

    2023-05-08 19:25:47
  • opencv利用视频的前n帧求平均图像

    2021-06-20 11:43:02
  • Android自定义viewGroup实现点击动画效果

    2022-09-01 00:27:07
  • SpringBoot部署在tomcat容器中运行的部署方法

    2023-08-04 13:02:28
  • Android自定义控件实现底部菜单(下)

    2023-03-28 15:35:38
  • Java基本数据类型(动力节点java学院整理)

    2022-09-26 12:14:13
  • Android解决所有双击优化的问题

    2023-07-27 09:07:03
  • 腾讯开源消息中间件TubeMQ总体介绍分析

    2022-06-21 05:10:03
  • 详解Java8 CompletableFuture的并行处理用法

    2023-07-27 11:48:06
  • Java静态代理和动态代理总结

    2022-01-02 18:49:21
  • Java四种权限修饰符知识点详解

    2023-11-11 06:12:59
  • SpringBoot + SpringSecurity 短信验证码登录功能实现

    2022-10-16 10:26:25
  • java实现微信小程序登录态维护的示例代码

    2023-08-22 18:29:46
  • asp之家 软件编程 m.aspxhome.com