Android Activity Transition Animation

前言

久未放晴的天空
依旧留着你的笑容
哭过 却无法掩埋歉疚

简介

Activity Transition Animation主要实现的是Activity间的动画切换,即我们常说的场景动画,主要有:

  • slide(位移)
  • fade(渐变)
  • explode(扩散)
  • Activity shared elements(Activity view间动画切换)

Slide

这里写图片描述

ContactActivity.java
在onCreate()方法中添加动画

  private void setSlideAnimation() {
      Slide exitTransition = new Slide(Gravity.TOP);
      exitTransition.setDuration(500);
      //设置Activity退出动画
      getWindow().setExitTransition(exitTransition);
      Slide reenterTransition = new Slide(Gravity.TOP);
      reenterTransition.setDuration(500);
      //设置状态栏不执行动画 false为默认执行
      reenterTransition.excludeTarget(android.R.id.statusBarBackground, true);
      //设置Activity再次进入时动画
      getWindow().setReenterTransition(reenterTransition);
  }

ContactDetailActivity.java

  private void setSlideAnimation() {
      Slide enterTransition = new Slide(Gravity.BOTTOM);
      enterTransition.setDuration(500);
      enterTransition.excludeTarget(android.R.id.statusBarBackground, true);
      //设置Activity进入动画
      getWindow().setEnterTransition(enterTransition);
      Slide returnTransition = new Slide(Gravity.BOTTOM);
      returnTransition.setDuration(500);
      returnTransition.excludeTarget(android.R.id.statusBarBackground, true);
      getWindow().setReturnTransition(returnTransition);
  }

Activity跳转

  Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) mContext).toBundle();
  Intent intent = new Intent(mContext, ContactDetailActivity.class);
  ActivityCompat.startActivity(mContext, intent, bundle);

Fade

这里写图片描述

ContactDetailActivity.java

  private void setFadeAnimation() {
      Fade fade = new Fade();
      fade.setDuration(500);
      getWindow().setEnterTransition(fade);
      getWindow().setReturnTransition(fade);
  }

Explode

这里写图片描述

ContactActivity.java

  private void setExplodeAnimation() {
      Explode explode = new Explode();
      explode.setDuration(500);
      getWindow().setExitTransition(explode);
      getWindow().setReenterTransition(explode);
  }

ContactDetailActivity.java

  private void setExplodeAnimation() {
      Explode explode = new Explode();
      explode.setDuration(500);
      getWindow().setEnterTransition(explode);
      getWindow().setReturnTransition(explode);
  }

Shared elements between activities

这里写图片描述

item_contact_list.xml
指定android:transitionName属性

  <TextView
      android:id="@+id/contact_name_textView"
      android:layout_width="match_parent"
      android:layout_height="54dp"
      android:background="@drawable/contact_item_press_ripple"
      android:gravity="center_vertical"
      android:paddingLeft="14dp"
      android:textColor="#333333"
      android:transitionName="@string/contact_transition_name"
      android:textSize="16sp" />

activity_contact_detail.xml

  <TextView
      android:id="@+id/content_text_view"
      android:layout_width="wrap_content"
      android:layout_height="60dp"
      android:layout_marginLeft="14dp"
      android:gravity="center_vertical"
      android:textColor="#333333"
      android:textSize="40sp"
      android:transitionName="@string/contact_transition_name" />

ContactAdapter.java

  String transitionName = mContext.getResources().getString(R.string.contact_transition_name);
  Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) mContext,
          holder.contactNameTextView, transitionName).toBundle();
  Intent intent = new Intent(mContext, ContactDetailActivity.class);
  intent.putExtra(ContactDetailActivity.TEXT_TAG, model.getName());
  ActivityCompat.startActivity(mContext, intent, bundle);

ContactDetailActivity.java
监听Activity动画结束后展示ImageView Reveal效果

  private void setFadeAnimation() {
      Fade fade = new Fade();
      fade.setDuration(500);
      getWindow().setEnterTransition(fade);
      getWindow().setReturnTransition(fade);
      fade.addListener(new Transition.TransitionListener() {
          @Override
          public void onTransitionStart(Transition transition) {
          }
          @Override
          public void onTransitionEnd(Transition transition) {
              createRevealAnimation();
          }
          @Override
          public void onTransitionCancel(Transition transition) {
          }
          @Override
          public void onTransitionPause(Transition transition) {
          }
          @Override
          public void onTransitionResume(Transition transition) {
          }
      });                                                     
  }      

  private void createRevealAnimation() {
      mImageView.setVisibility(View.VISIBLE);
      int width = mImageView.getWidth();
      int height = mImageView.getHeight();
      int radius = Math.max(width, height);
      int centerX = width / 2;
      int centerY = height / 2;
      Animator animator = ViewAnimationUtils.createCircularReveal(mImageView, centerX, centerY, 0, radius);
      animator.setDuration(1000);
      animator.start();
  }                                                         

源码链接

https://github.com/kuangxiaoguo0123/ActivityTransition

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值