Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
| 21 | import android.graphics.Bitmap; |
| 22 | import android.graphics.Canvas; |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 23 | import android.graphics.PorterDuff; |
Michael Jurka | daec1e8 | 2012-10-01 20:31:09 +0200 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 25 | import android.graphics.drawable.StateListDrawable; |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 26 | import android.widget.ImageView; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 27 | |
| 28 | public class HolographicViewHelper { |
| 29 | |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 30 | private final Canvas mTempCanvas = new Canvas(); |
| 31 | |
| 32 | private boolean mStatesUpdated; |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame^] | 33 | private int mHighlightColor, mHotwordColor; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 34 | |
| 35 | public HolographicViewHelper(Context context) { |
| 36 | Resources res = context.getResources(); |
| 37 | mHighlightColor = res.getColor(android.R.color.holo_blue_light); |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame^] | 38 | mHotwordColor = res.getColor(android.R.color.holo_green_light); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Generate the pressed/focused states if necessary. |
| 43 | */ |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 44 | void generatePressedFocusedStates(ImageView v) { |
| 45 | if (!mStatesUpdated && v != null) { |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 46 | mStatesUpdated = true; |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 47 | Bitmap original = createOriginalImage(v, mTempCanvas); |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame^] | 48 | Bitmap outline = createImageWithOverlay(v, mTempCanvas, mHighlightColor); |
| 49 | Bitmap hotword = createImageWithOverlay(v, mTempCanvas, mHotwordColor); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 50 | FastBitmapDrawable originalD = new FastBitmapDrawable(original); |
| 51 | FastBitmapDrawable outlineD = new FastBitmapDrawable(outline); |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame^] | 52 | FastBitmapDrawable hotwordD = new FastBitmapDrawable(hotword); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 53 | |
| 54 | StateListDrawable states = new StateListDrawable(); |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame^] | 55 | |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 56 | states.addState(new int[] {android.R.attr.state_pressed}, outlineD); |
| 57 | states.addState(new int[] {android.R.attr.state_focused}, outlineD); |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame^] | 58 | states.addState(new int[] {R.attr.stateHotwordOn}, hotwordD); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 59 | states.addState(new int[] {}, originalD); |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 60 | v.setImageDrawable(states); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 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 | /** |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 75 | * Creates a copy of the original image. |
| 76 | */ |
| 77 | private Bitmap createOriginalImage(ImageView v, Canvas canvas) { |
Michael Jurka | daec1e8 | 2012-10-01 20:31:09 +0200 | [diff] [blame] | 78 | final Drawable d = v.getDrawable(); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 79 | final Bitmap b = Bitmap.createBitmap( |
Michael Jurka | daec1e8 | 2012-10-01 20:31:09 +0200 | [diff] [blame] | 80 | d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 81 | |
| 82 | canvas.setBitmap(b); |
| 83 | canvas.save(); |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame^] | 84 | d.draw(canvas); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 85 | canvas.restore(); |
| 86 | canvas.setBitmap(null); |
| 87 | |
| 88 | return b; |
| 89 | } |
| 90 | |
| 91 | /** |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 92 | * Creates a new press state image which is the old image with a blue overlay. |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 93 | * Responsibility for the bitmap is transferred to the caller. |
| 94 | */ |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame^] | 95 | private Bitmap createImageWithOverlay(ImageView v, Canvas canvas, int color) { |
Michael Jurka | daec1e8 | 2012-10-01 20:31:09 +0200 | [diff] [blame] | 96 | final Drawable d = v.getDrawable(); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 97 | final Bitmap b = Bitmap.createBitmap( |
Michael Jurka | daec1e8 | 2012-10-01 20:31:09 +0200 | [diff] [blame] | 98 | d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 99 | |
| 100 | canvas.setBitmap(b); |
| 101 | canvas.save(); |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame^] | 102 | d.draw(canvas); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 103 | canvas.restore(); |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame^] | 104 | canvas.drawColor(color, PorterDuff.Mode.SRC_IN); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 105 | canvas.setBitmap(null); |
| 106 | |
| 107 | return b; |
| 108 | } |
| 109 | } |