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; |
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; |
| 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 | */ |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 43 | void generatePressedFocusedStates(ImageView v) { |
| 44 | if (!mStatesUpdated && v != null) { |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 45 | mStatesUpdated = true; |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 46 | Bitmap original = createOriginalImage(v, mTempCanvas); |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 47 | Bitmap outline = createPressImage(v, mTempCanvas); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 48 | FastBitmapDrawable originalD = new FastBitmapDrawable(original); |
| 49 | FastBitmapDrawable outlineD = new FastBitmapDrawable(outline); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 50 | |
| 51 | StateListDrawable states = new StateListDrawable(); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 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); |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 55 | v.setImageDrawable(states); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 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 | /** |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 70 | * Creates a copy of the original image. |
| 71 | */ |
| 72 | private Bitmap createOriginalImage(ImageView v, Canvas canvas) { |
Michael Jurka | daec1e8 | 2012-10-01 20:31:09 +0200 | [diff] [blame^] | 73 | final Drawable d = v.getDrawable(); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 74 | final Bitmap b = Bitmap.createBitmap( |
Michael Jurka | daec1e8 | 2012-10-01 20:31:09 +0200 | [diff] [blame^] | 75 | d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 76 | |
| 77 | canvas.setBitmap(b); |
| 78 | canvas.save(); |
Michael Jurka | daec1e8 | 2012-10-01 20:31:09 +0200 | [diff] [blame^] | 79 | d.draw(canvas); |
Winson Chung | 4cc332a | 2012-09-10 12:46:30 -0700 | [diff] [blame] | 80 | canvas.restore(); |
| 81 | canvas.setBitmap(null); |
| 82 | |
| 83 | return b; |
| 84 | } |
| 85 | |
| 86 | /** |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 87 | * 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] | 88 | * Responsibility for the bitmap is transferred to the caller. |
| 89 | */ |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 90 | private Bitmap createPressImage(ImageView v, Canvas canvas) { |
Michael Jurka | daec1e8 | 2012-10-01 20:31:09 +0200 | [diff] [blame^] | 91 | final Drawable d = v.getDrawable(); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 92 | final Bitmap b = Bitmap.createBitmap( |
Michael Jurka | daec1e8 | 2012-10-01 20:31:09 +0200 | [diff] [blame^] | 93 | d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 94 | |
| 95 | canvas.setBitmap(b); |
| 96 | canvas.save(); |
Michael Jurka | daec1e8 | 2012-10-01 20:31:09 +0200 | [diff] [blame^] | 97 | d.draw(canvas); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 98 | canvas.restore(); |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 99 | canvas.drawColor(mHighlightColor, PorterDuff.Mode.SRC_IN); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 100 | canvas.setBitmap(null); |
| 101 | |
| 102 | return b; |
| 103 | } |
| 104 | } |