Getting Started with Activity & Fragment Transitions

译自:http://www.androiddesignpatterns.com/2014/12/activity-fragment-transitions-in-android-lollipop-part1.html

这系列的博文将会对Transition稍作回顾,并介绍在android5.0 Lollipop中引进的新的Activity & Fragment Transitions Api。该系列的博文将会以如下的方式进行呈现。

  • Part1:Getting Started with Activity & Fragment Transitions
  • Part 2: Content Transitions In-Depth
  • Part 3a: Shared Element Transitions In-Depth
  • Part 3b: Postponed Shared Element Transitions
  • Part 3c: Implementing Shared Element Callbacks (coming soon!)
  • Part 4: Activity & Fragment Transition Examples (coming soon!)

直到我写到part4,我将会将所有的知识进行整合,并形成一个工程,将之托管至here

那么,我们先来回答以下的问题:what is Transition?

What is Transtion

在android Lollippop中,将Activity和Fragment的transition放置在一个相对新的特性上,我们称之为Transtions。在android 4.4 Kitkat中,transition framework提供了在不同的UI之间进行变换的方便的api。该framework是建立在两个重要且主要的概念上面的:scenes和transitions。一个scene定义了一个UI界面,显而易见,transition定义了在scenes之间的动画。

当scene变化的时候,transition需要进行两个重要的事务:

1、捕捉到每个view在开始和结束的ui中的状态,并且

2、构建在开始和结束的动画效果进行展示。

举个例子:考虑到当我们tab屏幕的时候,Activity进行渐进或者渐出,我们可以使用如下一些代码进行实现:

public class ExampleActivity extends Activity implements View.OnClickListener {
    private ViewGroup mRootView;
    private View mRedBox, mGreenBox, mBlueBox, mBlackBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRootView = (ViewGroup) findViewById(R.id.layout_root_view);
        mRootView.setOnClickListener(this);

        mRedBox = findViewById(R.id.red_box);
        mGreenBox = findViewById(R.id.green_box);
        mBlueBox = findViewById(R.id.blue_box);
        mBlackBox = findViewById(R.id.black_box);
    }
    @Override
    public void onClick(View v) {
        TransitionManager.beginDelayedTransition(mRootView, new Fade());
        toggleVisibility(mRedBox, mGreenBox, mBlueBox, mBlackBox);
    }

    private static void toggleVisibility(View... views) {
        for (View view : views) {
            boolean isVisible = view.getVisibility() == View.VISIBLE;
            view.setVisibility(isVisible ? View.INVISIBLE : View.VISIBLE);
        }
    }
}

为了更好的理解这些代码在运行的时候发生了什么,让我们对屏幕上出现的每个View进行逐步分解。

1、一个点击被屏幕侦测到,调用beginDelayedTransition()方法,将渐进或者渐出作为参数携带之根屏幕,framework对每个view立即调用captureStartValues() 方法,并且,transition记录下了每个view的显示与否状态。

2、当调用结束的时候,我们将每个view的显示状态置为INSIVIBLE。

3、在下一步中,framework调用captureEndValues()方法,并记录下每个view的状态。

4、framework调用createAnimator()方法,该方法会对每个view的两个不同状态进行分析,找出不同,譬如:在开始时可见但在最终不可见。

5、framework运行返回的Animator,进行动画的展示。

以上就是这个例子的简单分析。framework将对view的起始状态的不同进行动画的展示。首先,Transition会从起始状态和结束状态中抽象出所要进行的动画,而开发者只需要将起始和结束的状态设置好就行了,这大大减少了书写的代码量;其二,view之间的变化可以经由各种属性进行,比方说这里不是fade,而是Slide或者是Explode。

Activity & Fragment Transitions in Android Lollipop

