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
308
309
310
311
312
313
314
315
316
|
/*
* Copyright (C) 2017 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.widget;
import android.annotation.FloatRange;
import android.annotation.NonNull;
import android.annotation.TestApi;
import android.annotation.UiThread;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.PixelCopy;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewParent;
import android.view.ViewRootImpl;
import com.android.internal.util.Preconditions;
/**
* Android magnifier widget. Can be used by any view which is attached to a window.
*/
@UiThread
public final class Magnifier {
// Use this to specify that a previous configuration value does not exist.
private static final int NONEXISTENT_PREVIOUS_CONFIG_VALUE = -1;
// The view to which this magnifier is attached.
private final View mView;
// The coordinates of the view in the surface.
private final int[] mViewCoordinatesInSurface;
// The window containing the magnifier.
private final PopupWindow mWindow;
// The center coordinates of the window containing the magnifier.
private final Point mWindowCoords = new Point();
// The width of the window containing the magnifier.
private final int mWindowWidth;
// The height of the window containing the magnifier.
private final int mWindowHeight;
// The bitmap used to display the contents of the magnifier.
private final Bitmap mBitmap;
// The center coordinates of the content that is to be magnified.
private final Point mCenterZoomCoords = new Point();
// The callback of the pixel copy request will be invoked on this Handler when
// the copy is finished.
private final Handler mPixelCopyHandler = Handler.getMain();
// Current magnification scale.
private final float mZoomScale;
// Variables holding previous states, used for detecting redundant calls and invalidation.
private final Point mPrevStartCoordsInSurface = new Point(
NONEXISTENT_PREVIOUS_CONFIG_VALUE, NONEXISTENT_PREVIOUS_CONFIG_VALUE);
private final PointF mPrevPosInView = new PointF(
NONEXISTENT_PREVIOUS_CONFIG_VALUE, NONEXISTENT_PREVIOUS_CONFIG_VALUE);
private final Rect mPixelCopyRequestRect = new Rect();
/**
* Initializes a magnifier.
*
* @param view the view for which this magnifier is attached
*/
public Magnifier(@NonNull View view) {
mView = Preconditions.checkNotNull(view);
final Context context = mView.getContext();
final float elevation = context.getResources().getDimension(
com.android.internal.R.dimen.magnifier_elevation);
final View content = LayoutInflater.from(context).inflate(
com.android.internal.R.layout.magnifier, null);
content.findViewById(com.android.internal.R.id.magnifier_inner).setClipToOutline(true);
mWindowWidth = context.getResources().getDimensionPixelSize(
com.android.internal.R.dimen.magnifier_width);
mWindowHeight = context.getResources().getDimensionPixelSize(
com.android.internal.R.dimen.magnifier_height);
mZoomScale = context.getResources().getFloat(
com.android.internal.R.dimen.magnifier_zoom_scale);
// The view's surface coordinates will not be updated until the magnifier is first shown.
mViewCoordinatesInSurface = new int[2];
mWindow = new PopupWindow(context);
mWindow.setContentView(content);
mWindow.setWidth(mWindowWidth);
mWindow.setHeight(mWindowHeight);
mWindow.setElevation(elevation);
mWindow.setTouchable(false);
mWindow.setBackgroundDrawable(null);
final int bitmapWidth = Math.round(mWindowWidth / mZoomScale);
final int bitmapHeight = Math.round(mWindowHeight / mZoomScale);
mBitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
getImageView().setImageBitmap(mBitmap);
}
/**
* Shows the magnifier on the screen.
*
* @param xPosInView horizontal coordinate of the center point of the magnifier source relative
* to the view. The lower end is clamped to 0 and the higher end is clamped to the view
* width.
* @param yPosInView vertical coordinate of the center point of the magnifier source
* relative to the view. The lower end is clamped to 0 and the higher end is clamped to
* the view height.
*/
public void show(@FloatRange(from = 0) float xPosInView,
@FloatRange(from = 0) float yPosInView) {
xPosInView = Math.max(0, Math.min(xPosInView, mView.getWidth()));
yPosInView = Math.max(0, Math.min(yPosInView, mView.getHeight()));
configureCoordinates(xPosInView, yPosInView);
// Clamp the startX value to avoid magnifying content which does not belong to the magnified
// view. This will not take into account overlapping views.
// For this, we compute:
// - zeroScrollXInSurface: this is the start x of mView, where this is not masked by a
// potential scrolling container. For example, if mView is a
// TextView contained in a HorizontalScrollView,
// mViewCoordinatesInSurface will reflect the surface position of
// the first text character, rather than the position of the first
// visible one. Therefore, we need to add back the amount of
// scrolling from the parent containers.
// - actualWidth: similarly, the width of a View will be larger than its actually visible
// width when it is contained in a scrolling container. We need to use
// the minimum width of a scrolling container which contains this view.
int zeroScrollXInSurface = mViewCoordinatesInSurface[0];
int actualWidth = mView.getWidth();
ViewParent viewParent = mView.getParent();
while (viewParent instanceof View) {
final View container = (View) viewParent;
if (container.canScrollHorizontally(-1 /* left scroll */)
|| container.canScrollHorizontally(1 /* right scroll */)) {
zeroScrollXInSurface += container.getScrollX();
actualWidth = Math.min(actualWidth, container.getWidth()
- container.getPaddingLeft() - container.getPaddingRight());
}
viewParent = viewParent.getParent();
}
final int startX = Math.max(zeroScrollXInSurface, Math.min(
mCenterZoomCoords.x - mBitmap.getWidth() / 2,
zeroScrollXInSurface + actualWidth - mBitmap.getWidth()));
final int startY = mCenterZoomCoords.y - mBitmap.getHeight() / 2;
if (xPosInView != mPrevPosInView.x || yPosInView != mPrevPosInView.y) {
performPixelCopy(startX, startY);
mPrevPosInView.x = xPosInView;
mPrevPosInView.y = yPosInView;
if (mWindow.isShowing()) {
mWindow.update(mWindowCoords.x, mWindowCoords.y, mWindow.getWidth(),
mWindow.getHeight());
} else {
mWindow.showAtLocation(mView, Gravity.NO_GRAVITY, mWindowCoords.x, mWindowCoords.y);
}
}
}
/**
* Dismisses the magnifier from the screen. Calling this on a dismissed magnifier is a no-op.
*/
public void dismiss() {
mWindow.dismiss();
}
/**
* Forces the magnifier to update its content. It uses the previous coordinates passed to
* {@link #show(float, float)}. This only happens if the magnifier is currently showing.
*/
public void update() {
if (mWindow.isShowing()) {
// Update the contents shown in the magnifier.
performPixelCopy(mPrevStartCoordsInSurface.x, mPrevStartCoordsInSurface.y);
}
}
private void configureCoordinates(final float xPosInView, final float yPosInView) {
// Compute the coordinates of the center of the content going to be displayed in the
// magnifier. These are relative to the surface the content is copied from.
final float contentPosX;
final float contentPosY;
if (mView instanceof SurfaceView) {
// No offset required if the backing Surface matches the size of the SurfaceView.
contentPosX = xPosInView;
contentPosY = yPosInView;
} else {
mView.getLocationInSurface(mViewCoordinatesInSurface);
contentPosX = xPosInView + mViewCoordinatesInSurface[0];
contentPosY = yPosInView + mViewCoordinatesInSurface[1];
}
mCenterZoomCoords.x = Math.round(contentPosX);
mCenterZoomCoords.y = Math.round(contentPosY);
// Compute the position of the magnifier window. These have to be relative to the window
// of the view the magnifier is attached to, as the magnifier popup is a panel window
// attached to that window.
final int[] viewCoordinatesInWindow = new int[2];
mView.getLocationInWindow(viewCoordinatesInWindow);
final int verticalOffset = mView.getContext().getResources().getDimensionPixelSize(
com.android.internal.R.dimen.magnifier_offset);
final float magnifierPosX = xPosInView + viewCoordinatesInWindow[0];
final float magnifierPosY = yPosInView + viewCoordinatesInWindow[1] - verticalOffset;
mWindowCoords.x = Math.round(magnifierPosX - mWindowWidth / 2);
mWindowCoords.y = Math.round(magnifierPosY - mWindowHeight / 2);
}
private void performPixelCopy(final int startXInSurface, final int startYInSurface) {
// Get the view surface where the content will be copied from.
final Surface surface;
final int surfaceWidth;
final int surfaceHeight;
if (mView instanceof SurfaceView) {
final SurfaceHolder surfaceHolder = ((SurfaceView) mView).getHolder();
surface = surfaceHolder.getSurface();
surfaceWidth = surfaceHolder.getSurfaceFrame().right;
surfaceHeight = surfaceHolder.getSurfaceFrame().bottom;
} else if (mView.getViewRootImpl() != null) {
final ViewRootImpl viewRootImpl = mView.getViewRootImpl();
surface = viewRootImpl.mSurface;
surfaceWidth = viewRootImpl.getWidth();
surfaceHeight = viewRootImpl.getHeight();
} else {
surface = null;
surfaceWidth = NONEXISTENT_PREVIOUS_CONFIG_VALUE;
surfaceHeight = NONEXISTENT_PREVIOUS_CONFIG_VALUE;
}
if (surface == null || !surface.isValid()) {
return;
}
// Clamp copy coordinates inside the surface to avoid displaying distorted content.
final int clampedStartXInSurface = Math.max(0,
Math.min(startXInSurface, surfaceWidth - mWindowWidth));
final int clampedStartYInSurface = Math.max(0,
Math.min(startYInSurface, surfaceHeight - mWindowHeight));
// Perform the pixel copy.
mPixelCopyRequestRect.set(clampedStartXInSurface,
clampedStartYInSurface,
clampedStartXInSurface + mBitmap.getWidth(),
clampedStartYInSurface + mBitmap.getHeight());
PixelCopy.request(surface, mPixelCopyRequestRect, mBitmap,
result -> {
getImageView().invalidate();
mPrevStartCoordsInSurface.x = startXInSurface;
mPrevStartCoordsInSurface.y = startYInSurface;
},
mPixelCopyHandler);
}
private ImageView getImageView() {
return mWindow.getContentView().findViewById(
com.android.internal.R.id.magnifier_image);
}
/**
* @return the content being currently displayed in the magnifier, as bitmap
*
* @hide
*/
@TestApi
public Bitmap getContent() {
return mBitmap;
}
/**
* @return the position of the magnifier window relative to the screen
*
* @hide
*/
@TestApi
public Rect getWindowPositionOnScreen() {
final int[] viewLocationOnScreen = new int[2];
mView.getLocationOnScreen(viewLocationOnScreen);
final int[] viewLocationInSurface = new int[2];
mView.getLocationInSurface(viewLocationInSurface);
final int left = mWindowCoords.x + viewLocationOnScreen[0] - viewLocationInSurface[0];
final int top = mWindowCoords.y + viewLocationOnScreen[1] - viewLocationInSurface[1];
return new Rect(left, top, left + mWindow.getWidth(), top + mWindow.getHeight());
}
/**
* @return the size of the magnifier window in dp
*
* @hide
*/
@TestApi
public static PointF getMagnifierDefaultSize() {
final Resources resources = Resources.getSystem();
final float density = resources.getDisplayMetrics().density;
final PointF size = new PointF();
size.x = resources.getDimension(com.android.internal.R.dimen.magnifier_width) / density;
size.y = resources.getDimension(com.android.internal.R.dimen.magnifier_height) / density;
return size;
}
}
|