diff options
| author | Shu Chen <shuchen@google.com> | 2020-02-14 14:25:14 +0800 |
|---|---|---|
| committer | Shu Chen <shuchen@google.com> | 2020-02-19 09:17:01 +0800 |
| commit | d931a4763f77365c788d73decb7d5a57c8f62eb3 (patch) | |
| tree | a8eb9a7b3ce69938fad6a298ea7fe50d802ea364 /core/java/android/widget/Magnifier.java | |
| parent | 8a48bc0b1ffc891404396a2372188d8f91b44562 (diff) | |
Supports dynamic height for the new magnifier.
And also limits the pre-magnified line height:
- If the text line height is too big, don't show the magnifier.
- If the text line height is too small, enlarges the zoom factor so
that the magnified text can be seen clearly.
Bug: 77791703, 148181186
Test: locally verified.
Change-Id: I4cf56e0a54fda6b23dafe153040fe5ba206f5f1f
Diffstat (limited to 'core/java/android/widget/Magnifier.java')
| -rw-r--r-- | core/java/android/widget/Magnifier.java | 83 |
1 files changed, 75 insertions, 8 deletions
diff --git a/core/java/android/widget/Magnifier.java b/core/java/android/widget/Magnifier.java index a6a5ec55b972..ec6a53771d7c 100644 --- a/core/java/android/widget/Magnifier.java +++ b/core/java/android/widget/Magnifier.java @@ -89,7 +89,7 @@ public final class Magnifier { // The width of the window containing the magnifier. private final int mWindowWidth; // The height of the window containing the magnifier. - private final int mWindowHeight; + private int mWindowHeight; // The zoom applied to the view region copied to the magnifier view. private float mZoom; // The width of the content that will be copied to the magnifier. @@ -485,6 +485,21 @@ public final class Magnifier { } /** + * Updates the factors of source which may impact the magnifier's size. + * This can be called while the magnifier is showing and moving. + * @param sourceHeight the new source height. + * @param zoom the new zoom factor. + */ + void updateSourceFactors(final int sourceHeight, final float zoom) { + mZoom = zoom; + mSourceHeight = sourceHeight; + mWindowHeight = (int) (sourceHeight * zoom); + if (mWindow != null) { + mWindow.updateContentFactors(mWindowHeight, zoom); + } + } + + /** * Returns the zoom to be applied to the magnified view region copied to the magnifier. * If the zoom is x and the magnifier window size is (width, height), the original size * of the content being magnified will be (width / x, height / x). @@ -904,7 +919,7 @@ public final class Magnifier { private final Display mDisplay; // The size of the content of the magnifier. private final int mContentWidth; - private final int mContentHeight; + private int mContentHeight; // The insets of the content inside the allocated surface. private final int mOffsetX; private final int mOffsetY; @@ -947,7 +962,7 @@ public final class Magnifier { // The current content of the magnifier. It is mBitmap + mOverlay, only used for testing. private Bitmap mCurrentContent; - private final float mZoom; + private float mZoom; // The width of the ramp region in pixels on the left & right sides of the fish-eye effect. private final int mRamp; // Whether is in the new magnifier style. @@ -1009,11 +1024,11 @@ public final class Magnifier { final RecordingCanvas canvas = mRenderer.getRootNode().beginRecording(width, height); try { - canvas.insertReorderBarrier(); + canvas.enableZ(); canvas.drawRenderNode(mBitmapRenderNode); - canvas.insertInorderBarrier(); + canvas.disableZ(); canvas.drawRenderNode(mOverlayRenderNode); - canvas.insertInorderBarrier(); + canvas.disableZ(); } finally { mRenderer.getRootNode().endRecording(); } @@ -1034,15 +1049,66 @@ public final class Magnifier { } } + /** + * Updates the factors of content which may resize the window. + * @param contentHeight the new height of content. + * @param zoom the new zoom factor. + */ + private void updateContentFactors(final int contentHeight, final float zoom) { + if (mContentHeight == contentHeight && mZoom == zoom) { + return; + } + if (mContentHeight < contentHeight) { + // Grows the surface height as necessary. + new SurfaceControl.Transaction().setBufferSize( + mSurfaceControl, mContentWidth, contentHeight).apply(); + mSurface.copyFrom(mSurfaceControl); + mRenderer.setSurface(mSurface); + + final Outline outline = new Outline(); + outline.setRoundRect(0, 0, mContentWidth, contentHeight, 0); + outline.setAlpha(1.0f); + + mBitmapRenderNode.setLeftTopRightBottom(mOffsetX, mOffsetY, + mOffsetX + mContentWidth, mOffsetY + contentHeight); + mBitmapRenderNode.setOutline(outline); + + mOverlayRenderNode.setLeftTopRightBottom(mOffsetX, mOffsetY, + mOffsetX + mContentWidth, mOffsetY + contentHeight); + mOverlayRenderNode.setOutline(outline); + + final RecordingCanvas canvas = + mRenderer.getRootNode().beginRecording(mContentWidth, contentHeight); + try { + canvas.enableZ(); + canvas.drawRenderNode(mBitmapRenderNode); + canvas.disableZ(); + canvas.drawRenderNode(mOverlayRenderNode); + canvas.disableZ(); + } finally { + mRenderer.getRootNode().endRecording(); + } + } + mContentHeight = contentHeight; + mZoom = zoom; + fillMeshMatrix(); + } + private void createMeshMatrixForFishEyeEffect() { mMeshWidth = 1; mMeshHeight = 6; + mMeshLeft = new float[2 * (mMeshWidth + 1) * (mMeshHeight + 1)]; + mMeshRight = new float[2 * (mMeshWidth + 1) * (mMeshHeight + 1)]; + fillMeshMatrix(); + } + + private void fillMeshMatrix() { + mMeshWidth = 1; + mMeshHeight = 6; final float w = mContentWidth; final float h = mContentHeight; final float h0 = h / mZoom; final float dh = h - h0; - mMeshLeft = new float[2 * (mMeshWidth + 1) * (mMeshHeight + 1)]; - mMeshRight = new float[2 * (mMeshWidth + 1) * (mMeshHeight + 1)]; for (int i = 0; i < 2 * (mMeshWidth + 1) * (mMeshHeight + 1); i += 2) { // Calculates X value. final int colIndex = i % (2 * (mMeshWidth + 1)) / 2; @@ -1197,6 +1263,7 @@ public final class Magnifier { if (mBitmap != null) { mBitmap.recycle(); } + mOverlay.setCallback(null); } private void doDraw() { |
