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