Android实现翻转及延迟动画效果
龙旋
共 6608字,需浏览 14分钟
· 2021-09-26
只需要翻转背景FrameLayout180度(注意:此时整个页面是反转的),
然后把当前的RelativeLayout也翻转180度(当前的页面被翻转成正确的方向了),
最后把要显示的RelativeLayout逆向翻转180度(要显示的页面也被翻转成正确的方向了)。
/**
* 翻转动画
*
* @param context 当前上下文,提供给setCameraDistance()使用
* @param backgroundView 背景View
* @param frontView 当前处于前面的View
* @param behindView 想要翻转显示的View
*/
public static void setFlipAnimation(Context context, View backgroundView,
View frontView, View behindView) {
setCameraDistance(context, backgroundView, backgroundView);
setCameraDistance(context, frontView, behindView);
setAlphaAnimation(frontView, 1, 0, 0, 500, false);
//必须让backgroundView和frontView同时翻转 否则会出现异常
setRotateAnimation(backgroundView, ROTATION_Y, 0,
180, 0, 1000, false);
setRotateAnimation(frontView, ROTATION_Y, 0, 180,
0, 1000, false);
setRotateAnimation(behindView, ROTATION_Y, 0, -180,
0, 1000, false);
setAlphaAnimation(behindView, 0, 1,
500, 500, true);
}
/**
* 调整相机距离,避免旋转的时候高度差变化引起视觉效果差
*
* @param context 当前上下文
* @param frontView 正面的view
* @param behindView 背面的view
*/
private static void setCameraDistance(Context context, View frontView, View behindView) {
int distance = 16000;
float scale = context.getResources().getDisplayMetrics().density * distance;
frontView.setCameraDistance(scale);
behindView.setCameraDistance(scale);
}
/**
* 透明动画
* 最后一个isVisible是为了防止alpha为0时控件依然可被点击
*/
public static void setAlphaAnimation(final View view, float startAlpha,
float endAlpha, long delayMills,
long duration, boolean isVisible) {
ObjectAnimator animator;
animator = ObjectAnimator.ofFloat(view, "alpha", startAlpha, endAlpha);
animator.setDuration(duration).setStartDelay(delayMills);
animator.start();
//透明度动画伴随着页面是否可见,否则前一个页面仍然有点击事件
//如果为true,即要显示的页面,如果为false,即要隐藏的页面
if (isVisible) {
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
view.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
} else {
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
/**
* 平移动画
* translationDistance指的是应该平移多少距离
*/
public static void setTranslationAnimation(View view, String translation,
float startTranslation, float translationDistance,
long delayMills, long duration,boolean isOvershoot) {
ObjectAnimator animator;
animator = ObjectAnimator.ofFloat(view, translation, startTranslation,
translationDistance).setDuration(duration);
animator.setStartDelay(delayMills);
if (isOvershoot){
animator.setInterpolator(new OvershootInterpolator(1.5f));
}
animator.start();
}
/**
* 旋转动画,其中isOvershoot代表是否有回弹效果
*/
public static void setRotateAnimation(View view, String rotate, float startAngle,
float endAngle, long delayMills,
long duration, boolean isOvershoot) {
ObjectAnimator animator;
if (ROTATION_X.equals(rotate)) {
animator = ObjectAnimator.ofFloat(view, ROTATION_X, startAngle, endAngle);
} else if (ROTATION_Y.equals(rotate)) {
animator = ObjectAnimator.ofFloat(view, ROTATION_Y, startAngle, endAngle);
} else {
throw new RuntimeException("rotate can not be else string," +
" except 'rotationY,rotationX'");
}
if (isOvershoot) {
animator.setInterpolator(new OvershootInterpolator(1.5f));
}
animator.setDuration(duration).setStartDelay(delayMills);
animator.start();
}
/**
* 设置移动延迟动画,类似于每个控件依次入场或退场动画
*
* @param isVisible 控件是否可见 参考setAlphaAnimation解释
* @param eachDelay 每一个控件进场或退场的延迟时间
* @param eachDuration 每一个控件的duration
* @param views 控件可变长参数,按顺序输入依次进场或退场
*/
public static void setMoveDelayAnimation(String translation,
float startTranslation, float endTranslation,
float startAlpha, float endAlpha, boolean isVisible,
long firstDelay, long eachDelay, long eachDuration,
@NonNull View... views) {
int i = 0;
for (View view : views) {
if (i == 0) {
setTranslationAnimation(view, translation,
startTranslation, endTranslation, firstDelay, eachDuration,true);
setAlphaAnimation(view, startAlpha, endAlpha, firstDelay, eachDuration, isVisible);
} else {
long delayMills = eachDelay * i + firstDelay;
setTranslationAnimation(view, translation,
startTranslation, endTranslation, delayMills, eachDuration,true);
setAlphaAnimation(view, startAlpha, endAlpha, delayMills, eachDuration, isVisible);
}
i++;
}
}
在使用时仅需:
//登录信息页面依次从右往左出现
PocketAnimation.setMoveDelayAnimation(PocketAnimation.TRANSLATION_X,
SizeUtils.dp2px(100), 0, 1, true,
1000, 100, 500,
mSignInLayoutLogin, mSignInLayoutAccount, mSignInLayoutPassword,
mSignInLayoutSignIn, mSignInLayoutSignUp, mSignInLayoutForgetPassword);
嗯。这样动画就easy完成了!
源码地址:
https://github.com/512DIDIDI/Pocket
评论
用 Shader 实现旗帜飘扬动画效果
我觉得对于刚入门 3D 编程的朋友来说,如果能够完成代码创建模型数据->创建材质->编写Shader动画这一系列,想必会有满满的成就感。今天就用 Cocos Creator 的 utils.MeshUtils.createMesh 接口,带大家感受一下这个流程。这个流程不仅可以用于新手学
COCOS
2
乐普心安宝及心电图机,助力安康市搭建“心电一张网”,打通全域“生命线”!
为持续推动胸痛中心建设,助力全民健康,全面提升心血管疾病等急危重症救治能力水平。4月20日,由安康市卫健委主办、安康市中医医院承办的“第七届心血管汉江学术会议暨安康市胸痛中心大会”在高新国际会议中心顺利举行。市人大常委会主任王彪、市政协副主席唐纹、市政府党组成员刘英华等领导亲临现场,受邀参会的中国科
乐普医疗AI
0
SpringBoot+Minio实现上传凭证、分片上传、秒传和断点续传
关注我们,设为星标,每天7:40不见不散,架构路上与您共享回复架构师获取资源大家好,我是你们的朋友架构君,一个会写代码吟诗的架构师。Spring Boot整合Minio后,前端的文件上传有两种方式:1、文件上传到后端,由后端保存到Minio这种方式好处是完全由后端集中管理,可以很好的做到、身份验证、
Java架构师社区
0
超越原生,散点图实现华夫饼图
之前我们介绍过了如何使用新卡片图实现华夫饼图。参考:超越原生,PowerBI 华夫饼图实现但是利用卡片图实现的华夫饼图有一些缺点,形状之间的大小跟间距不太好把握,而且有时形状大一点的话显示就会不正常,需要做出二次调整。今天给大家介绍一种原生视觉对象生成华夫饼图的更佳方案,既简单又美观。上图是利用散点
PowerBI战友联盟
2
全新 SOTA backbone | 2024年了,再见ViT系列Backbone,实数难得,不知道效果如何?
点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达在构建用于精确匹配的深度固定长度表示时,确定指纹上的密集特征点,特别是在像素 Level 上,具有重大意义。为了探索指纹匹配的可解释性,作者提出了一种多阶段可解释的指纹匹配网络,名为通过视觉 Transformer 进行指纹匹配的
小白学视觉
10
Spring Boot + flowable 快速实现工作流
关注我们,设为星标,每天7:40不见不散,架构路上与您共享回复架构师获取资源大家好,我是你们的朋友架构君,一个会写代码吟诗的架构师。来源:blog.csdn.net/zhan107876/article/details/120815560总览一、flowable-ui部署运行二、绘制流程图绘图细节:
Java架构师社区
0
实现订单30分钟自动取消的策略
原文:juejin.cn/post/7285167401821798400简介在电商和其他涉及到在线支付的应用中,通常需要实现一个功能:如果用户在生成订单后的一定时间内未完成支付,系统将自动取消该订单。本文将详细介绍基于Spring Boot框架实现订单30分钟内未支付自动取消的几种方案,并提供实例
JAVA乐园
0
AI大模型之路 第三篇:从零实现词嵌入模型,加深理解!
你好,我是郭震今天我们研究「AI大模型第三篇」:词维度预测,很多读者听过词嵌入,这篇文章解答下面问题:词嵌入是什么意思?怎么做到的?原理是什么?从零实现一个专属你数据集的词嵌入我们完整从零走一遍,根基的东西要理解透,这样才能发明出更好的东西。1 skip-gram模型Skip-gram模型是一种广泛
Python与算法社区
11