Class ImageScroller
java.lang.Object
ca.corbett.extras.image.animation.ImageScroller
Displays an oversized image and slowly scrolls it either horizontally back-and-forth (for landscape
images) or vertically up-and-down (for portrait images). The trick here is that when a scroll limit
is reached, instead of just "bouncing" and immediately reversing the scroll direction at full speed,
the image scrolling will slow down as it approaches the scroll limit, bounce, and then slowly accelerate
as it moves away from the scroll limit. This makes the scrolling feel a lot more natural.
Controlling the bounce parameters
- You can use EasingStrength to determine the "strength" of the bounce. The options are Linear, Quadratic, and Cubic, with a progressively pronounced effect. The default is Quadratic.
- bounceZoneRatio determines what percentage of the image area to use as the "bounce zone" - that is, the zone in which deceleration and acceleration will happen. The default value is 0.06, indicating the extreme 6% edges of the image will be used for the bounce zone. Valid values range from 0.0 (no bounce, just reverse direction at full speed) up to 0.5 (half the image).
- ScrollSpeed is used to determine the maximum speed of the image scrolling (i.e. when not decelerating or accelerating). Values range from VERY_SLOW (1) up to VERY_FAST (5). The numeric valud represents the number of pixels per frame that the image will move.
- minSpeedRatio determines the minimum scrolling speed (during a "bounce") as a percentage of the ScrollSpeed. Note that the image will scroll a minimum pixel value of 1 per frame regardless of minSpeedRatio. Fractional pixel values are rounded up to 1 to prevent the animation from freezing.
- Since:
- swing-extras 2.3
- Author:
- scorbo2 (with help from Claude!)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumDetermines the extent to which we slow down as we approach a scroll limit, and also how quickly we speed up as we move away from a scroll limit.static enumDetermines pixels per frame of animation movement. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected floatprotected final intprotected final intprotected ImageScroller.EasingStrengthprotected BufferedImageprotected booleanprotected floatprotected booleanprotected ImageScroller.ScrollSpeedprotected floatprotected intprotected intprotected floatprotected intprotected intprotected float -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected floatcalculateSpeedMultiplier(int currentPos, int minPos, int screenSize) Calculates speed multiplier based on distance from bounce pointsfloatgetImage()floatvoidRenders a single frame of animation and handles scrolling the image by an appropriate amount.protected voidreset()voidsetBounceZoneRatio(float bounceZoneRatio) Sets the width of the "bounce zones", or edges of the image where we will slow down as we approach a scroll limit and speed up as we scroll away from it.voidsetEasingStrength(ImageScroller.EasingStrength easingStrength) Sets the strength of the "bounce" calculations, to exaggerate the acceleration and deceleration effects.voidsetImage(BufferedImage image) Sets the image to be scrolled.voidsetMinSpeedRatio(float minSpeedRatio) Sets the minimum allowable scroll speed during deceleration/acceleration, expressed as a percentage of the maximum speed.voidsetScrollSpeed(ImageScroller.ScrollSpeed scrollSpeed) This represents the maximum scrolling speed, in pixels per frame.voidstop()Stops the scrolling and flushes the current image.
-
Field Details
-
isRunning
protected boolean isRunning -
scrollSpeed
-
easingStrength
-
image
-
displayWidth
protected final int displayWidth -
displayHeight
protected final int displayHeight -
zoomFactor
protected float zoomFactor -
xOffset
protected int xOffset -
yOffset
protected int yOffset -
xDelta
protected float xDelta -
yDelta
protected float yDelta -
xDirection
protected int xDirection -
yDirection
protected int yDirection -
scaleCalculationsDone
protected boolean scaleCalculationsDone -
bounceZoneRatio
protected float bounceZoneRatio -
minSpeedRatio
protected float minSpeedRatio
-
-
Constructor Details
-
ImageScroller
-
-
Method Details
-
getScrollSpeed
-
setScrollSpeed
This represents the maximum scrolling speed, in pixels per frame. The actual scrolling speed may be less if the image is approaching or receding away from a "bounce point" (scroll limit). -
getEasingStrength
-
setEasingStrength
Sets the strength of the "bounce" calculations, to exaggerate the acceleration and deceleration effects. -
getBounceZoneRatio
public float getBounceZoneRatio() -
setBounceZoneRatio
public void setBounceZoneRatio(float bounceZoneRatio) Sets the width of the "bounce zones", or edges of the image where we will slow down as we approach a scroll limit and speed up as we scroll away from it. This is expressed as a percentage of the image scroll dimension (width for landscape images and height for portrait images). Valid values are 0.0 for no bounce effect up to 0.5 for a very exaggerated bounce effect. The default value is 0.06. -
getMinSpeedRatio
public float getMinSpeedRatio() -
setMinSpeedRatio
public void setMinSpeedRatio(float minSpeedRatio) Sets the minimum allowable scroll speed during deceleration/acceleration, expressed as a percentage of the maximum speed. Note that the image will always scroll by at least one pixel per frame, to prevent the case where a fractional pixel movement value will be rounded down to 0 and the animation freezes. -
setImage
Sets the image to be scrolled. Can be landscape (wider than tall) or portrait (taller than wide). Large images are better! The whole point is to scroll within an image larger than the display area. Think like 3000x1080 or something. The image will be scaled if needed so that it fills the display. (For example, a 3000x960 image on a 1920x1080 display would be scaled up proportionally to 3375x1080 so that it vertically fills the display). -
getImage
-
reset
protected void reset() -
stop
public void stop()Stops the scrolling and flushes the current image. -
renderFrame
Renders a single frame of animation and handles scrolling the image by an appropriate amount. Your animation loop may target whatever FPS your application requires, and this method will scroll the image by the correct amount of pixels based on our ScrollSpeed. -
calculateSpeedMultiplier
protected float calculateSpeedMultiplier(int currentPos, int minPos, int screenSize) Calculates speed multiplier based on distance from bounce points- Parameters:
currentPos- Current position (xOffset or yOffset)minPos- Minimum position (boundary)screenSize- Screen dimension (width or height)- Returns:
- Speed multiplier between minSpeedRatio and 1.0
-