Home | History | Annotate | Download | only in launcher2
      1 /*
      2  * Copyright (C) 2012 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.android.launcher2;
     18 
     19 import android.animation.Animator;
     20 import android.animation.Animator.AnimatorListener;
     21 import android.animation.TimeInterpolator;
     22 import android.view.ViewPropertyAnimator;
     23 import android.view.View;
     24 
     25 import java.util.ArrayList;
     26 import java.util.EnumSet;
     27 
     28 public class LauncherViewPropertyAnimator extends Animator implements AnimatorListener {
     29     enum Properties {
     30             TRANSLATION_X,
     31             TRANSLATION_Y,
     32             SCALE_X,
     33             SCALE_Y,
     34             ROTATION_Y,
     35             ALPHA,
     36             START_DELAY,
     37             DURATION,
     38             INTERPOLATOR
     39     }
     40     EnumSet<Properties> mPropertiesToSet = EnumSet.noneOf(Properties.class);
     41     ViewPropertyAnimator mViewPropertyAnimator;
     42     View mTarget;
     43 
     44     float mTranslationX;
     45     float mTranslationY;
     46     float mScaleX;
     47     float mScaleY;
     48     float mRotationY;
     49     float mAlpha;
     50     long mStartDelay;
     51     long mDuration;
     52     TimeInterpolator mInterpolator;
     53     ArrayList<Animator.AnimatorListener> mListeners;
     54     boolean mRunning = false;
     55 
     56     public LauncherViewPropertyAnimator(View target) {
     57         mTarget = target;
     58         mListeners = new ArrayList<Animator.AnimatorListener>();
     59     }
     60 
     61     @Override
     62     public void addListener(Animator.AnimatorListener listener) {
     63         mListeners.add(listener);
     64     }
     65 
     66     @Override
     67     public void cancel() {
     68         if (mViewPropertyAnimator != null) {
     69             mViewPropertyAnimator.cancel();
     70         }
     71     }
     72 
     73     @Override
     74     public Animator clone() {
     75         throw new RuntimeException("Not implemented");
     76     }
     77 
     78     @Override
     79     public void end() {
     80         throw new RuntimeException("Not implemented");
     81     }
     82 
     83     @Override
     84     public long getDuration() {
     85         return mDuration;
     86     }
     87 
     88     @Override
     89     public ArrayList<Animator.AnimatorListener> getListeners() {
     90         return mListeners;
     91     }
     92 
     93     @Override
     94     public long getStartDelay() {
     95         return mStartDelay;
     96     }
     97 
     98     @Override
     99     public void onAnimationCancel(Animator animation) {
    100         for (int i = 0; i < mListeners.size(); i++) {
    101             Animator.AnimatorListener listener = mListeners.get(i);
    102             listener.onAnimationCancel(this);
    103         }
    104         mRunning = false;
    105     }
    106 
    107     @Override
    108     public void onAnimationEnd(Animator animation) {
    109         for (int i = 0; i < mListeners.size(); i++) {
    110             Animator.AnimatorListener listener = mListeners.get(i);
    111             listener.onAnimationEnd(this);
    112         }
    113         mRunning = false;
    114     }
    115 
    116     @Override
    117     public void onAnimationRepeat(Animator animation) {
    118         for (int i = 0; i < mListeners.size(); i++) {
    119             Animator.AnimatorListener listener = mListeners.get(i);
    120             listener.onAnimationRepeat(this);
    121         }
    122     }
    123 
    124     @Override
    125     public void onAnimationStart(Animator animation) {
    126         for (int i = 0; i < mListeners.size(); i++) {
    127             Animator.AnimatorListener listener = mListeners.get(i);
    128             listener.onAnimationStart(this);
    129         }
    130         mRunning = true;
    131     }
    132 
    133     @Override
    134     public boolean isRunning() {
    135         return mRunning;
    136     }
    137 
    138     @Override
    139     public boolean isStarted() {
    140         return mViewPropertyAnimator != null;
    141     }
    142 
    143     @Override
    144     public void removeAllListeners() {
    145         mListeners.clear();
    146     }
    147 
    148     @Override
    149     public void removeListener(Animator.AnimatorListener listener) {
    150         mListeners.remove(listener);
    151     }
    152 
    153     @Override
    154     public Animator setDuration(long duration) {
    155         mPropertiesToSet.add(Properties.DURATION);
    156         mDuration = duration;
    157         return this;
    158     }
    159 
    160     @Override
    161     public void setInterpolator(TimeInterpolator value) {
    162         mPropertiesToSet.add(Properties.INTERPOLATOR);
    163         mInterpolator = value;
    164     }
    165 
    166     @Override
    167     public void setStartDelay(long startDelay) {
    168         mPropertiesToSet.add(Properties.START_DELAY);
    169         mStartDelay = startDelay;
    170     }
    171 
    172     @Override
    173     public void setTarget(Object target) {
    174         throw new RuntimeException("Not implemented");
    175     }
    176 
    177     @Override
    178     public void setupEndValues() {
    179 
    180     }
    181 
    182     @Override
    183     public void setupStartValues() {
    184     }
    185 
    186     @Override
    187     public void start() {
    188         mViewPropertyAnimator = mTarget.animate();
    189         if (mPropertiesToSet.contains(Properties.TRANSLATION_X)) {
    190             mViewPropertyAnimator.translationX(mTranslationX);
    191         }
    192         if (mPropertiesToSet.contains(Properties.TRANSLATION_Y)) {
    193             mViewPropertyAnimator.translationY(mTranslationY);
    194         }
    195         if (mPropertiesToSet.contains(Properties.SCALE_X)) {
    196             mViewPropertyAnimator.scaleX(mScaleX);
    197         }
    198         if (mPropertiesToSet.contains(Properties.ROTATION_Y)) {
    199             mViewPropertyAnimator.rotationY(mRotationY);
    200         }
    201         if (mPropertiesToSet.contains(Properties.SCALE_Y)) {
    202             mViewPropertyAnimator.scaleY(mScaleY);
    203         }
    204         if (mPropertiesToSet.contains(Properties.ALPHA)) {
    205             mViewPropertyAnimator.alpha(mAlpha);
    206         }
    207         if (mPropertiesToSet.contains(Properties.START_DELAY)) {
    208             mViewPropertyAnimator.setStartDelay(mStartDelay);
    209         }
    210         if (mPropertiesToSet.contains(Properties.DURATION)) {
    211             mViewPropertyAnimator.setDuration(mDuration);
    212         }
    213         if (mPropertiesToSet.contains(Properties.INTERPOLATOR)) {
    214             mViewPropertyAnimator.setInterpolator(mInterpolator);
    215         }
    216         mViewPropertyAnimator.setListener(this);
    217         mViewPropertyAnimator.start();
    218     }
    219 
    220     public LauncherViewPropertyAnimator translationX(float value) {
    221         mPropertiesToSet.add(Properties.TRANSLATION_X);
    222         mTranslationX = value;
    223         return this;
    224     }
    225 
    226     public LauncherViewPropertyAnimator translationY(float value) {
    227         mPropertiesToSet.add(Properties.TRANSLATION_Y);
    228         mTranslationY = value;
    229         return this;
    230     }
    231 
    232     public LauncherViewPropertyAnimator scaleX(float value) {
    233         mPropertiesToSet.add(Properties.SCALE_X);
    234         mScaleX = value;
    235         return this;
    236     }
    237 
    238     public LauncherViewPropertyAnimator scaleY(float value) {
    239         mPropertiesToSet.add(Properties.SCALE_Y);
    240         mScaleY = value;
    241         return this;
    242     }
    243 
    244     public LauncherViewPropertyAnimator rotationY(float value) {
    245         mPropertiesToSet.add(Properties.ROTATION_Y);
    246         mRotationY = value;
    247         return this;
    248     }
    249 
    250     public LauncherViewPropertyAnimator alpha(float value) {
    251         mPropertiesToSet.add(Properties.ALPHA);
    252         mAlpha = value;
    253         return this;
    254     }
    255 }
    256