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 | |
| 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; |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 23 | import android.graphics.PorterDuff; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 24 | import android.graphics.drawable.StateListDrawable; |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 25 | import android.widget.ImageView; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 26 | |
| 27 | public class HolographicViewHelper { |
| 28 | |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 29 | private final Canvas mTempCanvas = new Canvas(); |
| 30 | |
| 31 | private boolean mStatesUpdated; |
| 32 | private int mHighlightColor; |
| 33 | |
| 34 | public HolographicViewHelper(Context context) { |
| 35 | Resources res = context.getResources(); |
| 36 | mHighlightColor = res.getColor(android.R.color.holo_blue_light); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Generate the pressed/focused states if necessary. |
| 41 | */ |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 42 | void generatePressedFocusedStates(ImageView v) { |
| 43 | if (!mStatesUpdated && v != null) { |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 44 | mStatesUpdated = true; |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 45 | Bitmap original = createOriginalImage(v, mTempCanvas); |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 46 | Bitmap outline = createPressImage(v, mTempCanvas); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 47 | FastBitmapDrawable originalD = new FastBitmapDrawable(original); |
| 48 | FastBitmapDrawable outlineD = new FastBitmapDrawable(outline); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 49 | |
| 50 | StateListDrawable states = new StateListDrawable(); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 51 | states.addState(new int[] {android.R.attr.state_pressed}, outlineD); |
| 52 | states.addState(new int[] {android.R.attr.state_focused}, outlineD); |
| 53 | states.addState(new int[] {}, originalD); |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 54 | v.setImageDrawable(states); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 59 | * Invalidates the pressed/focused states. |
| 60 | */ |
| 61 | void invalidatePressedFocusedStates(ImageView v) { |
| 62 | mStatesUpdated = false; |
| 63 | if (v != null) { |
| 64 | v.invalidate(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 69 | * Creates a copy of the original image. |
| 70 | */ |
| 71 | private Bitmap createOriginalImage(ImageView v, Canvas canvas) { |
| 72 | final Bitmap b = Bitmap.createBitmap( |
| 73 | v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); |
| 74 | |
| 75 | canvas.setBitmap(b); |
| 76 | canvas.save(); |
| 77 | v.draw(canvas); |
| 78 | canvas.restore(); |
| 79 | canvas.setBitmap(null); |
| 80 | |
| 81 | return b; |
| 82 | } |
| 83 | |
| 84 | /** |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 85 | * 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] | 86 | * Responsibility for the bitmap is transferred to the caller. |
| 87 | */ |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 88 | private Bitmap createPressImage(ImageView v, Canvas canvas) { |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 89 | final Bitmap b = Bitmap.createBitmap( |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 90 | v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 91 | |
| 92 | canvas.setBitmap(b); |
| 93 | canvas.save(); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 94 | v.draw(canvas); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 95 | canvas.restore(); |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 96 | canvas.drawColor(mHighlightColor, PorterDuff.Mode.SRC_IN); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 97 | canvas.setBitmap(null); |
| 98 | |
| 99 | return b; |
| 100 | } |
| 101 | } |