Home | History | Annotate | Download | only in propertyanimations
      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.propertyanimations;
     18 
     19 import android.animation.Animator;
     20 import android.animation.AnimatorInflater;
     21 import android.animation.AnimatorSet;
     22 import android.animation.ObjectAnimator;
     23 import android.animation.PropertyValuesHolder;
     24 import android.animation.ValueAnimator;
     25 import android.app.Activity;
     26 import android.os.Bundle;
     27 import android.view.View;
     28 import android.widget.Button;
     29 import android.widget.CheckBox;
     30 
     31 /**
     32  * This example shows how to use property animations, specifically ObjectAnimator, to perform
     33  * various view animations. Compare this approach to that of the ViewAnimations demo, which
     34  * shows how to achieve similar effects using the pre-3.0 animation APIs.
     35  *
     36  * Watch the associated video for this demo on the DevBytes channel of developer.android.com
     37  * or on YouTube at https://www.youtube.com/watch?v=3UbJhmkeSig.
     38  */
     39 public class PropertyAnimations extends Activity {
     40 
     41     CheckBox mCheckBox;
     42 
     43     @Override
     44     public void onCreate(Bundle savedInstanceState) {
     45         super.onCreate(savedInstanceState);
     46         setContentView(R.layout.activity_property_animations);
     47 
     48         mCheckBox = (CheckBox) findViewById(R.id.checkbox);
     49         final Button alphaButton = (Button) findViewById(R.id.alphaButton);
     50         final Button translateButton = (Button) findViewById(R.id.translateButton);
     51         final Button rotateButton = (Button) findViewById(R.id.rotateButton);
     52         final Button scaleButton = (Button) findViewById(R.id.scaleButton);
     53         final Button setButton = (Button) findViewById(R.id.setButton);
     54 
     55         // Fade the button out and back in
     56         ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(alphaButton,
     57                 View.ALPHA, 0);
     58         alphaAnimation.setRepeatCount(1);
     59         alphaAnimation.setRepeatMode(ValueAnimator.REVERSE);
     60 
     61         // Move the button over to the right and then back
     62         ObjectAnimator translateAnimation =
     63                 ObjectAnimator.ofFloat(translateButton, View.TRANSLATION_X, 800);
     64         translateAnimation.setRepeatCount(1);
     65         translateAnimation.setRepeatMode(ValueAnimator.REVERSE);
     66 
     67         // Spin the button around in a full circle
     68         ObjectAnimator rotateAnimation =
     69                 ObjectAnimator.ofFloat(rotateButton, View.ROTATION, 360);
     70         rotateAnimation.setRepeatCount(1);
     71         rotateAnimation.setRepeatMode(ValueAnimator.REVERSE);
     72 
     73         // Scale the button in X and Y. Note the use of PropertyValuesHolder to animate
     74         // multiple properties on the same object in parallel.
     75         PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat(View.SCALE_X, 2);
     76         PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 2);
     77         ObjectAnimator scaleAnimation =
     78                 ObjectAnimator.ofPropertyValuesHolder(scaleButton, pvhX, pvhY);
     79         scaleAnimation.setRepeatCount(1);
     80         scaleAnimation.setRepeatMode(ValueAnimator.REVERSE);
     81 
     82         // Run the animations above in sequence
     83         AnimatorSet setAnimation = new AnimatorSet();
     84         setAnimation.play(translateAnimation).after(alphaAnimation).before(rotateAnimation);
     85         setAnimation.play(rotateAnimation).before(scaleAnimation);
     86 
     87         setupAnimation(alphaButton, alphaAnimation, R.animator.fade);
     88         setupAnimation(translateButton, translateAnimation, R.animator.move);
     89         setupAnimation(rotateButton, rotateAnimation, R.animator.spin);
     90         setupAnimation(scaleButton, scaleAnimation, R.animator.scale);
     91         setupAnimation(setButton, setAnimation, R.animator.combo);
     92 
     93     }
     94 
     95     private void setupAnimation(View view, final Animator animation, final int animationID) {
     96         view.setOnClickListener(new View.OnClickListener() {
     97             public void onClick(View v) {
     98                 // If the button is checked, load the animation from the given resource
     99                 // id instead of using the passed-in animation parameter. See the xml files
    100                 // for the details on those animations.
    101                 if (mCheckBox.isChecked()) {
    102                     Animator anim = AnimatorInflater.loadAnimator(PropertyAnimations.this, animationID);
    103                     anim.setTarget(v);
    104                     anim.start();
    105                     return;
    106                 }
    107                 animation.start();
    108             }
    109         });
    110     }
    111 }
    112