• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.viewanimations;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.view.View;
22 import android.view.animation.AlphaAnimation;
23 import android.view.animation.Animation;
24 import android.view.animation.AnimationSet;
25 import android.view.animation.AnimationUtils;
26 import android.view.animation.RotateAnimation;
27 import android.view.animation.ScaleAnimation;
28 import android.view.animation.TranslateAnimation;
29 import android.widget.Button;
30 import android.widget.CheckBox;
31 
32 /**
33  * This example shows how to use pre-3.0 view Animation classes to create various animated UI
34  * effects. See also the demo PropertyAnimations, which shows how this is done using the new
35  * ObjectAnimator API introduced in Android 3.0.
36  *
37  * Watch the associated video for this demo on the DevBytes channel of developer.android.com
38  * or on YouTube at https://www.youtube.com/watch?v=_UWXqFBF86U.
39  */
40 public class ViewAnimations extends Activity {
41 
42     CheckBox mCheckBox;
43 
44     @Override
onCreate(Bundle savedInstanceState)45     public void onCreate(Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47         setContentView(R.layout.activity_view_animations);
48 
49         mCheckBox = (CheckBox) findViewById(R.id.checkbox);
50         final Button alphaButton = (Button) findViewById(R.id.alphaButton);
51         final Button translateButton = (Button) findViewById(R.id.translateButton);
52         final Button rotateButton = (Button) findViewById(R.id.rotateButton);
53         final Button scaleButton = (Button) findViewById(R.id.scaleButton);
54         final Button setButton = (Button) findViewById(R.id.setButton);
55 
56         // Fade the button out and back in
57         final AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
58         alphaAnimation.setDuration(1000);
59 
60         // Move the button over and then back
61         final TranslateAnimation translateAnimation =
62                 new TranslateAnimation(Animation.ABSOLUTE, 0,
63                 Animation.RELATIVE_TO_PARENT, 1,
64                 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 100);
65         translateAnimation.setDuration(1000);
66 
67         // Spin the button around in a full circle
68         final RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
69                 Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
70         rotateAnimation.setDuration(1000);
71 
72         // Scale the button in X and Y.
73         final ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2);
74         scaleAnimation.setDuration(1000);
75 
76         // Run the animations above in sequence on the final button. Looks horrible.
77         final AnimationSet setAnimation = new AnimationSet(true);
78         setAnimation.addAnimation(alphaAnimation);
79         setAnimation.addAnimation(translateAnimation);
80         setAnimation.addAnimation(rotateAnimation);
81         setAnimation.addAnimation(scaleAnimation);
82 
83         setupAnimation(alphaButton, alphaAnimation, R.anim.alpha_anim);
84         setupAnimation(translateButton, translateAnimation, R.anim.translate_anim);
85         setupAnimation(rotateButton, rotateAnimation, R.anim.rotate_anim);
86         setupAnimation(scaleButton, scaleAnimation, R.anim.scale_anim);
87         setupAnimation(setButton, setAnimation, R.anim.set_anim);
88 
89     }
90 
setupAnimation(View view, final Animation animation, final int animationID)91     private void setupAnimation(View view, final Animation animation,
92             final int animationID) {
93         view.setOnClickListener(new View.OnClickListener() {
94             public void onClick(View v) {
95                 // If the button is checked, load the animation from the given resource
96                 // id instead of using the passed-in animation paramter. See the xml files
97                 // for the details on those animations.
98                 v.startAnimation(mCheckBox.isChecked() ?
99                         AnimationUtils.loadAnimation(ViewAnimations.this, animationID) :
100                         animation);
101             }
102         });
103     }
104 }
105