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; |
| 23 | import android.graphics.drawable.StateListDrawable; |
| 24 | import android.view.View; |
| 25 | |
| 26 | public class HolographicViewHelper { |
| 27 | |
| 28 | private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper(); |
| 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 | */ |
| 42 | void generatePressedFocusedStates(View v) { |
| 43 | if (!mStatesUpdated) { |
| 44 | mStatesUpdated = true; |
| 45 | Bitmap outline = createGlowingOutline(v, mTempCanvas, mHighlightColor, mHighlightColor); |
| 46 | FastBitmapDrawable d = new FastBitmapDrawable(outline); |
| 47 | |
| 48 | StateListDrawable states = new StateListDrawable(); |
| 49 | states.addState(new int[] {android.R.attr.state_pressed}, d); |
| 50 | states.addState(new int[] {android.R.attr.state_focused}, d); |
| 51 | v.setBackgroundDrawable(states); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. |
| 57 | * Responsibility for the bitmap is transferred to the caller. |
| 58 | */ |
| 59 | private Bitmap createGlowingOutline(View v, Canvas canvas, int outlineColor, int glowColor) { |
| 60 | final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS; |
| 61 | final Bitmap b = Bitmap.createBitmap( |
| 62 | v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); |
| 63 | |
| 64 | canvas.setBitmap(b); |
| 65 | canvas.save(); |
| 66 | v.draw(canvas); |
| 67 | canvas.restore(); |
| 68 | mOutlineHelper.applyOuterBlur(b, canvas, outlineColor); |
| 69 | canvas.setBitmap(null); |
| 70 | |
| 71 | return b; |
| 72 | } |
| 73 | } |