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.multipropertyanimations;
18 
19 import android.animation.ObjectAnimator;
20 import android.animation.PropertyValuesHolder;
21 import android.animation.ValueAnimator;
22 import android.animation.ValueAnimator.AnimatorUpdateListener;
23 import android.app.Activity;
24 import android.os.Bundle;
25 import android.view.View;
26 
27 /**
28  * This example shows various ways of animating multiple properties in parallel.
29  *
30  * Watch the associated video for this demo on the DevBytes channel of developer.android.com
31  * or on the DevBytes playlist in the androiddevelopers channel on YouTube at
32  * https://www.youtube.com/playlist?list=PLWz5rJ2EKKc_XOgcRukSoKKjewFJZrKV0.
33  */
34 public class MultiPropertyAnimations extends Activity {
35 
36     private static final float TX_START = 0;
37     private static final float TY_START = 0;
38     private static final float TX_END = 400;
39     private static final float TY_END = 200;
40 
41     @Override
onCreate(Bundle savedInstanceState)42     protected void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44         setContentView(R.layout.activity_multi_property_animations);
45     }
46 
47     /**
48      * A very manual approach to animation uses a ValueAnimator to animate a fractional
49      * value and then turns that value into the final property values which are then set
50      * directly on the target object.
51      */
runValueAnimator(final View view)52     public void runValueAnimator(final View view) {
53         ValueAnimator anim = ValueAnimator.ofFloat(0, 400);
54         anim.addUpdateListener(new AnimatorUpdateListener() {
55             @Override
56             public void onAnimationUpdate(ValueAnimator animator) {
57                 float fraction = animator.getAnimatedFraction();
58                 view.setTranslationX(TX_START + fraction * (TX_END - TX_START));
59                 view.setTranslationY(TY_START + fraction * (TY_END - TY_START));
60             }
61         });
62         anim.start();
63     }
64 
65     /**
66      * ViewPropertyAnimator is the cleanest and most efficient way of animating
67      * View properties, even when there are multiple properties to be animated
68      * in parallel.
69      */
runViewPropertyAnimator(View view)70     public void runViewPropertyAnimator(View view) {
71         view.animate().translationX(TX_END).translationY(TY_END);
72     }
73 
74     /**
75      * Multiple ObjectAnimator objects can be created and run in parallel.
76      */
runObjectAnimators(View view)77     public void runObjectAnimators(View view) {
78         ObjectAnimator.ofFloat(view, View.TRANSLATION_X, TX_END).start();
79         ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, TY_END).start();
80         // Optional: use an AnimatorSet to run these in parallel
81     }
82 
83     /**
84      * Using PropertyValuesHolder objects enables the use of a single ObjectAnimator
85      * per target, even when there are multiple properties being animated on that target.
86      */
runObjectAnimator(View view)87     public void runObjectAnimator(View view) {
88         PropertyValuesHolder pvhTX = PropertyValuesHolder.ofFloat(View.TRANSLATION_X, TX_END);
89         PropertyValuesHolder pvhTY = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, TY_END);
90         ObjectAnimator.ofPropertyValuesHolder(view, pvhTX, pvhTY).start();
91     }
92 }
93