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.example.android.apis.animation; 17 18 import com.example.android.apis.R; 19 20 import android.app.Activity; 21 import android.app.ActivityOptions; 22 import android.app.SharedElementCallback; 23 import android.content.Intent; 24 import android.graphics.drawable.ColorDrawable; 25 import android.os.Bundle; 26 import android.view.View; 27 import android.widget.ImageView; 28 29 import java.util.List; 30 import java.util.Map; 31 32 /** 33 * 34 */ 35 public class ActivityTransition extends Activity { 36 37 private static final String TAG = "ActivityTransition"; 38 39 private static final String KEY_ID = "ViewTransitionValues:id"; 40 41 private ImageView mHero; 42 43 public static final int[] DRAWABLES = { 44 R.drawable.ball, 45 R.drawable.block, 46 R.drawable.ducky, 47 R.drawable.jellies, 48 R.drawable.mug, 49 R.drawable.pencil, 50 R.drawable.scissors, 51 R.drawable.woot, 52 }; 53 54 public static final int[] IDS = { 55 R.id.ball, 56 R.id.block, 57 R.id.ducky, 58 R.id.jellies, 59 R.id.mug, 60 R.id.pencil, 61 R.id.scissors, 62 R.id.woot, 63 }; 64 65 public static final String[] NAMES = { 66 "ball", 67 "block", 68 "ducky", 69 "jellies", 70 "mug", 71 "pencil", 72 "scissors", 73 "woot", 74 }; 75 76 public static int getIdForKey(String id) { 77 return IDS[getIndexForKey(id)]; 78 } 79 80 public static int getDrawableIdForKey(String id) { 81 return DRAWABLES[getIndexForKey(id)]; 82 } 83 84 public static int getIndexForKey(String id) { 85 for (int i = 0; i < NAMES.length; i++) { 86 String name = NAMES[i]; 87 if (name.equals(id)) { 88 return i; 89 } 90 } 91 return 2; 92 } 93 94 @Override 95 protected void onCreate(Bundle savedInstanceState) { 96 super.onCreate(savedInstanceState); 97 getWindow().setBackgroundDrawable(new ColorDrawable(randomColor())); 98 setContentView(R.layout.image_block); 99 setupHero(); 100 } 101 102 private void setupHero() { 103 String name = getIntent().getStringExtra(KEY_ID); 104 mHero = null; 105 if (name != null) { 106 mHero = (ImageView) findViewById(getIdForKey(name)); 107 setEnterSharedElementCallback(new SharedElementCallback() { 108 @Override 109 public void onMapSharedElements(List<String> names, 110 Map<String, View> sharedElements) { 111 sharedElements.put("hero", mHero); 112 } 113 }); 114 } 115 } 116 117 public void clicked(View v) { 118 mHero = (ImageView) v; 119 Intent intent = new Intent(this, ActivityTransitionDetails.class); 120 intent.putExtra(KEY_ID, v.getTransitionName()); 121 ActivityOptions activityOptions 122 = ActivityOptions.makeSceneTransitionAnimation(this, mHero, "hero"); 123 startActivity(intent, activityOptions.toBundle()); 124 } 125 126 private static int randomColor() { 127 int red = (int)(Math.random() * 128); 128 int green = (int)(Math.random() * 128); 129 int blue = (int)(Math.random() * 128); 130 return 0xFF000000 | (red << 16) | (green << 8) | blue; 131 } 132 } 133