Home | History | Annotate | Download | only in deskclock
      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.android.deskclock;
     18 
     19 import android.animation.ArgbEvaluator;
     20 import android.animation.ObjectAnimator;
     21 import android.animation.PropertyValuesHolder;
     22 import android.animation.TypeEvaluator;
     23 import android.animation.ValueAnimator;
     24 import android.util.Property;
     25 import android.view.View;
     26 import android.view.animation.Interpolator;
     27 import android.widget.ImageView;
     28 
     29 import java.lang.reflect.InvocationTargetException;
     30 import java.lang.reflect.Method;
     31 
     32 public class AnimatorUtils {
     33 
     34     public static final long ANIM_DURATION_SHORT = 266L;  // 8/30 frames long
     35 
     36     public static final Interpolator DECELERATE_ACCELERATE_INTERPOLATOR = new Interpolator() {
     37         @Override
     38         public float getInterpolation(float x) {
     39             return 0.5f + 4.0f * (x - 0.5f) * (x - 0.5f) * (x - 0.5f);
     40         }
     41     };
     42 
     43     public static final Property<View, Integer> BACKGROUND_ALPHA =
     44             new Property<View, Integer>(Integer.class, "background.alpha") {
     45         @Override
     46         public Integer get(View view) {
     47             return view.getBackground().getAlpha();
     48         }
     49 
     50         @Override
     51         public void set(View view, Integer value) {
     52             view.getBackground().setAlpha(value);
     53         }
     54     };
     55 
     56     public static final Property<ImageView, Integer> DRAWABLE_ALPHA =
     57             new Property<ImageView, Integer>(Integer.class, "drawable.alpha") {
     58         @Override
     59         public Integer get(ImageView view) {
     60             return view.getDrawable().getAlpha();
     61         }
     62 
     63         @Override
     64         public void set(ImageView view, Integer value) {
     65             view.getDrawable().setAlpha(value);
     66         }
     67     };
     68 
     69     public static final Property<ImageView, Integer> DRAWABLE_TINT =
     70             new Property<ImageView, Integer>(Integer.class, "drawable.tint") {
     71         @Override
     72         public Integer get(ImageView view) {
     73             return null;
     74         }
     75 
     76         @Override
     77         public void set(ImageView view, Integer value) {
     78             view.getDrawable().setTint(value);
     79         }
     80     };
     81 
     82     public static final TypeEvaluator ARGB_EVALUATOR = new ArgbEvaluator();
     83 
     84     public static void start(ValueAnimator... animators) {
     85         for (ValueAnimator animator : animators) {
     86             final float fraction = animator.getAnimatedFraction();
     87             if (fraction < 1.0f) {
     88                 animator.start();
     89             }
     90         }
     91     }
     92 
     93     public static void reverse(ValueAnimator... animators) {
     94         for (ValueAnimator animator : animators) {
     95             final float fraction = animator.getAnimatedFraction();
     96             if (fraction > 0.0f) {
     97                 animator.reverse();
     98             }
     99         }
    100     }
    101 
    102     public static void cancel(ValueAnimator... animators) {
    103         for (ValueAnimator animator : animators) {
    104             animator.cancel();
    105         }
    106     }
    107 
    108     public static ValueAnimator getScaleAnimator(View view, float... values) {
    109         return ObjectAnimator.ofPropertyValuesHolder(view,
    110                 PropertyValuesHolder.ofFloat(View.SCALE_X, values),
    111                 PropertyValuesHolder.ofFloat(View.SCALE_Y, values));
    112     }
    113 }
    114