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 package com.android.transitiontests;
17 
18 import android.app.Activity;
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.transition.ChangeBounds;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.transition.Fade;
26 import android.transition.Scene;
27 import android.transition.Transition;
28 import android.transition.TransitionSet;
29 import android.widget.ImageView;
30 import android.widget.TextView;
31 import android.transition.Crossfade;
32 import android.transition.Rotate;
33 import android.transition.TransitionManager;
34 
35 public class ContactsExpansion extends Activity {
36 
37     String contactsData[] = {
38             "Alan Green", "56 Bob Street", "Boston, MA 02134", "617-555-5555", "blatt@blatt.com",
39             "Bob Foonman", "92 The Avenue", "Chico, CA 78456", "510-555-5556", "bob@jerk.com",
40             "Tracey Sue", "95 Houses Street", "San Jose, CA 96504", "415-555-5557", "ts@thing.com",
41     };
42 
43     View currentItem = null;
44 
45     TransitionSet mMyAutoTransition = new TransitionSet().
46             setOrdering(TransitionSet.ORDERING_SEQUENTIAL);
47 
48     @Override
onCreate(Bundle savedInstanceState)49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         setContentView(R.layout.contacts_list);
52         ViewGroup contactsContainer = findViewById(R.id.contactsContainer);
53 
54         int contactsIndex = 0;
55         addContact(contactsContainer, contactsIndex, R.drawable.self_portrait_square_100);
56         contactsIndex += 5;
57         addContact(contactsContainer, contactsIndex, R.drawable.self_portrait_square_100);
58         contactsIndex += 5;
59         addContact(contactsContainer, contactsIndex, R.drawable.self_portrait_square_100);
60 
61     }
62 
addContact(ViewGroup container, int dataIndex, int thumbnailID)63     private void addContact(ViewGroup container, int dataIndex, int thumbnailID) {
64         LayoutInflater inflater = (LayoutInflater)
65                 getSystemService(Context.LAYOUT_INFLATER_SERVICE);
66         View contactItem = inflater.inflate(R.layout.contact_collapsed, container, false);
67         ImageView thumbnailView = (ImageView) contactItem.findViewById(R.id.contact_picture);
68         thumbnailView.setImageResource(thumbnailID);
69         ((TextView)contactItem.findViewById(R.id.contact_name)).setText(contactsData[dataIndex++]);
70         ((TextView)contactItem.findViewById(R.id.contact_street)).
71                 setText(contactsData[dataIndex++]);
72         ((TextView)contactItem.findViewById(R.id.contact_city)).setText(contactsData[dataIndex++]);
73         ((TextView)contactItem.findViewById(R.id.contact_phone)).setText(contactsData[dataIndex++]);
74         ((TextView)contactItem.findViewById(R.id.contact_email)).setText(contactsData[dataIndex++]);
75         container.addView(contactItem);
76 
77         final TransitionSet myTransition = new TransitionSet();
78         myTransition.addTransition(new Fade(Fade.IN)).
79                 addTransition(new Rotate().addTarget(R.id.contact_arrow)).
80                 addTransition(new ChangeBounds()).
81                 addTransition(new Fade(Fade.OUT)).
82                 addTransition(new Crossfade().addTarget(R.id.contact_picture));
83         final ToggleScene toggleScene = new ToggleScene(container, myTransition);
84         contactItem.setOnClickListener(new View.OnClickListener() {
85             @Override
86             public void onClick(View v) {
87                 currentItem = v;
88                 toggleScene.changeToScene();
89             }
90         });
91     }
92 
93     class ToggleScene {
94         boolean expanded = false;
95         Scene mScene;
96         Transition mTransition;
97 
ToggleScene(ViewGroup rootView, Transition transition)98         ToggleScene(ViewGroup rootView, Transition transition) {
99             mScene = new Scene(rootView);
100             mTransition = transition;
101             mScene.setEnterAction(new Runnable() {
102                 @Override
103                 public void run() {
104                     if (currentItem != null) {
105                         System.out.println("onsceneChanged: currentItem = " + currentItem);
106                         View expandedContainer = currentItem.findViewById(R.id.expanded_info);
107                         expandedContainer.setVisibility(expanded ? View.GONE : View.VISIBLE);
108                         ImageView thumbnailView =
109                                 (ImageView) currentItem.findViewById(R.id.contact_picture);
110                         thumbnailView.setImageResource(expanded ? R.drawable.self_portrait_square_100 :
111                                 R.drawable.self_portrait_square_200);
112                         ImageView arrow = (ImageView) currentItem.findViewById(R.id.contact_arrow);
113                         arrow.setRotation(expanded ? 0 : 90);
114                         expanded = !expanded;
115                     }
116                 }
117             });
118         }
119 
changeToScene()120         void changeToScene() {
121             TransitionManager.go(mScene, mTransition);
122         }
123     };
124 }
125