Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Add show + hide functionality #86

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class ShimmerFrameLayout extends FrameLayout {
private final Paint mContentPaint = new Paint();
private final ShimmerDrawable mShimmerDrawable = new ShimmerDrawable();

private boolean mShowShimmer = true;

public ShimmerFrameLayout(Context context) {
super(context);
init(context, null);
Expand Down Expand Up @@ -100,6 +102,36 @@ public boolean isShimmerStarted() {
return mShimmerDrawable.isShimmerStarted();
}

/**
* Sets the ShimmerDrawable to be visible.
* @param startShimmer Whether to start the shimmer again.
*/
public void showShimmer(boolean startShimmer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Show / hide should be pretty simple and self sufficient within this class.

If we call showShimmer then it should simply set a boolean and start the shimmer if needed:

public void showShimmer() {
  if (mShowShimmer) {
    return;
  }
  mShowShimmer = true;
  if (startShimmer) {
    startShimmer();
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then in dispatchDraw we can just check to see if we want to show it.

if (mShowShimmer) {
  mShimmerDrawable.draw(canvas);
}

if (mShowShimmer) {
return;
}
mShowShimmer = true;
if (startShimmer) {
startShimmer();
}
}

/** Sets the ShimmerDrawable to be invisible, stopping it in the process. */
public void hideShimmer() {
if (!mShowShimmer) {
return;
}

stopShimmer();
mShowShimmer = false;
invalidate();
}

/** Return whether the shimmer drawable is visible. */
public boolean isShimmerVisible() {
return mShowShimmer;
}

@Override
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Expand All @@ -123,7 +155,9 @@ public void onDetachedFromWindow() {
@Override
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
mShimmerDrawable.draw(canvas);
if (mShowShimmer) {
mShimmerDrawable.draw(canvas);
}
}

@Override
Expand Down