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