1 /* 2 * Copyright (C) 2009 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.apis.app; 18 19 // Need the following import to get access to the app resources, since this 20 // class is in a sub-package. 21 import com.example.android.apis.R; 22 23 import android.app.Activity; 24 import android.app.ActivityOptions; 25 import android.content.Intent; 26 import android.graphics.Bitmap; 27 import android.graphics.Canvas; 28 import android.os.Bundle; 29 import android.util.Log; 30 import android.view.View; 31 import android.view.View.OnClickListener; 32 import android.widget.Button; 33 34 35 /** 36 * <p>Example of using a custom animation when transitioning between activities.</p> 37 */ 38 public class Animation extends Activity { 39 private static final String TAG = "Animation"; 40 41 @Override onCreate(Bundle savedInstanceState)42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 45 setContentView(R.layout.activity_animation); 46 47 // Watch for button clicks. 48 Button button = (Button)findViewById(R.id.fade_animation); 49 button.setOnClickListener(mFadeListener); 50 button = (Button)findViewById(R.id.zoom_animation); 51 button.setOnClickListener(mZoomListener); 52 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 53 button = (Button)findViewById(R.id.modern_fade_animation); 54 button.setOnClickListener(mModernFadeListener); 55 button = (Button)findViewById(R.id.modern_zoom_animation); 56 button.setOnClickListener(mModernZoomListener); 57 button = (Button)findViewById(R.id.scale_up_animation); 58 button.setOnClickListener(mScaleUpListener); 59 button = (Button)findViewById(R.id.zoom_thumbnail_animation); 60 button.setOnClickListener(mZoomThumbnailListener); 61 button = (Button)findViewById(R.id.no_animation); 62 button.setOnClickListener(mNoAnimationListener); 63 } else { 64 findViewById(R.id.modern_fade_animation).setEnabled(false); 65 findViewById(R.id.modern_zoom_animation).setEnabled(false); 66 findViewById(R.id.scale_up_animation).setEnabled(false); 67 findViewById(R.id.zoom_thumbnail_animation).setEnabled(false); 68 } 69 } 70 71 @Override onEnterAnimationComplete()72 public void onEnterAnimationComplete() { 73 Log.i(TAG, "onEnterAnimationComplete"); 74 } 75 76 private OnClickListener mFadeListener = new OnClickListener() { 77 public void onClick(View v) { 78 Log.i(TAG, "Starting fade-in animation..."); 79 // Request the next activity transition (here starting a new one). 80 startActivity(new Intent(Animation.this, AlertDialogSamples.class)); 81 // Supply a custom animation. This one will just fade the new 82 // activity on top. Note that we need to also supply an animation 83 // (here just doing nothing for the same amount of time) for the 84 // old activity to prevent it from going away too soon. 85 overridePendingTransition(R.anim.fade, R.anim.hold); 86 } 87 }; 88 89 private OnClickListener mZoomListener = new OnClickListener() { 90 public void onClick(View v) { 91 Log.i(TAG, "Starting zoom-in animation..."); 92 // Request the next activity transition (here starting a new one). 93 startActivity(new Intent(Animation.this, AlertDialogSamples.class)); 94 // This is a more complicated animation, involving transformations 95 // on both this (exit) and the new (enter) activity. Note how for 96 // the duration of the animation we force the exiting activity 97 // to be Z-ordered on top (even though it really isn't) to achieve 98 // the effect we want. 99 overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit); 100 } 101 }; 102 103 private OnClickListener mModernFadeListener = new OnClickListener() { 104 public void onClick(View v) { 105 Log.i(TAG, "Starting modern-fade-in animation..."); 106 // Create the desired custom animation, involving transformations 107 // on both this (exit) and the new (enter) activity. Note how for 108 // the duration of the animation we force the exiting activity 109 // to be Z-ordered on top (even though it really isn't) to achieve 110 // the effect we want. 111 ActivityOptions opts = ActivityOptions.makeCustomAnimation(Animation.this, 112 R.anim.fade, R.anim.hold); 113 // Request the activity be started, using the custom animation options. 114 startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle()); 115 } 116 }; 117 118 private OnClickListener mModernZoomListener = new OnClickListener() { 119 public void onClick(View v) { 120 Log.i(TAG, "Starting modern-zoom-in animation..."); 121 // Create a more complicated animation, involving transformations 122 // on both this (exit) and the new (enter) activity. Note how for 123 // the duration of the animation we force the exiting activity 124 // to be Z-ordered on top (even though it really isn't) to achieve 125 // the effect we want. 126 ActivityOptions opts = ActivityOptions.makeCustomAnimation(Animation.this, 127 R.anim.zoom_enter, R.anim.zoom_enter); 128 // Request the activity be started, using the custom animation options. 129 startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle()); 130 } 131 }; 132 133 private OnClickListener mScaleUpListener = new OnClickListener() { 134 public void onClick(View v) { 135 Log.i(TAG, "Starting scale-up animation..."); 136 // Create a scale-up animation that originates at the button 137 // being pressed. 138 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation( 139 v, 0, 0, v.getWidth(), v.getHeight()); 140 // Request the activity be started, using the custom animation options. 141 startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle()); 142 } 143 }; 144 145 private OnClickListener mZoomThumbnailListener = new OnClickListener() { 146 public void onClick(View v) { 147 Log.i(TAG, "Starting thumbnail-zoom animation..."); 148 // Create a thumbnail animation. We are going to build our thumbnail 149 // just from the view that was pressed. We make sure the view is 150 // not selected, because by the time the animation starts we will 151 // have finished with the selection of the tap. 152 v.setDrawingCacheEnabled(true); 153 v.setPressed(false); 154 v.refreshDrawableState(); 155 Bitmap bm = v.getDrawingCache(); 156 Canvas c = new Canvas(bm); 157 //c.drawARGB(255, 255, 0, 0); 158 ActivityOptions opts = ActivityOptions.makeThumbnailScaleUpAnimation( 159 v, bm, 0, 0); 160 // Request the activity be started, using the custom animation options. 161 startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle()); 162 v.setDrawingCacheEnabled(false); 163 } 164 }; 165 166 private OnClickListener mNoAnimationListener = new OnClickListener() { 167 public void onClick(View v) { 168 Log.i(TAG, "Starting no animation transition..."); 169 // Request the next activity transition (here starting a new one). 170 startActivity(new Intent(Animation.this, AlertDialogSamples.class)); 171 overridePendingTransition(0, 0); 172 } 173 }; 174 } 175