Skip to content

Commit

Permalink
Fixes #19 by adding an alternative method to perform a last-ditch pos…
Browse files Browse the repository at this point in the history
…t of the runnable when the view size is stil zero
  • Loading branch information
mattsilber committed Dec 15, 2023
1 parent e2d409a commit 9c9843a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class MainActivity: AppCompatActivity(), ScratchoffController.ThresholdChangedLi
.setClearAnimationDuration(1, TimeUnit.SECONDS)
.setClearAnimationInterpolator(LinearInterpolator())
.setUsePreDrawForLayoutEnabled(true)
.setAttemptPostForIncompleteLayout(true)
// .setTouchRadiusPx(25)
// .setThresholdAccuracyQuality(Quality.LOW)
// .setThresholdTargetRegionsProvider({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public interface Delegate {
private Long activeClearTag = 0L;

private boolean usePreDrawForLayoutEnabled = false;
private boolean attemptPostForIncompleteLayout = false;

public ScratchableLayoutDrawer(Delegate delegate) {
this.delegate = new WeakReference<>(delegate);
Expand Down Expand Up @@ -354,10 +355,7 @@ private void deferRunnableUntilViewIsLaidOut(final View view, final Runnable run
new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (runnable != null) {
runnable.run();
}

triggerOrPostRunnableOnLaidOut(view, runnable);

view
.getViewTreeObserver()
Expand All @@ -375,10 +373,7 @@ public boolean onPreDraw() {
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (runnable != null) {
runnable.run();
}

triggerOrPostRunnableOnLaidOut(view, runnable);

view
.getViewTreeObserver()
Expand All @@ -391,6 +386,22 @@ public void onGlobalLayout() {
view.requestLayout();
}

private void triggerOrPostRunnableOnLaidOut(View view, Runnable runnable) {
if (runnable == null) {
return;
}

if (attemptPostForIncompleteLayout) {
if (view.getWidth() < 1 || view.getHeight() < 1) {
view.post(runnable);

return;
}
}

runnable.run();
}

@SuppressWarnings("WeakerAccess")
public ScratchableLayoutDrawer setClearAnimationDurationMs(long clearAnimationDurationMs) {
this.clearAnimationDurationMs = clearAnimationDurationMs;
Expand All @@ -411,4 +422,11 @@ public ScratchableLayoutDrawer setUsePreDrawForLayoutEnabled(boolean usePreDrawF

return this;
}

@SuppressWarnings("WeakerAccess")
public ScratchableLayoutDrawer setAttemptPostForIncompleteLayout(boolean attemptPostForIncompleteLayout) {
this.attemptPostForIncompleteLayout = attemptPostForIncompleteLayout;

return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public interface ThresholdChangedListener {
private boolean stateRestorationEnabled;

private boolean usePreDrawForLayoutEnabled = false;
private boolean attemptPostForIncompleteLayout = false;

/**
* Create a new {@link ScratchoffController} instance targeting a scratchable layout.
Expand Down Expand Up @@ -154,7 +155,8 @@ protected ScratchableLayoutDrawer createLayoutDrawer() {
return new ScratchableLayoutDrawer(this)
.setClearAnimationDurationMs(clearAnimationDurationMs)
.setClearAnimationInterpolator(clearAnimationInterpolator)
.setUsePreDrawForLayoutEnabled(usePreDrawForLayoutEnabled);
.setUsePreDrawForLayoutEnabled(usePreDrawForLayoutEnabled)
.setAttemptPostForIncompleteLayout(attemptPostForIncompleteLayout);
}

protected ScratchoffThresholdProcessor createThresholdProcessor() {
Expand Down Expand Up @@ -484,6 +486,20 @@ public ScratchoffController setUsePreDrawForLayoutEnabled(boolean usePreDrawForL
return this;
}

/**
* Set whether or not to attempt one final last-ditch {@link View#post} when determining the
* layout sizing of our {@link #layoutDrawer} if our {@link android.view.ViewTreeObserver}
* attempt ran while the {@link #scratchableLayout}'s width or height is still zero.
* This is in attempt to fix #19 caused by the width or height of the View being
* zero when attempting to create the scratchable {@link Bitmap} instances.
* The default for this value is false for the original (crashing) behavior.
*/
public ScratchoffController setAttemptPostForIncompleteLayout(boolean attemptPostForIncompleteLayout) {
this.attemptPostForIncompleteLayout = attemptPostForIncompleteLayout;

return this;
}

public View getViewBehind() {
return behindView.get();
}
Expand Down

0 comments on commit 9c9843a

Please sign in to comment.