Magnifier.Builder


public static final class Magnifier.Builder
extends Object

java.lang.Object
   ↳ android.widget.Magnifier.Builder


Builder class for Magnifier objects.

Summary

Public constructors

Builder(View view)

Construct a new builder for Magnifier objects.

Public methods

Magnifier build()

Builds a Magnifier instance based on the configuration of this Builder.

Magnifier.Builder setClippingEnabled(boolean clip)

Defines the behavior of the magnifier when it is requested to position outside the surface of the main application window.

Magnifier.Builder setCornerRadius(float cornerRadius)

Sets the corner radius of the magnifier window, in pixels.

Magnifier.Builder setDefaultSourceToMagnifierOffset(int horizontalOffset, int verticalOffset)

Sets an offset that should be added to the content source center to obtain the position of the magnifier window, when the Magnifier.show(float, float) method is called.

Magnifier.Builder setElevation(float elevation)

Sets the elevation of the magnifier window, in pixels.

Magnifier.Builder setInitialZoom(float zoom)

Sets the zoom to be applied to the chosen content before being copied to the magnifier.

Magnifier.Builder setOverlay(Drawable overlay)

Sets an overlay that will be drawn on the top of the magnifier.

Magnifier.Builder setSize(int width, int height)

Sets the size of the magnifier window, in pixels.

Magnifier.Builder setSourceBounds(int left, int top, int right, int bottom)

Defines the bounds of the rectangle where the magnifier will be able to copy its content from.

Inherited methods

Public constructors

Builder

Added in API level 29
public Builder (View view)

Construct a new builder for Magnifier objects.

Parameters
view View: the view this magnifier is attached to This value cannot be null.

Public methods

build

Added in API level 29
public Magnifier build ()

Builds a Magnifier instance based on the configuration of this Builder.

Returns
Magnifier This value cannot be null.

setClippingEnabled

Added in API level 29
public Magnifier.Builder setClippingEnabled (boolean clip)

Defines the behavior of the magnifier when it is requested to position outside the surface of the main application window. The default value is true, which means that the position will be adjusted such that the magnifier will be fully within the bounds of the main application window, while also avoiding any overlap with system insets (such as the one corresponding to the status bar). If this flag is set to false, the area where the magnifier can be positioned will no longer be clipped, so the magnifier will be able to extend outside the main application window boundaries (and also overlap the system insets). This can be useful if you require a custom behavior, but it should be handled with care, when passing coordinates to Magnifier.show(float, float); note that:

  • in a multiwindow context, if the magnifier crosses the boundary between the two windows, it will not be able to show over the window of the other application
  • if the magnifier overlaps the status bar, there is no guarantee about which one will be displayed on top. This should be handled with care.

Parameters
clip boolean: whether the magnifier position will be adjusted

Returns
Magnifier.Builder This value cannot be null.

setCornerRadius

Added in API level 29
public Magnifier.Builder setCornerRadius (float cornerRadius)

Sets the corner radius of the magnifier window, in pixels. Defaults to 2dp.

Parameters
cornerRadius float: the corner radius to be set The units of this value are pixels. Value is 0 or greater

Returns
Magnifier.Builder This value cannot be null.

setDefaultSourceToMagnifierOffset

Added in API level 29
public Magnifier.Builder setDefaultSourceToMagnifierOffset (int horizontalOffset, 
                int verticalOffset)

Sets an offset that should be added to the content source center to obtain the position of the magnifier window, when the Magnifier.show(float, float) method is called. The offset is ignored when Magnifier.show(float, float, float, float) is used. The offset can be negative. It defaults to (0dp, 0dp).

Parameters
horizontalOffset int: the horizontal component of the offset The units of this value are pixels.

verticalOffset int: the vertical component of the offset The units of this value are pixels.

Returns
Magnifier.Builder This value cannot be null.

setElevation

Added in API level 29
public Magnifier.Builder setElevation (float elevation)

Sets the elevation of the magnifier window, in pixels. Defaults to 4dp.

Parameters
elevation float: the elevation to be set The units of this value are pixels. Value is 0 or greater

Returns
Magnifier.Builder This value cannot be null.

setInitialZoom

Added in API level 29
public Magnifier.Builder setInitialZoom (float zoom)

