VirtualWorkspaceView

public class VirtualWorkspaceView extends NonPropagatingViewGroup

Virtual view of a WorkspaceView.

This view class provides a viewport-sized window into a larger workspace and supports panning and zoom.

Inherited Constant Summary

Field Summary

protected boolean mScalable
protected boolean mScrollable

Inherited Field Summary

Public Constructor Summary

VirtualWorkspaceView(Context context)
VirtualWorkspaceView(Context context, AttributeSet attrs)
VirtualWorkspaceView(Context context, AttributeSet attrs, int defStyleAttr)

Public Method Summary

boolean
dispatchTouchEvent(MotionEvent event)
int
float
boolean
void
void
onLayout(boolean changed, int l, int t, int r, int b)
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
boolean
onTouchEvent(MotionEvent event)
void
postResetView()
Post a view reset to occur upon next call to onLayout(boolean, int, int, int, int).
void
resetView()
Reset the view to the top-left corner of the virtual workspace (with a small margin), and reset zoom to unit scale.
void
scrollTo(int x, int y)
Set scroll position for the WorkspaceView wrapped by this instance.
void
setDrawGrid(boolean drawGrid)
void
boolean
zoomIn()
Zoom into (i.e., enlarge) the workspace.
boolean
zoomOut()
Zoom out from (i.e.,shrink) the workspace.

Protected Method Summary

int
int
int
int
int
int
void
onDraw(Canvas c)
void
setScalable(boolean scalable)
Configures whether the user can scale the workspace by touch events.
void
setScrollable(boolean scrollable)
Configures whether the user can scroll the workspace by dragging.

Inherited Method Summary

Fields

protected boolean mScalable

protected boolean mScrollable

Public Constructors

public VirtualWorkspaceView (Context context)

Parameters
context

public VirtualWorkspaceView (Context context, AttributeSet attrs)

Parameters
context
attrs

public VirtualWorkspaceView (Context context, AttributeSet attrs, int defStyleAttr)

Parameters
context
attrs
defStyleAttr

Public Methods

public boolean dispatchTouchEvent (MotionEvent event)

Parameters
event

public int getGridSpacing ()

public float getViewScale ()

Returns
  • The current view scale factor. Larger than 1.0 means zoomed in, smaller than 1.0 means zoomed out.

public boolean isScrollable ()

public void onFinishInflate ()

public void onLayout (boolean changed, int l, int t, int r, int b)

Parameters
changed
l
t
r
b

public void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Parameters
widthMeasureSpec
heightMeasureSpec

public boolean onTouchEvent (MotionEvent event)

Parameters
event

public void postResetView ()

Post a view reset to occur upon next call to onLayout(boolean, int, int, int, int).

This method can be called when workspace content has been completely replaced, to provide the user with a good initial view of the new workspace content.

public void resetView ()

Reset the view to the top-left corner of the virtual workspace (with a small margin), and reset zoom to unit scale.

This is called when the "reset view" button is clicked, or when ERROR(/#mResetViewPending) is set.

public void scrollTo (int x, int y)

Set scroll position for the WorkspaceView wrapped by this instance.

The given scroll position specifies the absolute offset of the displayed area within the virtual workspace. Inside this method, the given scroll coordinates are clamped to their valid ranges determined from the bounding box of all blocks in the virtual workspace, and allowing for overscroll of half the width (or height, respectively) of this view.

Parameters
x The horizontal scroll position in pixel units of this view.
y The vertical scroll position in pixel units of this view.

public void setDrawGrid (boolean drawGrid)

Parameters
drawGrid

public void setZoomBehavior (ZoomBehavior zoomBehavior)

Parameters
zoomBehavior

public boolean zoomIn ()

Zoom into (i.e., enlarge) the workspace.

Returns
  • True if a zoom was changed, increased. Otherwise false.

public boolean zoomOut ()

Zoom out from (i.e.,shrink) the workspace.

Returns
  • True if a zoom was changed, decreased. Otherwise false.

Protected Methods

protected int computeHorizontalScrollExtent ()

Returns
  • Width of the horizontal scroll bar within its range.

    This value determines how wide the horizontal scroll bar is, relative to its range as returned by computeHorizontalScrollRange(). For example, if this value is half the current range, then the scroll bar will be draw half as wide as the view.

    Note that the units of the value returned by this method are essentially arbitrary in the sense that scroll bar position and width are invariant under scaling of range, offset, and extent by the same factor (up to truncation of fractions to nearest integers).

    We use screen pixels for the unit, so this is just the measured width of the view.

protected int computeHorizontalScrollOffset ()

Returns
  • Offset of the current horizontal scroll position, which determines the relative position of the horizontal scroll bar.

    This is a value between 0 and the return value of computeHorizontalScrollRange(). Zero corresponds to the left boundary of the currently-used workspace area, minus half the width of this view to allow for overscroll.

    Note that the units of the value returned by this method are essentially arbitrary in the sense that scroll bar position and width are invariant under scaling of range, offset, and extent by the same factor (up to truncation of fractions to nearest integers).

    The offset is just a translation of getScrollX() of the left edge of the viewport on the workspace in pixels relative to the farthest left position possible. In other words, offset goes from 0 to (workspace max - workspace min) in pixels.

protected int computeHorizontalScrollRange ()

Returns
  • Horizontal scroll range width, which is used to position the horizontal scroll bar.

    This value is the upper limit of the permitted horizontal scroll range, which corresponds to the right boundary of the currently-used workspace area, plus has the view width to allow for overscroll.

    Together with the return value of computeHorizontalScrollOffset(), this determines the relative position of the scroll bar.

    Note that the units of the value returned by this method are essentially arbitrary in the sense that scroll bar position and width are invariant under scaling of range, offset, and extent by the same factor (up to truncation of fractions to nearest integers).

    We compute this by taking the total width of the available workspace and converting it to screen pixels. The available workspace is the size needed to fully enclose all blocks with half the display's width for padding, or (workspace max - workspace min) in pixels + the visible width.

protected int computeVerticalScrollExtent ()

Returns

protected int computeVerticalScrollOffset ()

Returns

protected int computeVerticalScrollRange ()

Returns

protected void onDraw (Canvas c)

Parameters
c

protected void setScalable (boolean scalable)

Configures whether the user can scale the workspace by touch events.

Parameters
scalable Allow scalability if true. Otherwise, disable it.

protected void setScrollable (boolean scrollable)

Configures whether the user can scroll the workspace by dragging. If scrolling is disabled, the workspace will reset to 0,0 in the top right hand corner.

Parameters
scrollable Allow scrolling if true. Otherwise, disable it.