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

所属分类: 软件编程 / Android 阅读数: 27
收藏 0 赞 0 分享

前言

本文主要给大家介绍了关于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中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多