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.ValueAnimator;
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.graphics.drawable.GradientDrawable;
     22 import android.util.AttributeSet;
     23 import android.widget.ImageView;
     24 import com.android.car.hvac.R;
     25 
     26 /**
     27  * Represents a single bar in the fan speed bar.
     28  */
     29 public class FanSpeedBarSegment extends ImageView {
     30     private boolean mTurnedOn;
     31 
     32     private final int mDotExpandedSize;
     33     private final int mDotSize;
     34 
     35     private final ValueAnimator mDotWidthExpandAnimator;
     36 
     37     private final ValueAnimator.AnimatorUpdateListener mExpandListener
     38             = new ValueAnimator.AnimatorUpdateListener() {
     39         @Override
     40         public void onAnimationUpdate(ValueAnimator animation) {
     41             int size = (int) animation.getAnimatedValue();
     42             GradientDrawable drawable = (GradientDrawable) getDrawable();
     43             drawable.setCornerRadius(size / 2);
     44             drawable.setSize(size, size);
     45         }
     46     };
     47 
     48     public FanSpeedBarSegment(Context context) {
     49         super(context);
     50     }
     51 
     52     public FanSpeedBarSegment(Context context, AttributeSet attrs) {
     53         super(context, attrs);
     54     }
     55 
     56     public FanSpeedBarSegment(Context context, AttributeSet attrs, int defStyleAttr) {
     57         super(context, attrs, defStyleAttr);
     58     }
     59 
     60     {
     61         setScaleType(ScaleType.CENTER);
     62 
     63         Resources res = getResources();
     64         mDotExpandedSize = res.getDimensionPixelSize(R.dimen.hvac_fan_speed_dot_expanded_size);
     65         mDotSize = res.getDimensionPixelSize(R.dimen.hvac_fan_speed_dot_size);
     66         mDotWidthExpandAnimator = ValueAnimator.ofInt(mDotSize, mDotExpandedSize);
     67         mDotWidthExpandAnimator.addUpdateListener(mExpandListener);
     68 
     69         GradientDrawable dot = new GradientDrawable();
     70         dot.setColor(res.getColor(R.color.hvac_fanspeed_segment_color));
     71         dot.setSize(mDotSize, mDotSize);
     72         dot.setCornerRadius(mDotSize / 2);
     73         setImageDrawable(dot);
     74     }
     75 
     76     public void playTurnOnAnimation(int duration, int delayMs) {
     77         mDotWidthExpandAnimator.setStartDelay(delayMs);
     78         mDotWidthExpandAnimator.setDuration(duration);
     79         mDotWidthExpandAnimator.start();
     80         mTurnedOn = true;
     81     }
     82 
     83     public void playTurnOffAnimation(int duration, int delayMs) {
     84         mDotWidthExpandAnimator.setStartDelay(delayMs);
     85         mDotWidthExpandAnimator.setDuration(duration);
     86         mDotWidthExpandAnimator.reverse();
     87         mTurnedOn = false;
     88     }
     89 
     90     public void setTurnedOn(boolean isOn) {
     91         mTurnedOn = isOn;
     92         GradientDrawable drawable = (GradientDrawable) getDrawable();
     93         if (mTurnedOn) {
     94             drawable.setCornerRadius(0);
     95             // Setting size -1, makes the drawable grow to the size of the image view.
     96             drawable.setSize(-1, -1);
     97         } else {
     98             drawable.setCornerRadius(mDotSize / 2);
     99             drawable.setSize(mDotSize, mDotSize);
    100         }
    101         setImageDrawable(drawable);
    102     }
    103 
    104     public boolean isTurnedOn() {
    105         return mTurnedOn;
    106     }
    107 }