blob: 7ef035555874cd8571424a87b690b251ebd10752 [file] [log] [blame]
Winson Chung7d3810d2011-10-07 13:11:56 -07001/*
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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung7d3810d2011-10-07 13:11:56 -070018
19import android.content.Context;
20import android.content.res.Resources;
21import android.graphics.Bitmap;
22import android.graphics.Canvas;
Winson Chung62054222011-11-03 13:12:50 -070023import android.graphics.PorterDuff;
Michael Jurkadaec1e82012-10-01 20:31:09 +020024import android.graphics.drawable.Drawable;
Winson Chung7d3810d2011-10-07 13:11:56 -070025import android.graphics.drawable.StateListDrawable;
Winson Chung62054222011-11-03 13:12:50 -070026import android.widget.ImageView;
Winson Chung7d3810d2011-10-07 13:11:56 -070027
28public class HolographicViewHelper {
29
Winson Chung7d3810d2011-10-07 13:11:56 -070030 private final Canvas mTempCanvas = new Canvas();
31
32 private boolean mStatesUpdated;
Cristina Stancud7919982013-08-16 11:15:40 +010033 private int mHighlightColor, mHotwordColor;
Winson Chung7d3810d2011-10-07 13:11:56 -070034
35 public HolographicViewHelper(Context context) {
36 Resources res = context.getResources();
37 mHighlightColor = res.getColor(android.R.color.holo_blue_light);
Cristina Stancud7919982013-08-16 11:15:40 +010038 mHotwordColor = res.getColor(android.R.color.holo_green_light);
Winson Chung7d3810d2011-10-07 13:11:56 -070039 }
40
41 /**
42 * Generate the pressed/focused states if necessary.
43 */
Winson Chung62054222011-11-03 13:12:50 -070044 void generatePressedFocusedStates(ImageView v) {
45 if (!mStatesUpdated && v != null) {
Winson Chung7d3810d2011-10-07 13:11:56 -070046 mStatesUpdated = true;
Winson Chung4cc332a2012-09-10 12:46:30 -070047 Bitmap original = createOriginalImage(v, mTempCanvas);
Cristina Stancud7919982013-08-16 11:15:40 +010048 Bitmap outline = createImageWithOverlay(v, mTempCanvas, mHighlightColor);
49 Bitmap hotword = createImageWithOverlay(v, mTempCanvas, mHotwordColor);
Winson Chung4cc332a2012-09-10 12:46:30 -070050 FastBitmapDrawable originalD = new FastBitmapDrawable(original);
51 FastBitmapDrawable outlineD = new FastBitmapDrawable(outline);
Cristina Stancud7919982013-08-16 11:15:40 +010052 FastBitmapDrawable hotwordD = new FastBitmapDrawable(hotword);
Winson Chung7d3810d2011-10-07 13:11:56 -070053
54 StateListDrawable states = new StateListDrawable();
Cristina Stancud7919982013-08-16 11:15:40 +010055
Winson Chung4cc332a2012-09-10 12:46:30 -070056 states.addState(new int[] {android.R.attr.state_pressed}, outlineD);
57 states.addState(new int[] {android.R.attr.state_focused}, outlineD);
Cristina Stancud7919982013-08-16 11:15:40 +010058 states.addState(new int[] {R.attr.stateHotwordOn}, hotwordD);
Winson Chung4cc332a2012-09-10 12:46:30 -070059 states.addState(new int[] {}, originalD);
Winson Chung62054222011-11-03 13:12:50 -070060 v.setImageDrawable(states);
Winson Chung7d3810d2011-10-07 13:11:56 -070061 }
62 }
63
64 /**
Winson Chungbb185bd2011-11-21 12:31:42 -080065 * 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 /**
Winson Chung4cc332a2012-09-10 12:46:30 -070075 * Creates a copy of the original image.
76 */
77 private Bitmap createOriginalImage(ImageView v, Canvas canvas) {
Michael Jurkadaec1e82012-10-01 20:31:09 +020078 final Drawable d = v.getDrawable();
Winson Chung4cc332a2012-09-10 12:46:30 -070079 final Bitmap b = Bitmap.createBitmap(
Michael Jurkadaec1e82012-10-01 20:31:09 +020080 d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Winson Chung4cc332a2012-09-10 12:46:30 -070081
82 canvas.setBitmap(b);
83 canvas.save();
Cristina Stancud7919982013-08-16 11:15:40 +010084 d.draw(canvas);
Winson Chung4cc332a2012-09-10 12:46:30 -070085 canvas.restore();
86 canvas.setBitmap(null);
87
88 return b;
89 }
90
91 /**
Winson Chungbb185bd2011-11-21 12:31:42 -080092 * Creates a new press state image which is the old image with a blue overlay.
Winson Chung7d3810d2011-10-07 13:11:56 -070093 * Responsibility for the bitmap is transferred to the caller.
94 */
Cristina Stancud7919982013-08-16 11:15:40 +010095 private Bitmap createImageWithOverlay(ImageView v, Canvas canvas, int color) {
Michael Jurkadaec1e82012-10-01 20:31:09 +020096 final Drawable d = v.getDrawable();
Winson Chung7d3810d2011-10-07 13:11:56 -070097 final Bitmap b = Bitmap.createBitmap(
Michael Jurkadaec1e82012-10-01 20:31:09 +020098 d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Winson Chung7d3810d2011-10-07 13:11:56 -070099
100 canvas.setBitmap(b);
101 canvas.save();
Cristina Stancud7919982013-08-16 11:15:40 +0100102 d.draw(canvas);
Winson Chung7d3810d2011-10-07 13:11:56 -0700103 canvas.restore();
Cristina Stancud7919982013-08-16 11:15:40 +0100104 canvas.drawColor(color, PorterDuff.Mode.SRC_IN);
Winson Chung7d3810d2011-10-07 13:11:56 -0700105 canvas.setBitmap(null);
106
107 return b;
108 }
109}