Home | History | Annotate | Download | only in customtransition
      1 /*
      2  * Copyright (C) 2014 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.customtransition;
     18 
     19 import com.example.android.common.logger.Log;
     20 
     21 import android.content.Context;
     22 import android.os.Bundle;
     23 import android.support.v4.app.Fragment;
     24 import android.transition.Scene;
     25 import android.transition.Transition;
     26 import android.transition.TransitionManager;
     27 import android.view.LayoutInflater;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 import android.widget.FrameLayout;
     31 
     32 public class CustomTransitionFragment extends Fragment implements View.OnClickListener {
     33 
     34     private static final String STATE_CURRENT_SCENE = "current_scene";
     35 
     36     /** Tag for the logger */
     37     private static final String TAG = "CustomTransitionFragment";
     38 
     39     /** These are the Scenes we use. */
     40     private Scene[] mScenes;
     41 
     42     /** The current index for mScenes. */
     43     private int mCurrentScene;
     44 
     45     /** This is the custom Transition we use in this sample. */
     46     private Transition mTransition;
     47 
     48     public CustomTransitionFragment() {
     49     }
     50 
     51     @Override
     52     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     53         return inflater.inflate(R.layout.fragment_custom_transition, container, false);
     54     }
     55 
     56     @Override
     57     public void onViewCreated(View view, Bundle savedInstanceState) {
     58         Context context = getActivity();
     59         FrameLayout container = (FrameLayout) view.findViewById(R.id.container);
     60         view.findViewById(R.id.show_next_scene).setOnClickListener(this);
     61         if (null != savedInstanceState) {
     62             mCurrentScene = savedInstanceState.getInt(STATE_CURRENT_SCENE);
     63         }
     64         // We set up the Scenes here.
     65         mScenes = new Scene[] {
     66                 Scene.getSceneForLayout(container, R.layout.scene1, context),
     67                 Scene.getSceneForLayout(container, R.layout.scene2, context),
     68                 Scene.getSceneForLayout(container, R.layout.scene3, context),
     69         };
     70         // This is the custom Transition.
     71         mTransition = new ChangeColor();
     72         // Show the initial Scene.
     73         TransitionManager.go(mScenes[mCurrentScene % mScenes.length]);
     74     }
     75 
     76     @Override
     77     public void onSaveInstanceState(Bundle outState) {
     78         super.onSaveInstanceState(outState);
     79         outState.putInt(STATE_CURRENT_SCENE, mCurrentScene);
     80     }
     81 
     82     @Override
     83     public void onClick(View v) {
     84         switch (v.getId()) {
     85             case R.id.show_next_scene: {
     86                 mCurrentScene = (mCurrentScene + 1) % mScenes.length;
     87                 Log.i(TAG, "Transitioning to scene #" + mCurrentScene);
     88                 // Pass the custom Transition as second argument for TransitionManager.go
     89                 TransitionManager.go(mScenes[mCurrentScene], mTransition);
     90                 break;
     91             }
     92         }
     93     }
     94 
     95 }
     96