Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2014 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.apis.graphics;
     18 
     19 import android.animation.Animator;
     20 import android.animation.AnimatorSet;
     21 import android.animation.ObjectAnimator;
     22 import android.app.Activity;
     23 import android.os.Bundle;
     24 import android.view.ViewGroup;
     25 import android.widget.TextView;
     26 
     27 import com.example.android.apis.R;
     28 
     29 import java.util.ArrayList;
     30 
     31 public class ShadowCardStack extends Activity {
     32 
     33     private static final float X_SHIFT_DP = 1000;
     34     private static final float Y_SHIFT_DP = 50;
     35     private static final float Z_LIFT_DP = 8;
     36     private static final float ROTATE_DEGREES = 15;
     37 
     38     public AnimatorSet createSet(ArrayList<Animator> items, long startDelay) {
     39         AnimatorSet set = new AnimatorSet();
     40         set.playTogether(items);
     41         set.setStartDelay(startDelay);
     42         return set;
     43     }
     44 
     45     @Override
     46     protected void onCreate(Bundle savedInstanceState) {
     47         super.onCreate(savedInstanceState);
     48         setContentView(R.layout.shadow_card_stack);
     49 
     50         float density = getResources().getDisplayMetrics().density;
     51 
     52         final ViewGroup cardParent = (ViewGroup) findViewById(R.id.card_parent);
     53 
     54         final float X = X_SHIFT_DP * density;
     55         final float Y = Y_SHIFT_DP * density;
     56         final float Z = Z_LIFT_DP * density;
     57 
     58         ArrayList<Animator> towardAnimators = new ArrayList<Animator>();
     59         ArrayList<Animator> expandAnimators = new ArrayList<Animator>();
     60         ArrayList<Animator> moveAwayAnimators = new ArrayList<Animator>();
     61         ArrayList<Animator> moveBackAnimators = new ArrayList<Animator>();
     62         ArrayList<Animator> awayAnimators = new ArrayList<Animator>();
     63         ArrayList<Animator> collapseAnimators = new ArrayList<Animator>();
     64 
     65         final int max = cardParent.getChildCount();
     66         for (int i = 0; i < max; i++) {
     67             TextView card = (TextView) cardParent.getChildAt(i);
     68             card.setText("Card number " + i);
     69 
     70             float targetY = (i - (max-1) / 2.0f) * Y;
     71             Animator expand = ObjectAnimator.ofFloat(card, "translationY", targetY);
     72             expandAnimators.add(expand);
     73 
     74             Animator toward = ObjectAnimator.ofFloat(card, "translationZ", i * Z);
     75             toward.setStartDelay(200 * ((max) - i));
     76             towardAnimators.add(toward);
     77 
     78             card.setPivotX(X_SHIFT_DP);
     79             Animator rotateAway = ObjectAnimator.ofFloat(card, "rotationY",
     80                     i == 0 ? 0 : ROTATE_DEGREES);
     81             rotateAway.setStartDelay(200 * ((max) - i));
     82             rotateAway.setDuration(100);
     83             moveAwayAnimators.add(rotateAway);
     84             Animator slideAway = ObjectAnimator.ofFloat(card, "translationX",
     85                     i == 0 ? 0 : X);
     86             slideAway.setStartDelay(200 * ((max) - i));
     87             slideAway.setDuration(100);
     88             moveAwayAnimators.add(slideAway);
     89 
     90             Animator rotateBack = ObjectAnimator.ofFloat(card, "rotationY", 0);
     91             rotateBack.setStartDelay(200 * i);
     92             moveBackAnimators.add(rotateBack);
     93             Animator slideBack = ObjectAnimator.ofFloat(card, "translationX", 0);
     94             slideBack.setStartDelay(200 * i);
     95             moveBackAnimators.add(slideBack);
     96 
     97             Animator away = ObjectAnimator.ofFloat(card, "translationZ", 0);
     98             away.setStartDelay(200 * i);
     99             awayAnimators.add(away);
    100 
    101             Animator collapse = ObjectAnimator.ofFloat(card, "translationY", 0);
    102             collapseAnimators.add(collapse);
    103         }
    104 
    105         AnimatorSet totalSet = new AnimatorSet();
    106         totalSet.playSequentially(
    107                 createSet(expandAnimators, 250),
    108                 createSet(towardAnimators, 0),
    109 
    110                 createSet(moveAwayAnimators, 250),
    111                 createSet(moveBackAnimators, 0),
    112 
    113                 createSet(awayAnimators, 250),
    114                 createSet(collapseAnimators, 0));
    115         totalSet.start();
    116         totalSet.addListener(new RepeatListener(totalSet));
    117     }
    118 
    119     public static class RepeatListener implements Animator.AnimatorListener {
    120         final Animator mRepeatAnimator;
    121         public RepeatListener(Animator repeatAnimator) {
    122             mRepeatAnimator = repeatAnimator;
    123         }
    124 
    125         @Override
    126         public void onAnimationStart(Animator animation) {}
    127 
    128         @Override
    129         public void onAnimationEnd(Animator animation) {
    130             if (animation == mRepeatAnimator) {
    131                 mRepeatAnimator.start();
    132             }
    133         }
    134 
    135         @Override
    136         public void onAnimationCancel(Animator animation) {}
    137 
    138         @Override
    139         public void onAnimationRepeat(Animator animation) {}
    140     }
    141 }
    142