Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (c) 2016, 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 package com.android.car.hvac.ui;
     17 
     18 import android.animation.AnimatorSet;
     19 import android.animation.ValueAnimator;
     20 import android.content.Context;
     21 import android.content.res.Resources;
     22 import android.util.AttributeSet;
     23 import android.view.MotionEvent;
     24 import android.widget.LinearLayout;
     25 
     26 import com.android.car.hvac.R;
     27 
     28 /**
     29  * A row in the center HVAC panel.
     30  */
     31 public class HvacPanelRow extends LinearLayout {
     32     private static final int EXPAND_ANIMATION_TIME_MS = 300;
     33     private static final int EXPAND_ANIMATION_ALPHA_TIME_MS = 100;
     34 
     35     private final int mAnimationHeightShift;
     36 
     37     private boolean mDisableClickOnChildren;
     38 
     39     public HvacPanelRow(Context context) {
     40         super(context);
     41     }
     42 
     43     public HvacPanelRow(Context context, AttributeSet attrs) {
     44         super(context, attrs);
     45     }
     46 
     47     public HvacPanelRow(Context context, AttributeSet attrs, int defStyleAttr) {
     48         super(context, attrs, defStyleAttr);
     49     }
     50 
     51     {
     52         Resources res = getResources();
     53         mAnimationHeightShift
     54                 = res.getDimensionPixelSize(R.dimen.hvac_panel_row_animation_height_shift);
     55     }
     56 
     57     public AnimatorSet getExpandAnimation(long delayMs, float endAlpha) {
     58         return getTransitionAnimation(delayMs, endAlpha, true /* expanding */);
     59     }
     60 
     61     public AnimatorSet getCollapseAnimation(long delayMs, float startAlpha) {
     62         return getTransitionAnimation(delayMs, startAlpha, false /* expanding */);
     63     }
     64 
     65     /**
     66      * Disable/enable touch events on all child views of this panel
     67      */
     68     public void disablePanel(boolean disable) {
     69         mDisableClickOnChildren = disable;
     70     }
     71 
     72     @Override
     73     public boolean onInterceptTouchEvent(MotionEvent ev) {
     74         return mDisableClickOnChildren;
     75     }
     76 
     77     private AnimatorSet getTransitionAnimation(long delayMs, float maxAlpha, boolean expanding) {
     78         ValueAnimator alphaAnimator;
     79         ValueAnimator translationYAnimator;
     80 
     81         if (expanding) {
     82             alphaAnimator = ValueAnimator.ofFloat(0, maxAlpha);
     83             translationYAnimator = ValueAnimator.ofFloat(mAnimationHeightShift, 0);
     84         } else {
     85             alphaAnimator = ValueAnimator.ofFloat(maxAlpha, 0);
     86             translationYAnimator = ValueAnimator.ofFloat(0, mAnimationHeightShift);
     87         }
     88 
     89         alphaAnimator.setStartDelay(delayMs);
     90         alphaAnimator.setDuration(EXPAND_ANIMATION_ALPHA_TIME_MS);
     91         alphaAnimator.addUpdateListener(mAlphaUpdateListener);
     92         translationYAnimator.addUpdateListener(mTranslationYUpdateListener);
     93         translationYAnimator.setDuration(EXPAND_ANIMATION_TIME_MS);
     94 
     95         AnimatorSet set = new AnimatorSet();
     96         set.playTogether(alphaAnimator, translationYAnimator);
     97         return set;
     98     }
     99 
    100     private final ValueAnimator.AnimatorUpdateListener mTranslationYUpdateListener
    101             = new ValueAnimator.AnimatorUpdateListener() {
    102         @Override
    103         public void onAnimationUpdate(ValueAnimator animation) {
    104             float value = (float) animation.getAnimatedValue();
    105             setTranslationY(value);
    106         }
    107     };
    108 
    109     private final ValueAnimator.AnimatorUpdateListener mAlphaUpdateListener
    110             = new ValueAnimator.AnimatorUpdateListener() {
    111         @Override
    112         public void onAnimationUpdate(ValueAnimator animation) {
    113             float alpha = (float) animation.getAnimatedValue();
    114             setAlpha(alpha);
    115         }
    116     };
    117 }
    118