public class ImageViewer extends Component
ImageViewer allows zooming/panning an image and potentially flicking between multiple images
within a list of images.
E.g. the trivial usage works like this:
You can simulate pinch to zoom on the simulator by dragging the right button away from the top left corner to zoom in and towards the top left corner to zoom out. On Mac touchpads you can drag two fingers to achieve that.
A more elaborate usage includes flicking between multiple images e.g.:
You can even download image URL's dynamically into the ImageViewer
thanks to the usage of the
ListModel
. E.g. in this model book cover images are downloaded dynamically:
Modifier and Type | Field and Description |
---|---|
static int |
IMAGE_FILL
Indicates the initial position of the image in the viewer to FILL the
component size.
|
static int |
IMAGE_FIT
Indicates the initial position of the image in the viewer to FIT to the
component size
|
BASELINE, BOTTOM, BRB_CENTER_OFFSET, BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT, BRB_OTHER, CENTER, CROSSHAIR_CURSOR, DEFAULT_CURSOR, DRAG_REGION_IMMEDIATELY_DRAG_X, DRAG_REGION_IMMEDIATELY_DRAG_XY, DRAG_REGION_IMMEDIATELY_DRAG_Y, DRAG_REGION_LIKELY_DRAG_X, DRAG_REGION_LIKELY_DRAG_XY, DRAG_REGION_LIKELY_DRAG_Y, DRAG_REGION_NOT_DRAGGABLE, DRAG_REGION_POSSIBLE_DRAG_X, DRAG_REGION_POSSIBLE_DRAG_XY, DRAG_REGION_POSSIBLE_DRAG_Y, E_RESIZE_CURSOR, HAND_CURSOR, LEFT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NW_RESIZE_CURSOR, RIGHT, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, TOP, W_RESIZE_CURSOR, WAIT_CURSOR
Constructor and Description |
---|
ImageViewer()
Default constructor
|
ImageViewer(Image i)
Initializes the component with an image
|
Modifier and Type | Method and Description |
---|---|
boolean |
animate()
Allows the animation to reduce "repaint" calls when it returns false.
|
protected Dimension |
calcPreferredSize()
Calculates the preferred size based on component content.
|
void |
deinitialize()
Invoked to indicate that the component initialization is being reversed
since the component was detached from the container hierarchy.
|
protected void |
dragFinished(int x,
int y)
Callback indicating that the drag has finished either via drop or by releasing the component
|
Image |
getCroppedImage(int backgroundColor)
Gets the current image cropped using the current pan and zoom state.
|
Image |
getCroppedImage(int width,
int height,
int backgroundColor)
Gets the current image cropped using the current pan and zoom state.
|
Image |
getImage()
Returns the currently showing image
|
ListModel<Image> |
getImageList()
Returns the list model containing the images in the we can swipe through
|
int |
getImageX()
Returns the x position of the image viewport which can be useful when it is being panned by the user
|
int |
getImageY()
Returns the y position of the image viewport which can be useful when it is being panned by the user
|
String[] |
getPropertyNames()
A component may expose mutable property names for a UI designer to manipulate, this
API is designed for usage internally by the GUI builder code
|
String[] |
getPropertyTypeNames()
This method is here to workaround an XMLVM array type bug where property types aren't
identified properly, it returns the names of the types using the following type names:
String,int,double,long,byte,short,char,String[],String[][],byte[],Image,Image[],Object[],ListModel,ListCellRenderer
|
Class[] |
getPropertyTypes()
Matches the property names method (see that method for further details).
|
Object |
getPropertyValue(String name)
Returns the current value of the property name, this method is used by the GUI builder
|
Image |
getSwipePlaceholder()
This image is shown briefly during swiping while the full size image is loaded
|
float |
getSwipeThreshold()
The swipe threshold is a number between 0 and 1 that indicates the threshold
after which the swiped image moves to the next image.
|
float |
getZoom()
Manipulate the zoom level of the application
|
void |
initComponent()
Allows subclasses to bind functionality that relies on fully initialized and
"ready for action" component state
|
boolean |
isAllowScaleDown()
Allows the image to scale down when image initial position is set to fit
this is off by default since the UX isn't great
|
boolean |
isAnimatedZoom()
Indicates if the zoom should bee animated.
|
boolean |
isCycleLeft()
By default the ImageViewer cycles from the beginning to the end of the list
when going to the left, setting this to false prevents this behaviour
|
boolean |
isCycleRight()
By default the ImageViewer cycles from the end to the beginning of the list
when going to the right, setting this to false prevents this behaviour
|
boolean |
isEagerLock()
Eager locking effectively locks the right/left images as well as the main image, as a result
more heap is taken
|
void |
keyReleased(int key)
If this Component is focused, the key released event
will call this method
|
protected void |
laidOut()
This is a callback method to inform the Component when it's been laidout
on the parent Container
|
void |
paint(Graphics g)
This method paints the Component on the screen, it should be overriden
by subclasses to perform custom drawing or invoke the UI API's to let
the PLAF perform the rendering.
|
protected void |
paintBackground(Graphics g)
This method paints the Component background, it should be overriden
by subclasses to perform custom background drawing.
|
protected boolean |
pinch(float scale)
Invoked by subclasses interested in handling pinch to zoom events, if true is returned
other drag events will not be broadcast
|
protected void |
pinchReleased(int x,
int y)
To be implemented by subclasses interested in being notified when a pinch zoom has
ended (i.e the user has removed one of their fingers, but is still dragging).
|
void |
pointerDragged(int x,
int y)
If this Component is focused, the pointer dragged event
will call this method
|
void |
pointerPressed(int x,
int y)
If this Component is focused, the pointer pressed event
will call this method
|
void |
pointerReleased(int x,
int y)
If this Component is focused, the pointer released event
will call this method
|
protected void |
resetFocusable()
Restores the state of the focusable flag to its default state
|
void |
setAllowScaleDown(boolean allowScaleDown)
Allows the image to scale down when image initial position is set to fit
this is off by default since the UX isn't great
|
void |
setAnimateZoom(boolean animateZoom)
Indicates if the zoom should bee animated.
|
void |
setCycleLeft(boolean cycleLeft)
By default the ImageViewer cycles from the beginning to the end of the list
when going to the left, setting this to false prevents this behaviour
|
void |
setCycleRight(boolean cycleRight)
By default the ImageViewer cycles from the end to the beginning of the list
when going to the right, setting this to false prevents this behaviour
|
void |
setEagerLock(boolean eagerLock)
Eager locking effectively locks the right/left images as well as the main image, as a result
more heap is taken
|
void |
setImage(Image image)
Sets the currently showing image
|
void |
setImageInitialPosition(int imageInitialPosition)
Sets the viewer initial image position to fill or to fit.
|
void |
setImageList(ListModel<Image> model)
By providing this optional list of images you can allows swiping between multiple images
|
void |
setImageNoReposition(Image image)
Sets the current image without any changes to the panning/scaling
|
String |
setPropertyValue(String name,
Object value)
Sets a new value to the given property, returns an error message if failed
and null if successful.
|
void |
setSwipePlaceholder(Image swipePlaceholder)
This image is shown briefly during swiping while the full size image is loaded
|
void |
setSwipeThreshold(float swipeThreshold)
The swipe threshold is a number between 0 and 1 that indicates the threshold
after which the swiped image moves to the next image.
|
void |
setZoom(float zoom)
Manipulate the zoom level of the application
|
void |
setZoom(float zoom,
float panPositionX,
float panPositionY)
Manipulate the zoom level of the application
|
protected boolean |
shouldBlockSideSwipe()
A component that might need side swipe such as the slider
could block it from being used for some other purpose when
on top of said component.
|
addDragFinishedListener, addDragOverListener, addDropListener, addFocusListener, addLongPressListener, addPointerDraggedListener, addPointerPressedListener, addPointerReleasedListener, addPullToRefresh, addScrollListener, addStateChangeListener, bindProperty, blocksSideSwipe, calcScrollSize, cancelRepaints, clearClientProperties, contains, containsOrOwns, createStyleAnimation, deinitializeCustomStyle, dragEnter, dragExit, draggingOver, dragInitiated, drawDraggedImage, drop, fireClicked, focusGained, focusLost, getAbsoluteX, getAbsoluteY, getAllStyles, getAnimationManager, getBaseline, getBaselineResizeBehavior, getBindablePropertyNames, getBindablePropertyTypes, getBorder, getBottomGap, getBoundPropertyValue, getBounds, getBounds, getClientProperty, getCloudBoundProperty, getCloudDestinationProperty, getComponentForm, getComponentState, getCursor, getDefaultDragTransparency, getDirtyRegion, getDisabledStyle, getDraggedx, getDraggedy, getDragImage, getDragRegionStatus, getDragSpeed, getDragTransparency, getEditingDelegate, getGridPosX, getGridPosY, getHeight, getInlineAllStyles, getInlineDisabledStyles, getInlinePressedStyles, getInlineSelectedStyles, getInlineStylesTheme, getInlineUnselectedStyles, getInnerHeight, getInnerPreferredH, getInnerPreferredW, getInnerWidth, getInnerX, getInnerY, getLabelForComponent, getName, getNativeOverlay, getNextFocusDown, getNextFocusLeft, getNextFocusRight, getNextFocusUp, getOuterHeight, getOuterPreferredH, getOuterPreferredW, getOuterWidth, getOuterX, getOuterY, getOwner, getParent, getPreferredH, getPreferredSize, getPreferredSizeStr, getPreferredTabIndex, getPreferredW, getPressedStyle, getSameHeight, getSameWidth, getScrollable, getScrollAnimationSpeed, getScrollDimension, getScrollOpacity, getScrollOpacityChangeSpeed, getScrollX, getScrollY, getSelectCommandText, getSelectedRect, getSelectedStyle, getSideGap, getStyle, getTabIndex, getTensileLength, getTextSelectionSupport, getTooltip, getUIID, getUIManager, getUnselectedStyle, getVisibleBounds, getVisibleBounds, getWidth, getX, getY, growShrink, handlesInput, hasFixedPreferredSize, hasFocus, hideNativeOverlay, initCustomStyle, initDisabledStyle, initLaf, initPressedStyle, initSelectedStyle, initUnselectedStyle, installDefaultPainter, isAlwaysTensile, isBlockLead, isCellRenderer, isChildOf, isDragActivated, isDragAndDropOperation, isDraggable, isDragRegion, isDropTarget, isEditable, isEditing, isEnabled, isFlatten, isFocusable, isGrabsPointerEvents, isHidden, isHidden, isHideInLandscape, isHideInPortrait, isIgnorePointerEvents, isInClippingRegion, isInitialized, isOpaque, isOwnedBy, isPinchBlocksDragAndDrop, isRippleEffect, isRTL, isScrollable, isScrollableX, isScrollableY, isScrollVisible, isSelectableInteraction, isSetCursorSupported, isSmoothScrolling, isSnapToGrid, isStickyDrag, isTactileTouch, isTactileTouch, isTensileDragEnabled, isTraversable, isVisible, keyPressed, keyRepeated, longKeyPress, longPointerPress, onScrollX, onScrollY, paintBackgrounds, paintBorder, paintBorderBackground, paintComponent, paintComponent, paintIntersectingComponentsAbove, paintLock, paintLockRelease, paintRippleOverlay, paintScrollbars, paintScrollbarX, paintScrollbarY, paintShadows, paramString, parsePreferredSize, pinch, pointerDragged, pointerHover, pointerHoverPressed, pointerHoverReleased, pointerPressed, pointerReleased, putClientProperty, refreshTheme, refreshTheme, refreshTheme, remove, removeDragFinishedListener, removeDragOverListener, removeDropListener, removeFocusListener, removeLongPressListener, removePointerDraggedListener, removePointerPressedListener, removePointerReleasedListener, removeScrollListener, removeStateChangeListener, repaint, repaint, requestFocus, respondsToPointerEvents, scrollRectToVisible, scrollRectToVisible, setAlwaysTensile, setBlockLead, setBoundPropertyValue, setCellRenderer, setCloudBoundProperty, setCloudDestinationProperty, setComponentState, setCursor, setDefaultDragTransparency, setDirtyRegion, setDisabledStyle, setDraggable, setDragTransparency, setDropTarget, setEditingDelegate, setEnabled, setFlatten, setFocus, setFocusable, setGrabsPointerEvents, setHandlesInput, setHeight, setHidden, setHidden, setHideInLandscape, setHideInPortrait, setIgnorePointerEvents, setInitialized, setInlineAllStyles, setInlineDisabledStyles, setInlinePressedStyles, setInlineSelectedStyles, setInlineStylesTheme, setInlineUnselectedStyles, setIsScrollVisible, setLabelForComponent, setName, setNextFocusDown, setNextFocusLeft, setNextFocusRight, setNextFocusUp, setOpaque, setOwner, setPinchBlocksDragAndDrop, setPreferredH, setPreferredSize, setPreferredSizeStr, setPreferredTabIndex, setPreferredW, setPressedStyle, setRippleEffect, setRTL, setSameHeight, setSameSize, setSameWidth, setScrollAnimationSpeed, setScrollOpacityChangeSpeed, setScrollSize, setScrollVisible, setScrollX, setScrollY, setSelectCommandText, setSelectedStyle, setShouldCalcPreferredSize, setSize, setSmoothScrolling, setSnapToGrid, setTabIndex, setTactileTouch, setTensileDragEnabled, setTensileLength, setTooltip, setTraversable, setUIID, setUIID, setUnselectedStyle, setVisible, setWidth, setX, setY, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, shouldRenderComponentSelection, showNativeOverlay, startEditingAsync, stopEditing, stripMarginAndPadding, styleChanged, toImage, toString, unbindProperty, updateNativeOverlay, visibleBoundsContains
public static final int IMAGE_FIT
public static final int IMAGE_FILL
public ImageViewer()
public ImageViewer(Image i)
i
- image to showprotected void resetFocusable()
resetFocusable
in class Component
public String[] getPropertyNames()
getPropertyNames
in class Component
protected boolean shouldBlockSideSwipe()
shouldBlockSideSwipe
in class Component
public Class[] getPropertyTypes()
getPropertyTypes
in class Component
public String[] getPropertyTypeNames()
getPropertyTypeNames
in class Component
public Object getPropertyValue(String name)
getPropertyValue
in class Component
name
- the name of the propertypublic String setPropertyValue(String name, Object value)
setPropertyValue
in class Component
name
- the name of the propertyvalue
- new value for the propertypublic void initComponent()
initComponent
in class Component
public int getImageX()
public int getImageY()
public void deinitialize()
deinitialize
in class Component
public void keyReleased(int key)
keyReleased
in class Component
key
- the key code value to indicate a physical key.public void pointerPressed(int x, int y)
pointerPressed
in class Component
x
- the pointer x coordinatey
- the pointer y coordinateprotected void dragFinished(int x, int y)
Component
dragFinished
in class Component
x
- the x locationy
- the y locationpublic void pointerReleased(int x, int y)
pointerReleased
in class Component
x
- the pointer x coordinatey
- the pointer y coordinateprotected void pinchReleased(int x, int y)
Component
pinchReleased
in class Component
x
- The x-coordinate of the remaining finger in the drag. (Absolute)y
- The y-coordinate of the remaining finger in the drag. (Absolute)public void pointerDragged(int x, int y)
pointerDragged
in class Component
x
- the pointer x coordinatey
- the pointer y coordinateprotected void laidOut()
protected boolean pinch(float scale)
pinch
in class Component
scale
- the scaling of the pinch operation a number larger than 1 means scaling up and smaller than 1 means scaling down.
It is recommended that code would threshold the number (so a change between 1.0 and 1.02 shouldn't necessarily trigger zoom).
Notice that this number is relevant to current zoom levels and unaware of them so you should also enforce limits of maximum/minimum
zoom levels.public Image getCroppedImage(int backgroundColor)
backgroundColor
- The background color, visible for letterboxing.public Image getCroppedImage(int width, int height, int backgroundColor)
width
- The width of the cropped image. Use -1 to match aspect ratio of the ImageViewer component. Either height or width must be positive.height
- The height of the cropped image. Use -1 to match aspect ratio of the ImageViewer component. Either height or width must be positive.backgroundColor
- Background color to use for letterboxing.protected Dimension calcPreferredSize()
calcPreferredSize
in class Component
public boolean animate()
Display
class.public boolean isAllowScaleDown()
public void setAllowScaleDown(boolean allowScaleDown)
allowScaleDown
- the allowScaleDown to setpublic void paint(Graphics g)
protected void paintBackground(Graphics g)
paintBackground
in class Component
g
- the component graphicspublic Image getImage()
public void setImage(Image image)
image
- the image to setpublic void setImageNoReposition(Image image)
image
- new image instancepublic void setImageList(ListModel<Image> model)
model
- a list of imagespublic ListModel<Image> getImageList()
public void setAnimateZoom(boolean animateZoom)
animateZoom
- true if zoom is animatedpublic boolean isAnimatedZoom()
public float getZoom()
public void setZoom(float zoom)
zoom
- the zoom to setpublic void setZoom(float zoom, float panPositionX, float panPositionY)
zoom
- the zoom to setpanPositionX
- A float value between 0 and 1 to set the image x positionpanPositionY
- A float value between 0 and 1 to set the image y positionpublic Image getSwipePlaceholder()
public void setSwipePlaceholder(Image swipePlaceholder)
swipePlaceholder
- the swipePlaceholder to setpublic boolean isEagerLock()
public void setEagerLock(boolean eagerLock)
eagerLock
- the eagerLock to setpublic boolean isCycleLeft()
public void setCycleLeft(boolean cycleLeft)
cycleLeft
- the cycle left to setpublic boolean isCycleRight()
public void setCycleRight(boolean cycleRight)
cycleRight
- the cycle right to setpublic float getSwipeThreshold()
public void setSwipeThreshold(float swipeThreshold)
swipeThreshold
- the swipeThreshold to setpublic void setImageInitialPosition(int imageInitialPosition)
imageInitialPosition
- values can be IMAGE_FILL or IMAGE_FIT