blob: 93d78535184b59e43fe38d527ef3de7a21f03f07 [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;
Michael Jurkadaec1e82012-10-01 20:31:09 +020024import android.graphics.drawable.Drawable;
Winson Chung7d3810d2011-10-07 13:11:56 -070025import android.graphics.drawable.StateListDrawable;
Winson Chung62054222011-11-03 13:12:50 -070026import android.widget.ImageView;
Winson Chung7d3810d2011-10-07 13:11:56 -070027
28public class HolographicViewHelper {
29
Winson Chung7d3810d2011-10-07 13:11:56 -070030 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 Chung62054222011-11-03 13:12:50 -070043 void generatePressedFocusedStates(ImageView v) {
44 if (!mStatesUpdated && v != null) {
Winson Chung7d3810d2011-10-07 13:11:56 -070045 mStatesUpdated = true;
Winson Chung4cc332a2012-09-10 12:46:30 -070046 Bitmap original = createOriginalImage(v, mTempCanvas);
Winson Chungbb185bd2011-11-21 12:31:42 -080047 Bitmap outline = createPressImage(v, mTempCanvas);
Winson Chung4cc332a2012-09-10 12:46:30 -070048 FastBitmapDrawable originalD = new FastBitmapDrawable(original);
49 FastBitmapDrawable outlineD = new FastBitmapDrawable(outline);
Winson Chung7d3810d2011-10-07 13:11:56 -070050
51 StateListDrawable states = new StateListDrawable();
Winson Chung4cc332a2012-09-10 12:46:30 -070052 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 Chung62054222011-11-03 13:12:50 -070055 v.setImageDrawable(states);
Winson Chung7d3810d2011-10-07 13:11:56 -070056 }
57 }
58
59 /**
Winson Chungbb185bd2011-11-21 12:31:42 -080060 * 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 Chung4cc332a2012-09-10 12:46:30 -070070 * Creates a copy of the original image.
71 */
72 private Bitmap createOriginalImage(ImageView v, Canvas canvas) {
Michael Jurkadaec1e82012-10-01 20:31:09 +020073 final Drawable d = v.getDrawable();
Winson Chung4cc332a2012-09-10 12:46:30 -070074 final Bitmap b = Bitmap.createBitmap(
Michael Jurkadaec1e82012-10-01 20:31:09 +020075 d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Winson Chung4cc332a2012-09-10 12:46:30 -070076
77 canvas.setBitmap(b);
78 canvas.save();
Michael Jurkadaec1e82012-10-01 20:31:09 +020079 d.draw(canvas);
Winson Chung4cc332a2012-09-10 12:46:30 -070080 canvas.restore();
81 canvas.setBitmap(null);
82
83 return b;
84 }
85
86 /**
Winson Chungbb185bd2011-11-21 12:31:42 -080087 * Creates a new press state image which is the old image with a blue overlay.
Winson Chung7d3810d2011-10-07 13:11:56 -070088 * Responsibility for the bitmap is transferred to the caller.
89 */
Winson Chungbb185bd2011-11-21 12:31:42 -080090 private Bitmap createPressImage(ImageView v, Canvas canvas) {
Michael Jurkadaec1e82012-10-01 20:31:09 +020091 final Drawable d = v.getDrawable();
Winson Chung7d3810d2011-10-07 13:11:56 -070092 final Bitmap b = Bitmap.createBitmap(
Michael Jurkadaec1e82012-10-01 20:31:09 +020093 d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Winson Chung7d3810d2011-10-07 13:11:56 -070094
95 canvas.setBitmap(b);
96 canvas.save();
Michael Jurkadaec1e82012-10-01 20:31:09 +020097 d.draw(canvas);
Winson Chung7d3810d2011-10-07 13:11:56 -070098 canvas.restore();
Winson Chung62054222011-11-03 13:12:50 -070099 canvas.drawColor(mHighlightColor, PorterDuff.Mode.SRC_IN);
Winson Chung7d3810d2011-10-07 13:11:56 -0700100 canvas.setBitmap(null);
101
102 return b;
103 }
104}