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


猜你喜欢
- 背景传统 SpringMVC 项目中,我们可以定义容器初始化 Servlet ,然后在 web.xml 配置该 Servlet ,指定 lo
- 解决C#中WebBrowser的DocumentCompleted事件不执行的实现方法 :使用WebBrowser的ProgressChan
- package com.jiucool.www.struts.action; import java.io.B
- 本文实例为大家分享了Android实现手势密码功能的具体代码,供大家参考,具体内容如下首先声明一下,九宫格布局是从网上扒了一个大神写好的,大
- 本文实例为大家分享了Java实现简单邮件发送的具体代码,供大家参考,具体内容如下需要的jar包:activation-1.1.1.jarma
- 传播inbound事件有关于inbound事件, 在概述中做过简单的介绍, 就是以自己为基准, 流向自己的事件, 比如最常见的channel
- Mybatis注解开发单表操作MyBatis的常用注解Mybatis也可以使用注解开发方式,这样我们就可以减少编写Mapper映射文件了。我
- 下面提供代码示例,请参考。public boolean onKeyDown(int keyCode, KeyEvent event) {&n
- 前面学习过过滤器, 但是过滤器是针对servlet的, 用在springmvc和spring boot里面, 功能上, 感觉并不是很好用.那
- 前言在计算机内存中,数据的存储方式都是以0和1的形式存储,也就是二进制的形式,数据是如何向内存写入的呢?整形数据以补码的形式存储,浮点型的存
- 1.RecycledPool的重用场景以及使用:多个RecyclerView出现,并且他们的item布局结构一致,这时候可以进行重用。在进行
- KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt提出的,因此人们称它为克努特—莫里斯—普
- 在很多系统开发中,我们希望在指定的方法调用之前或者之后能打印出该方法的调用时间以及方法的出参和入参,就可以使用spring的AOP,还可以结
- 整型变量基本语法格式int变量名= 初始值;代码示例int a = 10;int表示变量的类型是一个整型。在 Java 中, 一个int变量
- 写了一个人民币小写转大写的方法,Java版本,思路很简单,没有测出什么Bug,有bug欢迎反馈public class RMBChange
- rocketmq client 日志的问题处理使用rocketmq后,默认会在{user.home}\logs\rocketmqlogs 目
- 接上一篇文章:Android实现图片区域裁剪功能上一篇文章提及了通过调用系统相册或拍照来实现图片的缩放\裁剪。不过这对于笔者项目的要求同样不
- ***Source URL: http://i.yesky.com/bbs/jsp/view.jsp?articleID=889992&am
- 什么是事件?用户对组件的一个操作,称之为一个事件。事件源:能够产生事件的GUI组件对象。事件处理方法:能够接受,解析和处理事件类对象,实现与
- 1. 简单工厂模式简介简单工厂模式(Simple Factory),又被称为"静态工厂方法模式"。它属于"创建