在Lollipop中,Transition现在可以对Activity或者Fragment之间的切换做出精细的动画效果。虽然在之前的android版本中,我们可以通过Activity#overridePendingTransition()FragmentTransaction#setCustomAnimation()这样的方式进行。但是一个致命的缺点就是:不能进行对Activity或者Fragment所承载的Container进行整体动画变换。但是Lollippop做到了:可以进行整体动画效果的展示,并且如果在动画变换的过程中,有公用的view,我们也能对变换的效果进行动画展示。

现在我们先来讨论熟悉一下接下来需要使用的一些术语,注意到的是,尽管下面的术语是以Activity中定义的,但在Fragment中一样适用。

A和B同为Activity,假设A现在启动到了B,那么我们称A为启动方(CallingActivity),B为被启动方(CalledActivity).

在Transition API中,定义了exit,enter,return和reenter这四种变换。在如下的A和B俩Activity中,我们这样称呼四种变化:

Activity A’s exit transition determines how views in A are animated when A starts B.

Activity B’s enter transition determines how views in B are animated when A starts B.

Activity B’s return transition determines how views in B are animated when B returns to A.

Activity A’s reenter transition determines how views in A are animated when B returns to A.

最后,framework api定义了变化的两种类型:content transition和shared element transition。每种皆可为你所用。

A content transition determines how an activity’s non-shared views—called transitioning views—enter or exit the activity scene.

A shared element transition determines how an activity’s shared elements (also called hero views) are animated between two activities.

Introducing the Activity Transition API

当你开始学习Lollipop中新的api的时候,我们建议你使用一下的几个步骤,当然,这些是基础的,也是重要的,后面的几篇博文中,我们将进行深入的拓展。

  • 使用transition变换的api的时候,要将你需要变换的activity的xml文件中使用 Window.FEATURE_ACTIVITY_TRANSITIONS

  • 为你的变换动作做出精细的说明,因为android在找不到这样的说明的时候,会默认使用null或者fade进行动画。

  • 对于activity之间共享的view的动画效果同样需要如上详细说明。

  • 不要使用startActivity(Context)了,使用startActivity(Context,Bundle),其中Bundle如下构造:ActivityOptions.makeSceneTransitionAnimation(activity, pairs).toBundle();,其中pairs是activity中共享的view。当然,不要忘记给你的共享的view在代码中或者xml中一个唯一的名称,否则,不会起作用了。

  • 你要使用finishAfterTransition()而非finish().

  • material-themed应用在activity变换之前会有一个细微的enter/return动作,如果你希望你自定义的动作无缝且完美,你可以调用setWindowAllowEnterTransitionOverlap()setWindowAllowReturnTransitionOverlap()方法或者在theme的xml文件中仔细定义这些属性。

Introducing the Fragment Transition API

  • Content exit, enter, reenter, and return transitions should be set by calling the corresponding methods in the Fragment class or as attributes in your Fragment’s XML tag.
  • Shared element enter and return transitions should be set by calling the corresponding methods in the Fragment class or as attributes in your Fragment’s XML.
  • Whereas Activity transitions are triggered by explicit calls to startActivity() and finishAfterTransition(), Fragment transitions are triggered automatically when a fragment is added, removed, attached, detached, shown, or hidden by a FragmentTransaction.
  • Shared elements should be specified as part of the FragmentTransaction by calling the addSharedElement(View, String) method before the transaction is committed.

基本上和activity差别不大。

Conclusion

1、如果你想练习,这里有xml文件:xml;
2、It might look like the views in A are fading in/out of the screen at first, but what you are really seeing is activity B fading in/out of the screen on top of activity A. The views in activity A are not actually animating during this time. You can adjust the duration of the background fade by calling setTransitionBackgroundFadeDuration() on the called activity’s Window.
3、For an explanation describing the differences between the FEATURE_ACTIVITY_TRANSITIONS and FEATURE_CONTENT_TRANSITIONS window feature flags, see this StackOverflow post
4、使用ActivityOptions.makeSceneTransitionAnimation(activity).toBundle()进行无共享view的动画;如果你需要不需要activity之间的变换,那么传递一个null就好啦!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值