Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 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.view.LayoutInflater; |
| 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
| 24 | import android.view.transition.Fade; |
| 25 | import android.view.transition.Scene; |
| 26 | import android.view.transition.Transition; |
| 27 | import android.widget.ImageView; |
| 28 | import android.widget.TextView; |
| 29 | import android.view.transition.Crossfade; |
| 30 | import android.view.transition.Move; |
| 31 | import android.view.transition.Rotate; |
| 32 | import android.view.transition.TransitionGroup; |
| 33 | import android.view.transition.TransitionManager; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 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 | TransitionGroup mMyAutoTransition = new TransitionGroup(TransitionGroup.SEQUENTIALLY); |
| 46 | |
| 47 | @Override |
| 48 | protected void onCreate(Bundle savedInstanceState) { |
| 49 | super.onCreate(savedInstanceState); |
| 50 | setContentView(R.layout.contacts_list); |
| 51 | ViewGroup contactsContainer = (ViewGroup) findViewById(R.id.contactsContainer); |
| 52 | |
| 53 | int contactsIndex = 0; |
| 54 | addContact(contactsContainer, contactsIndex, R.drawable.self_portrait_square_100); |
| 55 | contactsIndex += 5; |
| 56 | addContact(contactsContainer, contactsIndex, R.drawable.self_portrait_square_100); |
| 57 | contactsIndex += 5; |
| 58 | addContact(contactsContainer, contactsIndex, R.drawable.self_portrait_square_100); |
| 59 | |
| 60 | } |
| 61 | |
| 62 | private void addContact(ViewGroup container, int dataIndex, int thumbnailID) { |
| 63 | LayoutInflater inflater = (LayoutInflater) |
| 64 | getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 65 | View contactItem = inflater.inflate(R.layout.contact_collapsed, container, false); |
| 66 | ImageView thumbnailView = (ImageView) contactItem.findViewById(R.id.contact_picture); |
| 67 | thumbnailView.setImageResource(thumbnailID); |
| 68 | ((TextView)contactItem.findViewById(R.id.contact_name)).setText(contactsData[dataIndex++]); |
| 69 | ((TextView)contactItem.findViewById(R.id.contact_street)). |
| 70 | setText(contactsData[dataIndex++]); |
| 71 | ((TextView)contactItem.findViewById(R.id.contact_city)).setText(contactsData[dataIndex++]); |
| 72 | ((TextView)contactItem.findViewById(R.id.contact_phone)).setText(contactsData[dataIndex++]); |
| 73 | ((TextView)contactItem.findViewById(R.id.contact_email)).setText(contactsData[dataIndex++]); |
| 74 | container.addView(contactItem); |
| 75 | |
| 76 | final TransitionGroup myTransition = new TransitionGroup(); |
| 77 | myTransition.addTransitions(new Fade(Fade.IN), |
| 78 | new Rotate().setTargetIds(R.id.contact_arrow), |
| 79 | new Move(), new Fade(Fade.OUT), |
| 80 | new Crossfade().setTargetIds(R.id.contact_picture)); |
| 81 | final ToggleScene toggleScene = new ToggleScene(container, myTransition); |
| 82 | contactItem.setOnClickListener(new View.OnClickListener() { |
| 83 | @Override |
| 84 | public void onClick(View v) { |
| 85 | currentItem = v; |
| 86 | toggleScene.changeToScene(); |
| 87 | } |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | class ToggleScene { |
| 92 | boolean expanded = false; |
| 93 | Scene mScene; |
| 94 | Transition mTransition; |
| 95 | |
| 96 | ToggleScene(ViewGroup rootView, Transition transition) { |
| 97 | mScene = new Scene(rootView); |
| 98 | mTransition = transition; |
| 99 | mScene.setEnterAction(new Runnable() { |
| 100 | @Override |
| 101 | public void run() { |
| 102 | if (currentItem != null) { |
| 103 | System.out.println("onsceneChanged: currentItem = " + currentItem); |
| 104 | View expandedContainer = currentItem.findViewById(R.id.expanded_info); |
| 105 | expandedContainer.setVisibility(expanded ? View.GONE : View.VISIBLE); |
| 106 | ImageView thumbnailView = |
| 107 | (ImageView) currentItem.findViewById(R.id.contact_picture); |
| 108 | thumbnailView.setImageResource(expanded ? R.drawable.self_portrait_square_100 : |
| 109 | R.drawable.self_portrait_square_200); |
| 110 | ImageView arrow = (ImageView) currentItem.findViewById(R.id.contact_arrow); |
| 111 | arrow.setRotation(expanded ? 0 : 90); |
| 112 | expanded = !expanded; |
| 113 | } |
| 114 | } |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | void changeToScene() { |
| 119 | TransitionManager.go(mScene, mTransition); |
| 120 | } |
| 121 | }; |
| 122 | } |