Home | History | Annotate | Download | only in launcher3
      1 /*
      2  * Copyright (C) 2011 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.launcher3;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.graphics.Bitmap;
     22 import android.graphics.Canvas;
     23 import android.graphics.PorterDuff;
     24 import android.graphics.drawable.Drawable;
     25 import android.graphics.drawable.StateListDrawable;
     26 import android.widget.ImageView;
     27 
     28 public class HolographicViewHelper {
     29 
     30     private final Canvas mTempCanvas = new Canvas();
     31 
     32     private boolean mStatesUpdated;
     33     private int mHighlightColor, mHotwordColor;
     34 
     35     public HolographicViewHelper(Context context) {
     36         Resources res = context.getResources();
     37         mHighlightColor = res.getColor(android.R.color.holo_blue_light);
     38         mHotwordColor = res.getColor(android.R.color.holo_green_light);
     39     }
     40 
     41     /**
     42      * Generate the pressed/focused states if necessary.
     43      */
     44     void generatePressedFocusedStates(ImageView v) {
     45         if (!mStatesUpdated && v != null) {
     46             mStatesUpdated = true;
     47             Bitmap original = createOriginalImage(v, mTempCanvas);
     48             Bitmap outline = createImageWithOverlay(v, mTempCanvas, mHighlightColor);
     49             Bitmap hotword = createImageWithOverlay(v, mTempCanvas, mHotwordColor);
     50             FastBitmapDrawable originalD = new FastBitmapDrawable(original);
     51             FastBitmapDrawable outlineD = new FastBitmapDrawable(outline);
     52             FastBitmapDrawable hotwordD = new FastBitmapDrawable(hotword);
     53 
     54             StateListDrawable states = new StateListDrawable();
     55 
     56             states.addState(new int[] {android.R.attr.state_pressed}, outlineD);
     57             states.addState(new int[] {android.R.attr.state_focused}, outlineD);
     58             states.addState(new int[] {R.attr.stateHotwordOn}, hotwordD);
     59             states.addState(new int[] {}, originalD);
     60             v.setImageDrawable(states);
     61         }
     62     }
     63 
     64     /**
     65      * Invalidates the pressed/focused states.
     66      */
     67     void invalidatePressedFocusedStates(ImageView v) {
     68         mStatesUpdated = false;
     69         if (v != null) {
     70             v.invalidate();
     71         }
     72     }
     73 
     74     /**
     75      * Creates a copy of the original image.
     76      */
     77     private Bitmap createOriginalImage(ImageView v, Canvas canvas) {
     78         final Drawable d = v.getDrawable();
     79         final Bitmap b = Bitmap.createBitmap(
     80                 d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
     81 
     82         canvas.setBitmap(b);
     83         canvas.save();
     84         d.draw(canvas);
     85         canvas.restore();
     86         canvas.setBitmap(null);
     87 
     88         return b;
     89     }
     90 
     91     /**
     92      * Creates a new press state image which is the old image with a blue overlay.
     93      * Responsibility for the bitmap is transferred to the caller.
     94      */
     95     private Bitmap createImageWithOverlay(ImageView v, Canvas canvas, int color) {
     96         final Drawable d = v.getDrawable();
     97         final Bitmap b = Bitmap.createBitmap(
     98                 d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
     99 
    100         canvas.setBitmap(b);
    101         canvas.save();
    102         d.draw(canvas);
    103         canvas.restore();
    104         canvas.drawColor(color, PorterDuff.Mode.SRC_IN);
    105         canvas.setBitmap(null);
    106 
    107         return b;
    108     }
    109 }
    110