blob: 11e81b4d36efe5f84c987a8a78c9dd7ca1c7f718 [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
17package com.android.launcher2;
18
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;
Winson Chung7d3810d2011-10-07 13:11:56 -070024import android.graphics.drawable.StateListDrawable;
Winson Chung62054222011-11-03 13:12:50 -070025import android.widget.ImageView;
Winson Chung7d3810d2011-10-07 13:11:56 -070026
27public class HolographicViewHelper {
28
Winson Chung7d3810d2011-10-07 13:11:56 -070029 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 Chung62054222011-11-03 13:12:50 -070042 void generatePressedFocusedStates(ImageView v) {
43 if (!mStatesUpdated && v != null) {
Winson Chung7d3810d2011-10-07 13:11:56 -070044 mStatesUpdated = true;
Winson Chung62054222011-11-03 13:12:50 -070045 Bitmap outline = createGlowingOutline(v, mTempCanvas);
Winson Chung7d3810d2011-10-07 13:11:56 -070046 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);
Winson Chung62054222011-11-03 13:12:50 -070051 states.addState(new int[] {}, v.getDrawable());
52 v.setImageDrawable(states);
Winson Chung7d3810d2011-10-07 13:11:56 -070053 }
54 }
55
56 /**
57 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
58 * Responsibility for the bitmap is transferred to the caller.
59 */
Winson Chung62054222011-11-03 13:12:50 -070060 private Bitmap createGlowingOutline(ImageView v, Canvas canvas) {
Winson Chung7d3810d2011-10-07 13:11:56 -070061 final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
62 final Bitmap b = Bitmap.createBitmap(
63 v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
64
65 canvas.setBitmap(b);
66 canvas.save();
Winson Chung62054222011-11-03 13:12:50 -070067 v.getDrawable().draw(canvas);
Winson Chung7d3810d2011-10-07 13:11:56 -070068 canvas.restore();
Winson Chung62054222011-11-03 13:12:50 -070069 canvas.drawColor(mHighlightColor, PorterDuff.Mode.SRC_IN);
Winson Chung7d3810d2011-10-07 13:11:56 -070070 canvas.setBitmap(null);
71
72 return b;
73 }
74}