1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.graphics.drawable;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
/**
* An extension of LayerDrawable that is intended to react to touch hotspots
* and reveal the second layer atop the first.
* <p>
* It can be defined in an XML file with the <code><reveal></code> element.
* Each Drawable in the transition is defined in a nested <code><item></code>.
* For more information, see the guide to <a href="{@docRoot}
* guide/topics/resources/drawable-resource.html">Drawable Resources</a>.
*
* @attr ref android.R.styleable#LayerDrawableItem_left
* @attr ref android.R.styleable#LayerDrawableItem_top
* @attr ref android.R.styleable#LayerDrawableItem_right
* @attr ref android.R.styleable#LayerDrawableItem_bottom
* @attr ref android.R.styleable#LayerDrawableItem_drawable
* @attr ref android.R.styleable#LayerDrawableItem_id
*/
public class RevealDrawable extends LayerDrawable {
private final Rect mTempRect = new Rect();
/** Lazily-created map of touch hotspot IDs to ripples. */
private SparseArray<Ripple> mTouchedRipples;
/** Lazily-created list of actively animating ripples. */
private ArrayList<Ripple> mActiveRipples;
/** Lazily-created runnable for scheduling invalidation. */
private Runnable mAnimationRunnable;
/** Whether the animation runnable has been posted. */
private boolean mAnimating;
/** Target density, used to scale density-independent pixels. */
private float mDensity = 1.0f;
/** Paint used to control appearance of ripples. */
private Paint mRipplePaint;
/** Paint used to control reveal layer masking. */
private Paint mMaskingPaint;
/**
* Create a new reveal drawable with the specified list of layers. At least
* two layers are required for this drawable to work properly.
*/
public RevealDrawable(Drawable[] layers) {
this(new RevealState(null, null, null), layers);
}
/**
* Create a new reveal drawable with no layers. To work correctly, at least
* two layers must be added to this drawable.
*
* @see #RevealDrawable(Drawable[])
*/
RevealDrawable() {
this(new RevealState(null, null, null), (Resources) null);
}
private RevealDrawable(RevealState state, Resources res) {
super(state, res);
}
private RevealDrawable(RevealState state, Drawable[] layers) {
super(layers, state);
}
@Override
public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
throws XmlPullParserException, IOException {
super.inflate(r, parser, attrs);
setTargetDensity(r.getDisplayMetrics());
setPaddingMode(PADDING_MODE_STACK);
}
@Override
LayerState createConstantState(LayerState state, Resources res) {
return new RevealState((RevealState) state, this, res);
}
/**
* Set the density at which this drawable will be rendered.
*
* @param metrics The display metrics for this drawable.
*/
private void setTargetDensity(DisplayMetrics metrics) {
if (mDensity != metrics.density) {
mDensity = metrics.density;
invalidateSelf();
}
}
/**
* @hide until hotspot APIs are finalized
*/
@Override
public boolean supportsHotspots() {
return true;
}
/**
* @hide until hotspot APIs are finalized
*/
@Override
public void setHotspot(int id, float x, float y) {
if (mTouchedRipples == null) {
mTouchedRipples = new SparseArray<Ripple>();
mActiveRipples = new ArrayList<Ripple>();
}
final Ripple ripple = mTouchedRipples.get(id);
if (ripple == null) {
final Rect padding = mTempRect;
getPadding(padding);
final Ripple newRipple = new Ripple(getBounds(), padding, x, y, mDensity);
newRipple.enter();
mActiveRipples.add(newRipple);
mTouchedRipples.put(id, newRipple);
} else {
ripple.move(x, y);
}
scheduleAnimation();
}
/**
* @hide until hotspot APIs are finalized
*/
@Override
public void removeHotspot(int id) {
if (mTouchedRipples == null) {
return;
}
final Ripple ripple = mTouchedRipples.get(id);
if (ripple != null) {
ripple.exit();
mTouchedRipples.remove(id);
scheduleAnimation();
}
}
/**
* @hide until hotspot APIs are finalized
*/
@Override
public void clearHotspots() {
if (mTouchedRipples == null) {
return;
}
final int n = mTouchedRipples.size();
for (int i = 0; i < n; i++) {
final Ripple ripple = mTouchedRipples.valueAt(i);
ripple.exit();
}
if (n > 0) {
mTouchedRipples.clear();
scheduleAnimation();
}
}
/**
* Schedules the next animation, if necessary.
*/
private void scheduleAnimation() {
if (mActiveRipples == null || mActiveRipples.isEmpty()) {
mAnimating = false;
} else if (!mAnimating) {
mAnimating = true;
if (mAnimationRunnable == null) {
mAnimationRunnable = new Runnable() {
@Override
public void run() {
mAnimating = false;
scheduleAnimation();
invalidateSelf();
}
};
}
scheduleSelf(mAnimationRunnable, SystemClock.uptimeMillis() + 1000 / 60);
}
}
@Override
public void draw(Canvas canvas) {
final int layerCount = getNumberOfLayers();
if (layerCount == 0) {
return;
}
getDrawable(0).draw(canvas);
final Rect bounds = getBounds();
final ArrayList<Ripple> activeRipples = mActiveRipples;
if (layerCount == 1 || bounds.isEmpty() || activeRipples == null
|| activeRipples.isEmpty()) {
// Nothing to reveal, we're done here.
return;
}
if (mRipplePaint == null) {
mRipplePaint = new Paint();
mRipplePaint.setAntiAlias(true);
}
// Draw ripple mask into a buffer that merges using SRC_OVER.
boolean needsMask = false;
int layerSaveCount = -1;
int n = activeRipples.size();
for (int i = 0; i < n; i++) {
final Ripple ripple = activeRipples.get(i);
if (!ripple.active()) {
activeRipples.remove(i);
i--;
n--;
} else {
if (layerSaveCount < 0) {
layerSaveCount = canvas.saveLayer(
bounds.left, bounds.top, bounds.right, bounds.bottom, null, 0);
// Ripples must be clipped to bounds, otherwise SRC_IN will
// miss them and we'll get artifacts.
canvas.clipRect(bounds);
}
needsMask |= ripple.draw(canvas, mRipplePaint);
}
}
// If a layer was saved, it contains the ripple mask. Draw the reveal
// into another layer and composite using SRC_IN, then composite onto
// the original canvas.
if (layerSaveCount >= 0) {
if (needsMask) {
if (mMaskingPaint == null) {
mMaskingPaint = new Paint();
mMaskingPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
}
// TODO: When Drawable.setXfermode() is supported by all drawables,
// we won't need an extra layer.
canvas.saveLayer(
bounds.left, bounds.top, bounds.right, bounds.bottom, mMaskingPaint, 0);
getDrawable(1).draw(canvas);
}
canvas.restoreToCount(layerSaveCount);
}
}
private static class RevealState extends LayerState {
public RevealState(RevealState orig, RevealDrawable owner, Resources res) {
super(orig, owner, res);
}
@Override
public Drawable newDrawable() {
return newDrawable(null);
}
@Override
public Drawable newDrawable(Resources res) {
return new RevealDrawable(this, res);
}
}
}
|