这几天做了个带动画的分享页面。如今把它分享出来,假设你认为实用,请直接使用,避免反复造*
先看下效果图
认为仅仅是看效果图不明显。那么用手机扫描以下的二维码下载安装包:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAOKUlEQVR4Xu3dXW4cSQ4EYPlmA5/Ae9LVzQbwBbzQPCwWWHV+iY6istoOv7KCP0FGMqtbkr/99f37r7ff9N+/39+jyv7140eEF1j5Kb7wip/6F17xX8H+rQJ53KbpAdCAK77wGsDUv/CK/wr2CmTRpekB0IArvvAawNS/8Ir/CvYKpAJ5yIAEWIG8gsQXOarBKm96AJSf4guf1if/yk/xX8HeDdIN0g2ymIEKpAKpQJ4ViFbs6RWpFX86f+U3zd90/apvOn7Kn/L/8L/cIK9e4On8dxqQNnmFn65f9U3HT7lT/hVIyjDwOw2YTGF6QFXfdPyUO+VfgaQMVyBLBiqQ4QGTe50Apxuk/FRfap+uX/VNx0/5Uf7dICnD3SDdIMMzFLnXCXD6BFN+UfEb4On6Vd90/A0Klo8o/26QlOFukG6QFQM7CkxmUCeQ4guv3ORfeNnT/ORfdtWn/Kbxyl/2NP94g4ggFSB7WqDwin+6PuWX2lWf+JvGp/Wl+Vcgh69IamA6IMJPD7jqU3zlL/sV8aNv0u9eoAgSwafrU36pXfWJv2l8Wl+afzdIN0j0kl2B4He6RdDpE0AniPI7XZ/yS+2qT/xN49P60vy7QbpBukEwA30HWRCkE3L6hEv9C6/60hM4xSt/2a+I/0cLZHpA0gYKr/yFlz0dsBSv/GS/In4FsmD5CoJXTZR/DUAFsmZI/O7wV4FUIA8ZSAcsxeuAkP2K+BVIBVKBLGagAqlAKpAK5HMGdAe9YkX3HeQxA+JfVyjZr+hfN0g3SDdIN0g3iE7bz+zpCZzin8n5fzFXxO8GGdwgVzTo5BUtzT/FVyBgICX41fEaENUnvN4B5H8ar/xlT/P/8N8N0g3Sd5C+g5x5B7niBOsVS3visf0K/rtBukG6QbpBukE+Y2D6HeKK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0MxpgOdaAy7/wil+BVCA7M/L0MxpgOdaAy7/wil+BVCA7M/L0MxpgOdaAy7/wil+BVCA7M/L0MxpgOdaAy7/wil+BVCA7M/L0MxpgOdaAy7/wil+BHBaIGpQ2OB0g4dP8T/vf4bc/i7Xo8g6BqyGZHgANqOKrPuEV/+7+lV83SDfIkoEKpL9yuxyQnROmG+QxA+LvtACVXzdIN0g3CGag7yB9B3nIwOkTfvodpxvk/V0c94q1YKAC6TtIBVKBLGcgumJFx/MFYK1InYDCpylOx5d/5a/6p/0rvzS+/Kv++CVdCUzbVaAIFj7Nfzq+/Ct/1T/tX/ml8eVf9Vcg+I1EESy7GrzToJMfIyt/1X+6vivy6xVLLAZ2DdjpAVJ85S9q5F/4NL787+RXgYjFwK4G7zSoGyRoAKA7/Fcgc/y/VSA/InbFX+T87e2tAsH3IDsEJU1Qg9P48q/cFX/av/JL48u/6u9Lel/SlzOUDujOAE5eIccFogB3t6vBaqDwql/+hZd9Oj/5V33Cq77T/rlBVMDd7WrQHRqQcKj65DutP8VP55f6r0Bu/pcZ1eAKZP5n7ZafYqlBd7drgE6fgCl/qk/+0/pT/HR+qf9ukG6Q6CW9ApEEb27XCXu6wSl9qk/+0/pT/HR+qf9ukG6QbhCoqO8gC4KmT2idcLJP5yf/3SDq0M3td29wSp/qk/90wFP8dH6p/w/8fwCyF+tKAekSGQAAAABJRU5ErkJggg==" alt="这里写图片描写叙述" title="">
这个效果本身没有什么特别的难度,都是用Animator实现的,仅仅是动画效果的代码量有点多。由于分享模块一般都会做,把这个分享出来,想要用的话,就不要反复写这么多行代码了
对于熟悉Animator的人。以下的代码能够略过了
我来看下基本的实现代码,完整的代码看底部的项目Github地址
1. 我用了一个方法。实现进入的效果,加上判定,是否须要隐藏二维码页面
private void moveInAnim(boolean isHideCode) {
ObjectAnimator friendAnimatorX = ObjectAnimator.ofFloat(tvFriend, "TranslationX", 0);
ObjectAnimator friendAnimatorY = ObjectAnimator.ofFloat(tvFriend, "TranslationY", 0);
ObjectAnimator timelineAnimatorX = ObjectAnimator.ofFloat(tvTimeline, "TranslationX", 0);
ObjectAnimator timelineAnimatorY = ObjectAnimator.ofFloat(tvTimeline, "TranslationY", 0);
ObjectAnimator qrcodeAnimatorX = ObjectAnimator.ofFloat(tvQrcode, "TranslationX", 0);
ObjectAnimator qrcodeAnimatorY = ObjectAnimator.ofFloat(tvQrcode, "TranslationY", 0);
ObjectAnimator copyAnimatorX = ObjectAnimator.ofFloat(tvCopylink, "TranslationX", 0);
ObjectAnimator copyAnimatorY = ObjectAnimator.ofFloat(tvCopylink, "TranslationY", 0);
AnimatorSet set = new AnimatorSet();
set.setDuration(ANIM_TIME);
if (isHideCode) {
ObjectAnimator animatorX = ObjectAnimator.ofFloat(tvCode, "ScaleX", 0.1f);
ObjectAnimator animatorY = ObjectAnimator.ofFloat(tvCode, "ScaleY", 0.1f);
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
tvCode.setVisibility(View.INVISIBLE);
}
});
set.playTogether(friendAnimatorX, friendAnimatorY, timelineAnimatorX, timelineAnimatorY
, qrcodeAnimatorX, qrcodeAnimatorY, copyAnimatorX, copyAnimatorY, animatorX, animatorY);
} else {
set.setInterpolator(new FastOutSlowInInterpolator());
set.playTogether(friendAnimatorX, friendAnimatorY, timelineAnimatorX, timelineAnimatorY
, qrcodeAnimatorX, qrcodeAnimatorY, copyAnimatorX, copyAnimatorY);
}
set.start();
}
在Animator中实现x轴跟Y轴,还有缩放的效果。有心人可能会发现,为什么我移动的參数都是0f呢,那么起始的值跑到哪里去了,请接着看第二点。
2. 初始化位置
tvFriend.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
tvFriend.getViewTreeObserver().removeOnPreDrawListener(this);
tvFriend.setTranslationX(-screenWidth / 2);
tvFriend.setTranslationY(-tvFriend.getHeight() * 2);
return false;
}
});
tvTimeline.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
tvTimeline.getViewTreeObserver().removeOnPreDrawListener(this);
tvTimeline.setTranslationX(screenWidth / 2);
tvTimeline.setTranslationY(-tvFriend.getHeight() * 2);
return false;
}
});
tvQrcode.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
tvQrcode.getViewTreeObserver().removeOnPreDrawListener(this);
tvQrcode.setTranslationX(-screenWidth / 2);
tvQrcode.setTranslationY(tvFriend.getHeight() * 2);
return false;
}
});
tvCopylink.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
tvCopylink.getViewTreeObserver().removeOnPreDrawListener(this);
tvCopylink.setTranslationX(screenWidth / 2);
tvCopylink.setTranslationY(tvFriend.getHeight() * 2);
return false;
}
});
初始化位置的方法,要放在addOnPreDrawListener中,这就是为什么在上一点中,仅仅要把值设成0f就能够的原因
3. 移动出去的动画
private void moveOutAnim(boolean isFinishActivity, boolean isShowCode) {
ObjectAnimator friendAnimatorX = ObjectAnimator.ofFloat(tvFriend, "TranslationX", -screenWidth / 2);
ObjectAnimator friendAnimatorY = ObjectAnimator.ofFloat(tvFriend, "TranslationY", -tvFriend.getHeight() * 2);
ObjectAnimator timelineAnimatorX = ObjectAnimator.ofFloat(tvTimeline, "TranslationX", screenWidth / 2);
ObjectAnimator timelineAnimatorY = ObjectAnimator.ofFloat(tvTimeline, "TranslationY", -tvFriend.getHeight() * 2);
ObjectAnimator qrcodeAnimatorX = ObjectAnimator.ofFloat(tvQrcode, "TranslationX", -screenWidth / 2);
ObjectAnimator qrcodeAnimatorY = ObjectAnimator.ofFloat(tvQrcode, "TranslationY", tvFriend.getHeight() * 2);
ObjectAnimator copyAnimatorX = ObjectAnimator.ofFloat(tvCopylink, "TranslationX", screenWidth / 2);
ObjectAnimator copyAnimatorY = ObjectAnimator.ofFloat(tvCopylink, "TranslationY", tvFriend.getHeight() * 2);
AnimatorSet set = new AnimatorSet();
set.setDuration(ANIM_TIME);
if (isShowCode) {
addQrcode();
ObjectAnimator animatorScaleX = ObjectAnimator.ofFloat(tvCode, "ScaleX", 1f);
ObjectAnimator animatorScaleY = ObjectAnimator.ofFloat(tvCode, "ScaleY", 1f);
animatorScaleX.setInterpolator(overshootInterpolator);
animatorScaleY.setInterpolator(overshootInterpolator);
set.playTogether(friendAnimatorX, friendAnimatorY, timelineAnimatorX, timelineAnimatorY
, qrcodeAnimatorX, qrcodeAnimatorY, copyAnimatorX, copyAnimatorY, animatorScaleX, animatorScaleY);
} else {
set.playTogether(friendAnimatorX, friendAnimatorY, timelineAnimatorX, timelineAnimatorY
, qrcodeAnimatorX, qrcodeAnimatorY, copyAnimatorX, copyAnimatorY);
}
if (isFinishActivity) {
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
finish();
overridePendingTransition(0, 0);
}
});
}
set.start();
}
跟移动进来的动画大同小异,我就不复述了
4. 刚開始打开页面的时候,启动动画的方法:
tvFriend.post(new Runnable() {
@Override
public void run() {
moveInAnim(false);
}
});
用post的方法,能够非常好的实现一開始的动画,假设直接在onResume中调用,或者其它的地方调用。就会出现卡顿,至于为什么这种方法能够。或者说有没有更好的的方法,希望有熟悉handle之类的人来解释下,感谢了
5. 注意的地方
有些手机会有默认的页面切换动画,比方我的M2手机就是向左滑动。退出就是向右滑动,为了避免有些手机的默认动画,能够在启动页面和退出页面的代码后面,加上例如以下的代码,避免出现默认的切换效果
overridePendingTransition(0, 0);
项目的源码我放到了Github。欢迎查看!
有不论什么疑问或者探讨了,欢迎留言或者联系我