Sets the zoom to be applied to the chosen content before being copied to the magnifier. A content of size (content_width, content_height) will be magnified to (content_width * zoom, content_height * zoom), which will coincide with the size of the magnifier. A zoom of 1 will translate to no magnification (the content will be just copied to the magnifier with no scaling). The zoom defaults to 1.25. Note that the zoom can also be changed after the instance is built, using the Magnifier.setZoom(float) method.

Parameters
zoom float: the zoom to be set Value is 0f or greater

Returns
Magnifier.Builder This value cannot be null.

setOverlay

Added in API level 29
public Magnifier.Builder setOverlay (Drawable overlay)

Sets an overlay that will be drawn on the top of the magnifier. In general, the overlay should not be opaque, in order to let the magnified content be partially visible in the magnifier. The default overlay is null (no overlay). As an example, TextView applies a white ColorDrawable overlay with 5% alpha, aiming to make the magnifier distinguishable when shown in dark application regions. To disable the overlay, the parameter should be set to null. If not null, the overlay will be automatically redrawn when the drawable is invalidated. To achieve this, the magnifier will set a new Drawable.Callback for the overlay drawable, so keep in mind that any existing one set by the application will be lost.

Parameters
overlay Drawable: the overlay to be drawn on top This value may be null.

Returns
Magnifier.Builder

setSize

Added in API level 29
public Magnifier.Builder setSize (int width, 
                int height)

Sets the size of the magnifier window, in pixels. Defaults to (100dp, 48dp). Note that the size of the content being magnified and copied to the magnifier will be computed as (window width / zoom, window height / zoom).

Parameters
width int: the window width to be set The units of this value are pixels. Value is 0 or greater

height int: the window height to be set The units of this value are pixels. Value is 0 or greater

Returns
Magnifier.Builder This value cannot be null.

setSourceBounds

Added in API level 29
public Magnifier.Builder setSourceBounds (int left, 
                int top, 
                int right, 
                int bottom)

Defines the bounds of the rectangle where the magnifier will be able to copy its content from. The content will always be copied from the Surface of the main application window unless the magnified view is a SurfaceView, in which case its backing surface will be used. Each bound can have a different behavior, with the options being:

  • Magnifier.SOURCE_BOUND_MAX_VISIBLE, which extends the bound as much as possible while remaining in the visible region of the magnified view, as given by View.getGlobalVisibleRect(Rect). For example, this will take into account the case when the view is contained in a scrollable container, and the magnifier will refuse to copy content outside of the visible view region
  • Magnifier.SOURCE_BOUND_MAX_IN_SURFACE, which extends the bound as much as possible while remaining inside the surface the content is copied from.
Note that if either of the first three options is used, the bound will be compared to the bound of the surface (i.e. as if Magnifier.SOURCE_BOUND_MAX_IN_SURFACE was used), and the more restrictive one will be chosen. In other words, no attempt to copy content from outside the surface will be permitted. If two opposite bounds are not well-behaved (i.e. left + sourceWidth > right or top + sourceHeight > bottom), the left and top bounds will have priority and the others will be extended accordingly. If the pairs obtained this way still remain out of bounds, the smallest possible offset will be added to the pairs to bring them inside the surface bounds. If this is impossible (i.e. the surface is too small for the size of the content we try to copy on either dimension), an error will be logged and the magnifier content will look distorted. The default values assumed by the builder for the source bounds are left: Magnifier.SOURCE_BOUND_MAX_VISIBLE, top: Magnifier.SOURCE_BOUND_MAX_IN_SURFACE, right: Magnifier.SOURCE_BOUND_MAX_VISIBLE, bottom: Magnifier.SOURCE_BOUND_MAX_IN_SURFACE.

Parameters
left int: the left bound for content copy Value is Magnifier.SOURCE_BOUND_MAX_IN_SURFACE, or Magnifier.SOURCE_BOUND_MAX_VISIBLE

top int: the top bound for content copy Value is Magnifier.SOURCE_BOUND_MAX_IN_SURFACE, or Magnifier.SOURCE_BOUND_MAX_VISIBLE

right int: the right bound for content copy Value is Magnifier.SOURCE_BOUND_MAX_IN_SURFACE, or Magnifier.SOURCE_BOUND_MAX_VISIBLE

bottom int: the bottom bound for content copy Value is Magnifier.SOURCE_BOUND_MAX_IN_SURFACE, or Magnifier.SOURCE_BOUND_MAX_VISIBLE

Returns
Magnifier.Builder This value cannot be null.