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 
17 package com.example.android.windowanimations;
18 
19 import android.app.Activity;
20 import android.app.ActivityOptions;
21 import android.content.Intent;
22 import android.graphics.Bitmap;
23 import android.graphics.drawable.BitmapDrawable;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.widget.Button;
27 import android.widget.ImageView;
28 
29 /**
30  * This example shows how to create custom Window animations to animate between different
31  * sub-activities.
32  *
33  * Watch the associated video for this demo on the DevBytes channel of developer.android.com
34  * or on YouTube at https://www.youtube.com/watch?v=Ho8vk61lVIU.
35  */
36 public class WindowAnimations extends Activity {
37 
38     @Override
onCreate(Bundle savedInstanceState)39     public void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         setContentView(R.layout.activity_window_animations);
42 
43         final Button defaultButton = (Button) findViewById(R.id.defaultButton);
44         final Button translateButton = (Button) findViewById(R.id.translateButton);
45         final Button scaleButton = (Button) findViewById(R.id.scaleButton);
46         final ImageView thumbnail = (ImageView) findViewById(R.id.thumbnail);
47 
48         // By default, launching a sub-activity uses the system default for window animations
49         defaultButton.setOnClickListener(new View.OnClickListener() {
50             @Override
51             public void onClick(View v) {
52                 Intent subActivity = new Intent(WindowAnimations.this,
53                         SubActivity.class);
54                 startActivity(subActivity);
55             }
56         });
57 
58         // Custom animations allow us to do things like slide the next activity in as we
59         // slide this activity out
60         translateButton.setOnClickListener(new View.OnClickListener() {
61             @Override
62             public void onClick(View v) {
63                 // Using the AnimatedSubActivity also allows us to animate exiting that
64                 // activity - see that activity for details
65                 Intent subActivity = new Intent(WindowAnimations.this,
66                         AnimatedSubActivity.class);
67                 // The enter/exit animations for the two activities are specified by xml resources
68                 Bundle translateBundle =
69                         ActivityOptions.makeCustomAnimation(WindowAnimations.this,
70                         R.anim.slide_in_left, R.anim.slide_out_left).toBundle();
71                 startActivity(subActivity, translateBundle);
72             }
73         });
74 
75         // Starting in Jellybean, you can provide an animation that scales up the new
76         // activity from a given source rectangle
77         scaleButton.setOnClickListener(new View.OnClickListener() {
78             @Override
79             public void onClick(View v) {
80                 Intent subActivity = new Intent(WindowAnimations.this,
81                         AnimatedSubActivity.class);
82                 Bundle scaleBundle = ActivityOptions.makeScaleUpAnimation(
83                         v, 0, 0, v.getWidth(), v.getHeight()).toBundle();
84                 startActivity(subActivity, scaleBundle);
85             }
86         });
87 
88         // Starting in Jellybean, you can also provide an animation that scales up the new
89         // activity from a given bitmap, cross-fading between the starting and ending
90         // representations. Here, we scale up from a thumbnail image of the final sub-activity
91         thumbnail.setOnClickListener(new View.OnClickListener() {
92             @Override
93             public void onClick(View v) {
94                 BitmapDrawable drawable = (BitmapDrawable) thumbnail.getDrawable();
95                 Bitmap bm = drawable.getBitmap();
96                 Intent subActivity = new Intent(WindowAnimations.this, AnimatedSubActivity.class);
97                 Bundle scaleBundle = ActivityOptions.makeThumbnailScaleUpAnimation(
98                         thumbnail, bm, 0, 0).toBundle();
99                 startActivity(subActivity, scaleBundle);
100             }
101         });
102 
103 
104     }
105 
106 }
107