-
- All Implemented Interfaces:
-
android.view.View.OnTouchListener,com.jackpocket.scratchoff.ScratchableLayoutDrawer.Delegate,com.jackpocket.scratchoff.ScratchoffThresholdProcessor.Delegate,com.jackpocket.scratchoff.ScratchoffThresholdProcessor.TargetRegionsProvider,com.jackpocket.scratchoff.paths.ScratchPathPointsAggregator
public class ScratchoffController implements View.OnTouchListener, ScratchableLayoutDrawer.Delegate, ScratchoffThresholdProcessor.Delegate, ScratchPathPointsAggregator
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public interfaceScratchoffController.ThresholdChangedListener
-
Field Summary
Fields Modifier and Type Field Description private WeakReference<ScratchoffController.ThresholdChangedListener>thresholdChangedListenerprivate ScratchableLayoutDrawerlayoutDrawerprivate inttouchRadiusPxprivate ScratchoffThresholdProcessor.QualitythresholdAccuracyQualityprivate ScratchoffThresholdProcessor.TargetRegionsProviderthresholdTargetRegionsProviderprivate floatthresholdCompletionPercentprivate booleanclearOnThresholdReachedEnabledprivate booleanclearAnimationEnabledprivate InterpolatorclearAnimationInterpolatorprivate booleantouchInteractionIgnoredprivate booleanstateRestorationEnabledprivate booleanactivePathRecoveryEnabled
-
Constructor Summary
Constructors Constructor Description ScratchoffController(View scratchableLayout)Create a new ScratchoffController instance targeting a scratchable layout.
-
Method Summary
Modifier and Type Method Description ScratchoffControllersetThresholdChangedListener(ScratchoffController.ThresholdChangedListener thresholdChangedListener)Set callbacks to be triggered when the percentage of scratched area changesand the scratch threshold has been reached. ScratchableLayoutDrawergetLayoutDrawer()intgetTouchRadiusPx()ScratchoffControllersetTouchRadiusPx(int touchRadius)Set the radius, in pixels, of the circle to be scratched away on MotionEvents.Must be greater than 0, or throws an IllegalStateException. ScratchoffThresholdProcessor.QualitygetThresholdAccuracyQuality()ScratchoffControllersetThresholdAccuracyQuality(ScratchoffThresholdProcessor.Quality thresholdAccuracyQuality)Set the ScratchoffThresholdProcessor.Quality for the underlying ScratchoffThresholdProcessor. ScratchoffControllersetThresholdTargetRegionsProvider(ScratchoffThresholdProcessor.TargetRegionsProvider thresholdTargetRegionsProvider)Override the default ScratchoffThresholdProcessor.TargetRegionsProvider for the underlying ScratchoffThresholdProcessor to define specific regions of the Bitmap that shouldbe used to calculate the scratched percentage. floatgetThresholdCompletionPercent()ScratchoffControllersetThresholdCompletionPercent(float thresholdCompletionPercent)Set the threshold percentage, between [0.0f, 1.0f], that should trigger thecompletion callback and clearing, if enabled. ScratchoffControllersetClearOnThresholdReachedEnabled(boolean clearOnThresholdReachedEnabled)Set whether automatic clearing of the scratchableLayout should be performed on reaching the thresholdCompletionPercent. ScratchoffControllersetClearAnimationEnabled(boolean clearAnimationEnabled)Set whether to use the fade-out AlphaAnimation, or immediately hidethe scratchableLayout, on clearing. ScratchoffControllersetClearAnimationInterpolator(Interpolator clearAnimationInterpolator)Set the Interpolator for the fade-out AlphaAnimation run on clearing. ScratchoffControllersetTouchInteractionIgnored(boolean touchInteractionIgnored)Set to `true` to block all MotionEvent instances from being passed to anytouch observers or processors. ScratchoffControllersetStateRestorationEnabled(boolean stateRestorationEnabled)Set whether or not the history can be restored from the scratchable View's state. ScratchoffControllersetActivePathRecoveryEnabled(boolean activePathRecoveryEnabled)Set whether or not to recover from a null active android.graphics.Path duringa touch-move event, instead of allowing a NullPointerException to be thrown.This can occur when a move event arrives without a preceding down event,e.g. ScratchoffControllersetMatchLayoutWithBehindView(View behindView)When set, the system will adjust the width/height of the scratchableLayout LayoutParamsto match that of the view supplied here. ScratchoffControllerattach()Attach (or reset) to the pre-scratched state. voidonScratchableLayoutAvailable(int width, int height)booleanonTouch(View view, MotionEvent event)voidaddScratchPathPoints(Collection<ScratchPathPoint> events)Add the collection of ScratchPathPoint instances to the ScratchableLayoutDrawer, the ScratchoffThresholdProcessor,and the internal history queue; then invalidate the scratchableLayout. voiddraw(Canvas canvas)Render the scratched paths to the supplied Canvas through the ScratchableLayoutDrawer instance. ScratchoffControlleronDestroy()ScratchoffControllerclear()Call this to clear/hide the scratchableLayout and reveal the behindView. ViewgetScratchImageLayout()ScratchoffControllersetClearAnimationDuration(long value, TimeUnit unit)Set the duration of the fade-out AlphaAnimation run on clearing. ScratchoffControllersetTouchRadiusDip(Context context, int touchRadius)Set the radius, in DIP, of the circle to be scratched away on MotionEvents.Must be greater than 0, or throws an IllegalStateException. booleanisThresholdReached()ViewgetViewBehind()ScratchoffControlleraddTouchObserver(View.OnTouchListener touchListener)Add an OnTouchListener to observe MotionEvents as they are passedinto this ScratchoffController instance. ScratchoffControllerremoveTouchObserver(View.OnTouchListener touchListener)Remove a OnTouchListener from this ScratchoffController instance. voidremoveTouchObservers()Remove all touch observers from this ScratchoffController instance. List<Rect>createScratchableRegions(Bitmap source)Array<int>getScratchableLayoutSize()booleanisScratchableLayoutAvailable()voidpostScratchPercentChanged(float percent)voidpostScratchThresholdReached()voidpostInvalidateScratchableLayout()ScratchoffStateparcelize(Parcelable state)Create a ScratchoffState that can be used to restore thedrawing history of this instance. voidsetStateRestorationParcel(Parcelable state)ScratchoffControllerremovePendingStateRestorationParcel()Remove any pending restoration data. static ScratchoffControllerfindByViewId(Activity activity, int resourceId)Find the ScratchoffController instance by the View's ID in the Activity layout hierarchy. static ScratchoffControllerfindByViewId(ViewGroup parent, int resourceId)Find the ScratchoffController instance by the View's ID in the parent's layout hierarchy. -
Methods inherited from class android.view.View.OnTouchListener
onTouch -
Methods inherited from class com.jackpocket.scratchoff.ScratchoffThresholdProcessor.Delegate
postScratchPercentChanged -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Constructor Detail
-
ScratchoffController
ScratchoffController(View scratchableLayout)
Create a new ScratchoffController instance targeting a scratchable layout.
-
-
Method Detail
-
setThresholdChangedListener
ScratchoffController setThresholdChangedListener(ScratchoffController.ThresholdChangedListener thresholdChangedListener)
Set callbacks to be triggered when the percentage of scratched area changesand the scratch threshold has been reached.Callback values for scratch percentages are in the range [0.0, 100.0].You must maintain a reference to the supplied ThresholdChangedListener as it will be weakly held.
-
getLayoutDrawer
ScratchableLayoutDrawer getLayoutDrawer()
-
getTouchRadiusPx
int getTouchRadiusPx()
-
setTouchRadiusPx
ScratchoffController setTouchRadiusPx(int touchRadius)
Set the radius, in pixels, of the circle to be scratched away on MotionEvents.Must be greater than 0, or throws an IllegalStateException.Note: this must be called before attach or it will have no effect.
-
getThresholdAccuracyQuality
ScratchoffThresholdProcessor.Quality getThresholdAccuracyQuality()
-
setThresholdAccuracyQuality
ScratchoffController setThresholdAccuracyQuality(ScratchoffThresholdProcessor.Quality thresholdAccuracyQuality)
Set the ScratchoffThresholdProcessor.Quality for the underlying ScratchoffThresholdProcessor.The default is HIGH, which implies no reduction in quality.MEDIUM will attempt to reduce the quality to 50%, while LOW will use the lowest-supported quality value at runtime(1 / min (touchRadiusPx, width, height)).This reduction is solely applied to elements of the ScratchoffThresholdProcessor, and does notaffect the drawing quality in any way.If the supplied quality value is below the runtime-calculated minimum of(1 / min (touchRadiusPx, width, height)), or above the maximum (1.0f),it will be ignored in favor of the minimum/maximum values.Note: this must be called before attach or it will have no effect.
-
setThresholdTargetRegionsProvider
ScratchoffController setThresholdTargetRegionsProvider(ScratchoffThresholdProcessor.TargetRegionsProvider thresholdTargetRegionsProvider)
Override the default ScratchoffThresholdProcessor.TargetRegionsProvider for the underlying ScratchoffThresholdProcessor to define specific regions of the Bitmap that shouldbe used to calculate the scratched percentage.The size of the Bitmap used by the ScratchoffThresholdProcessor is determined by the ScratchoffThresholdProcessor.Quality and the runtime conditions of the scratchable layout. Ifthe quality is not set to HIGH, the Bitmap will likely bemuch smaller than the size on screen.It is recommended that you calculate the positions of the desired areas by their relativepositioning from the edges of the original Bitmap. e.g. left = 0.25 * bitmap.widthWarning: If any of the regions returned by the call to createScratchableRegions exceed the boundaries of the supplied Bitmap, the Threshold processor will break.
-
getThresholdCompletionPercent
float getThresholdCompletionPercent()
-
setThresholdCompletionPercent
ScratchoffController setThresholdCompletionPercent(float thresholdCompletionPercent)
Set the threshold percentage, between [0.0f, 1.0f], that should trigger thecompletion callback and clearing, if enabled.Note: this must be called before attach or it will have no effect.
-
setClearOnThresholdReachedEnabled
ScratchoffController setClearOnThresholdReachedEnabled(boolean clearOnThresholdReachedEnabled)
Set whether automatic clearing of the scratchableLayout should be performed on reaching the thresholdCompletionPercent. If false, no clearing or animations will be performed,and you must manually call clear.
-
setClearAnimationEnabled
ScratchoffController setClearAnimationEnabled(boolean clearAnimationEnabled)
Set whether to use the fade-out AlphaAnimation, or immediately hidethe scratchableLayout, on clearing.If clearOnThresholdReachedEnabled is false, this will have no effect.
-
setClearAnimationInterpolator
ScratchoffController setClearAnimationInterpolator(Interpolator clearAnimationInterpolator)
Set the Interpolator for the fade-out AlphaAnimation run on clearing. The defaultis a LinearInterpolator.If clearOnThresholdReachedEnabled is false, this will have no effect.
-
setTouchInteractionIgnored
ScratchoffController setTouchInteractionIgnored(boolean touchInteractionIgnored)
Set to `true` to block all MotionEvent instances from being passed to anytouch observers or processors. Touches will continue to be ignored until this iscalled again with a value of `false`.This is useful for instrumentation testing or feature demonstration in conjunctionwith addScratchPathPoints.
- Parameters:
touchInteractionIgnored- flag indicating if touches should be ignored
-
setStateRestorationEnabled
ScratchoffController setStateRestorationEnabled(boolean stateRestorationEnabled)
Set whether or not the history can be restored from the scratchable View's state.This restoration will occur after the call to attachIf the threshold has already been reached, the restoration will automaticallyclear the scratchable View to review the behind View, regardless of clearing settings.If the scratchable View is restored with a different layout size, no restorationwill be performed.
-
setActivePathRecoveryEnabled
ScratchoffController setActivePathRecoveryEnabled(boolean activePathRecoveryEnabled)
Set whether or not to recover from a null active android.graphics.Path duringa touch-move event, instead of allowing a NullPointerException to be thrown.This can occur when a move event arrives without a preceding down event,e.g. due to race conditions during layout callbacks.The default for this value is false for the original (crashing) behavior.
-
setMatchLayoutWithBehindView
ScratchoffController setMatchLayoutWithBehindView(View behindView)
When set, the system will adjust the width/height of the scratchableLayout LayoutParamsto match that of the view supplied here. If null, no layout-matching will beperformed.
-
attach
ScratchoffController attach()
Attach (or reset) to the pre-scratched state.If the backing scratchableLayout state is available, and stateRestorationEnabled is true, the history will beautomatically restored on the condition that the layout is thesame width/height.
-
onScratchableLayoutAvailable
void onScratchableLayoutAvailable(int width, int height)
-
onTouch
boolean onTouch(View view, MotionEvent event)
-
addScratchPathPoints
void addScratchPathPoints(Collection<ScratchPathPoint> events)
Add the collection of ScratchPathPoint instances to the ScratchableLayoutDrawer, the ScratchoffThresholdProcessor,and the internal history queue; then invalidate the scratchableLayout.Warning: this method does not ensure the layout or aggregatorsare actually available, yet.
-
draw
void draw(Canvas canvas)
Render the scratched paths to the supplied Canvas through the ScratchableLayoutDrawer instance.
-
onDestroy
ScratchoffController onDestroy()
-
clear
ScratchoffController clear()
Call this to clear/hide the scratchableLayout and reveal the behindView. If clearAnimationEnabled is true, the contentswill be faded out before altering the View's visibility.Calling this will stop all processors and prevent any further scratching until attach has been called again.
-
getScratchImageLayout
View getScratchImageLayout()
-
setClearAnimationDuration
ScratchoffController setClearAnimationDuration(long value, TimeUnit unit)
Set the duration of the fade-out AlphaAnimation run on clearing.If clearOnThresholdReachedEnabled is false, this will have no effect.
-
setTouchRadiusDip
ScratchoffController setTouchRadiusDip(Context context, int touchRadius)
Set the radius, in DIP, of the circle to be scratched away on MotionEvents.Must be greater than 0, or throws an IllegalStateException.Note: this must be called before attach or it will have no effect.
-
isThresholdReached
boolean isThresholdReached()
-
getViewBehind
View getViewBehind()
-
addTouchObserver
ScratchoffController addTouchObserver(View.OnTouchListener touchListener)
Add an OnTouchListener to observe MotionEvents as they are passedinto this ScratchoffController instance. Events will be forwarded regardless ofthe ScratchoffController's enabled state, and all return values will be ignored.If adding observers (in Activity.onResume), you should also call removeTouchObservers (in Activity.onPause).
- Parameters:
touchListener- a non-null OnTouchListener
-
removeTouchObserver
ScratchoffController removeTouchObserver(View.OnTouchListener touchListener)
Remove a OnTouchListener from this ScratchoffController instance.
- Parameters:
touchListener- a non-null OnTouchListener
-
removeTouchObservers
void removeTouchObservers()
Remove all touch observers from this ScratchoffController instance.
-
createScratchableRegions
List<Rect> createScratchableRegions(Bitmap source)
-
getScratchableLayoutSize
Array<int> getScratchableLayoutSize()
-
isScratchableLayoutAvailable
boolean isScratchableLayoutAvailable()
-
postScratchPercentChanged
void postScratchPercentChanged(float percent)
-
postScratchThresholdReached
void postScratchThresholdReached()
-
postInvalidateScratchableLayout
void postInvalidateScratchableLayout()
-
parcelize
ScratchoffState parcelize(Parcelable state)
Create a ScratchoffState that can be used to restore thedrawing history of this instance.
-
setStateRestorationParcel
void setStateRestorationParcel(Parcelable state)
-
removePendingStateRestorationParcel
ScratchoffController removePendingStateRestorationParcel()
Remove any pending restoration data. Calling this will ensurethat a subsequent call to attach will reset to a pre-scratched state.If setStateRestorationParcel is called again, this call will have no effect.
-
findByViewId
static ScratchoffController findByViewId(Activity activity, int resourceId)
Find the ScratchoffController instance by the View's ID in the Activity layout hierarchy.This is equivalent to calling getScratchoffController or getScratchoffController.
- Parameters:
resourceId- the identifier assigned to the com.jackpocket.scratchoff.views.ScratchableLinearLayout or com.jackpocket.scratchoff.views.ScratchableRelativeLayout instance in the Activity.
-
findByViewId
static ScratchoffController findByViewId(ViewGroup parent, int resourceId)
Find the ScratchoffController instance by the View's ID in the parent's layout hierarchy.This is equivalent to calling getScratchoffController.
- Parameters:
resourceId- the identifier assigned to the com.jackpocket.scratchoff.views.ScratchableLayout instance in the ViewGroup.
-
-
-
-