Home | History | Annotate | Download | only in flashlight
      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.wearable.flashlight;
     18 
     19 import android.animation.ArgbEvaluator;
     20 import android.content.Context;
     21 import android.graphics.Canvas;
     22 import android.graphics.Color;
     23 import android.os.Handler;
     24 import android.os.Message;
     25 import android.util.AttributeSet;
     26 import android.view.View;
     27 
     28 /**
     29  * Flashing party lights!
     30  */
     31 public class PartyLightView extends View {
     32 
     33     private int[] mColors = new int[] {
     34             Color.RED,
     35             Color.GREEN,
     36             Color.BLUE,
     37             Color.CYAN,
     38             Color.MAGENTA
     39     };
     40 
     41     private int mFromColorIndex;
     42     private int mToColorIndex;
     43 
     44     /**
     45      * Value b/t 0 and 1.
     46      */
     47     private float mProgress;
     48 
     49     private ArgbEvaluator mEvaluator;
     50 
     51     private int mCurrentColor;
     52 
     53     private Handler mHandler;
     54 
     55     public PartyLightView(Context context) {
     56         super(context);
     57         init();
     58     }
     59 
     60     public PartyLightView(Context context, AttributeSet attrs) {
     61         super(context, attrs);
     62         init();
     63     }
     64 
     65     @Override
     66     protected void onDraw(Canvas canvas) {
     67         canvas.drawColor(mCurrentColor);
     68         super.onDraw(canvas);
     69     }
     70 
     71     public void startCycling() {
     72         mHandler.sendEmptyMessage(0);
     73     }
     74 
     75     public void stopCycling() {
     76         mHandler.removeMessages(0);
     77     }
     78 
     79     private void init() {
     80         mEvaluator = new ArgbEvaluator();
     81         mHandler = new Handler() {
     82 
     83             @Override
     84             public void handleMessage(Message msg) {
     85                 mCurrentColor = getColor(mProgress, mColors[mFromColorIndex],
     86                         mColors[mToColorIndex]);
     87                 postInvalidate();
     88                 mProgress += 0.1;
     89                 if (mProgress > 1.0) {
     90                     mFromColorIndex = mToColorIndex;
     91                     // Find a new color.
     92                     mToColorIndex++;
     93                     if (mToColorIndex >= mColors.length) {
     94                         mToColorIndex = 0;
     95                     }
     96                 }
     97                 mHandler.sendEmptyMessageDelayed(0, 100);
     98             }
     99         };
    100     }
    101 
    102     private int getColor(float fraction, int colorStart, int colorEnd) {
    103         int startInt = colorStart;
    104         int startA = (startInt >> 24) & 0xff;
    105         int startR = (startInt >> 16) & 0xff;
    106         int startG = (startInt >> 8) & 0xff;
    107         int startB = startInt & 0xff;
    108 
    109         int endInt = colorEnd;
    110         int endA = (endInt >> 24) & 0xff;
    111         int endR = (endInt >> 16) & 0xff;
    112         int endG = (endInt >> 8) & 0xff;
    113         int endB = endInt & 0xff;
    114 
    115         return (startA + (int)(fraction * (endA - startA))) << 24 |
    116                 (startR + (int)(fraction * (endR - startR))) << 16 |
    117                 (startG + (int)(fraction * (endG - startG))) << 8 |
    118                 ((startB + (int)(fraction * (endB - startB))));
    119     }
    120 }
    121