1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.basictransition;
18 
19 import android.os.Bundle;
20 import android.support.v4.app.Fragment;
21 import android.transition.Scene;
22 import android.transition.TransitionInflater;
23 import android.transition.TransitionManager;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.RadioGroup;
28 
29 public class BasicTransitionFragment extends Fragment
30         implements RadioGroup.OnCheckedChangeListener {
31 
32     // We transition between these Scenes
33     private Scene mScene1;
34     private Scene mScene2;
35     private Scene mScene3;
36 
37     /** A custom TransitionManager */
38     private TransitionManager mTransitionManagerForScene3;
39 
40     /** Transitions take place in this ViewGroup. We retain this for the dynamic transition on scene 4. */
41     private ViewGroup mSceneRoot;
42 
newInstance()43     public static BasicTransitionFragment newInstance() {
44         return new BasicTransitionFragment();
45     }
46 
BasicTransitionFragment()47     public BasicTransitionFragment() {
48     }
49 
50     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)51     public View onCreateView(LayoutInflater inflater, ViewGroup container,
52                              Bundle savedInstanceState) {
53         View view = inflater.inflate(R.layout.fragment_basic_transition, container, false);
54         assert view != null;
55         RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.select_scene);
56         radioGroup.setOnCheckedChangeListener(this);
57         mSceneRoot = (ViewGroup) view.findViewById(R.id.scene_root);
58 
59         // BEGIN_INCLUDE(instantiation_from_view)
60         // A Scene can be instantiated from a live view hierarchy.
61         mScene1 = new Scene(mSceneRoot, (ViewGroup) mSceneRoot.findViewById(R.id.container));
62         // END_INCLUDE(instantiation_from_view)
63 
64         // BEGIN_INCLUDE(instantiation_from_resource)
65         // You can also inflate a generate a Scene from a layout resource file.
66         mScene2 = Scene.getSceneForLayout(mSceneRoot, R.layout.scene2, getActivity());
67         // END_INCLUDE(instantiation_from_resource)
68 
69         // Another scene from a layout resource file.
70         mScene3 = Scene.getSceneForLayout(mSceneRoot, R.layout.scene3, getActivity());
71 
72         // BEGIN_INCLUDE(custom_transition_manager)
73         // We create a custom TransitionManager for Scene 3, in which ChangeBounds and Fade
74         // take place at the same time.
75         mTransitionManagerForScene3 = TransitionInflater.from(getActivity())
76                 .inflateTransitionManager(R.transition.scene3_transition_manager, mSceneRoot);
77         // END_INCLUDE(custom_transition_manager)
78 
79         return view;
80     }
81 
82     @Override
onCheckedChanged(RadioGroup group, int checkedId)83     public void onCheckedChanged(RadioGroup group, int checkedId) {
84         switch (checkedId) {
85             case R.id.select_scene_1: {
86                 // BEGIN_INCLUDE(transition_simple)
87                 // You can start an automatic transition with TransitionManager.go().
88                 TransitionManager.go(mScene1);
89                 // END_INCLUDE(transition_simple)
90                 break;
91             }
92             case R.id.select_scene_2: {
93                 TransitionManager.go(mScene2);
94                 break;
95             }
96             case R.id.select_scene_3: {
97                 // BEGIN_INCLUDE(transition_custom)
98                 // You can also start a transition with a custom TransitionManager.
99                 mTransitionManagerForScene3.transitionTo(mScene3);
100                 // END_INCLUDE(transition_custom)
101                 break;
102             }
103             case R.id.select_scene_4: {
104                 // BEGIN_INCLUDE(transition_dynamic)
105                 // Alternatively, transition can be invoked dynamically without a Scene.
106                 // For this, we first call TransitionManager.beginDelayedTransition().
107                 TransitionManager.beginDelayedTransition(mSceneRoot);
108                 // Then, we can just change view properties as usual.
109                 View square = mSceneRoot.findViewById(R.id.transition_square);
110                 ViewGroup.LayoutParams params = square.getLayoutParams();
111                 int newSize = getResources().getDimensionPixelSize(R.dimen.square_size_expanded);
112                 params.width = newSize;
113                 params.height = newSize;
114                 square.setLayoutParams(params);
115                 // END_INCLUDE(transition_dynamic)
116                 break;
117             }
118         }
119     }
120 
121 }
122