Android Activity转场 -- 揭露动画

来源:互联网 发布:雷蛇淘宝假的 编辑:程序博客网 时间:2024/05/20 20:02

A     当前Activity

B     待启动Activity

        在A页面,启动B的时,需要在startActivity之后,添加A的自定义退场动画用来覆盖系统的动画overridePendingTransition(0,R.anim.home_close);
       调转到B页面,对根布局进行addOnLayoutChangeListenerj监听,并且在其中启动揭露动画

揭露动画代码

         注意需要在动画启动的时候,移除监听,不然有些机型会导致重复调用

/** * 开始动画 * * @param viewRoot */@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)private void animateRevealShow(final View viewRoot) {    if (isShowAnimation) {        return;    }    int finalRadius = (int) Math.sqrt(viewRoot.getWidth() * viewRoot.getWidth()            + viewRoot.getHeight() * viewRoot.getHeight());    Animator anim = ViewAnimationUtils.createCircularReveal(viewRoot, 0, 0, 0, finalRadius);    anim.setDuration(300);    anim.addListener(new AnimatorListenerAdapter() {        @Override        public void onAnimationStart(Animator animation) {            super.onAnimationStart(animation);            //动画开始后,移除监听,防止二次            isShowAnimation = true;        }        @Override        public void onAnimationEnd(Animator animation) {            super.onAnimationEnd(animation);            isShowAnimation = false;        }    });    anim.setInterpolator(new AccelerateInterpolator());    anim.start();}
   退出揭露动画

             需要重写onBackPressed方法,当动画执行完毕后,再进行关闭

/** * 退出动画 * * @param viewRoot */@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)private void animateRevealDisappear(final View viewRoot) {    if (isShowAnimation) {        return;    }    int finalRadius = (int) Math.sqrt(viewRoot.getWidth() * viewRoot.getWidth()            + viewRoot.getHeight() * viewRoot.getHeight());    Animator anim = ViewAnimationUtils.createCircularReveal(viewRoot, 0, viewRoot.getHeight(), finalRadius, 0);    anim.setDuration(300);    anim.addListener(new AnimatorListenerAdapter() {        @Override        public void onAnimationStart(Animator animation) {            super.onAnimationStart(animation);            isShowAnimation = true;        }        @Override        public void onAnimationEnd(Animator animation) {            super.onAnimationEnd(animation);            isShowAnimation = false;            //防止finish销毁不及时,引起闪屏,所以先隐藏布局            viewRoot.setVisibility(View.GONE);            finish();        }    });    anim.setInterpolator(new AccelerateInterpolator());    anim.start();}
@Overridepublic void finish() {    super.finish();    //设置退场动画覆盖系统的退场动画    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        overridePendingTransition(R.anim.nav_close, 0);    }}@Overridepublic void onBackPressed() {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        animateRevealDisappear(flAll);    } else {        super.onBackPressed();    }}@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)@Overridepublic void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {    animateRevealShow(flAll);}
最重要的是不要忘记设置主题,不然会有黑屏闪屏出现
windowIsTranslucent  窗体透明

<style name="AppThemeNav" parent="Theme.AppCompat.Light.NoActionBar">    <!-- Customize your theme here. -->  <!--  <item name="colorPrimary">@color/GREEN_NAV</item>    <item name="colorPrimaryDark">@color/GREEN_NAV</item>    <item name="colorAccent">@color/colorAccent</item>-->    <item name="windowNoTitle">true</item>    <item name="android:windowIsTranslucent">true</item>    <item name="android:windowBackground">@android:color/transparent</item>    <item name="android:windowActionBar">false</item>    <item name="toolbarStyle">@style/CustomToolbar</item></style>

阅读全文
0 0