This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
/
ShimmerFrameLayout.java
204 lines (175 loc) · 5.73 KB
/
ShimmerFrameLayout.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.shimmer;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* Shimmer is an Android library that provides an easy way to add a shimmer effect to any {@link
* android.view.View}. It is useful as an unobtrusive loading indicator, and was originally
* developed for Facebook Home.
*
* <p>Find more examples and usage instructions over at: facebook.github.io/shimmer-android
*/
public class ShimmerFrameLayout extends FrameLayout {
private final Paint mContentPaint = new Paint();
private final ShimmerDrawable mShimmerDrawable = new ShimmerDrawable();
private boolean mShowShimmer = true;
private boolean mStoppedShimmerBecauseVisibility = false;
public ShimmerFrameLayout(Context context) {
super(context);
init(context, null);
}
public ShimmerFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public ShimmerFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ShimmerFrameLayout(
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, @Nullable AttributeSet attrs) {
setWillNotDraw(false);
mShimmerDrawable.setCallback(this);
if (attrs == null) {
setShimmer(new Shimmer.AlphaHighlightBuilder().build());
return;
}
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShimmerFrameLayout, 0, 0);
try {
Shimmer.Builder shimmerBuilder =
a.hasValue(R.styleable.ShimmerFrameLayout_shimmer_colored)
&& a.getBoolean(R.styleable.ShimmerFrameLayout_shimmer_colored, false)
? new Shimmer.ColorHighlightBuilder()
: new Shimmer.AlphaHighlightBuilder();
setShimmer(shimmerBuilder.consumeAttributes(a).build());
} finally {
a.recycle();
}
}
public ShimmerFrameLayout setShimmer(@Nullable Shimmer shimmer) {
mShimmerDrawable.setShimmer(shimmer);
if (shimmer != null && shimmer.clipToChildren) {
setLayerType(LAYER_TYPE_HARDWARE, mContentPaint);
} else {
setLayerType(LAYER_TYPE_NONE, null);
}
return this;
}
public @Nullable Shimmer getShimmer() {
return mShimmerDrawable.getShimmer();
}
/** Starts the shimmer animation. */
public void startShimmer() {
if (isAttachedToWindow()) {
mShimmerDrawable.startShimmer();
}
}
/** Stops the shimmer animation. */
public void stopShimmer() {
mStoppedShimmerBecauseVisibility = false;
mShimmerDrawable.stopShimmer();
}
/** Return whether the shimmer animation has been started. */
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) {
mShowShimmer = true;
if (startShimmer) {
startShimmer();
}
invalidate();
}
/** Sets the ShimmerDrawable to be invisible, stopping it in the process. */
public void hideShimmer() {
stopShimmer();
mShowShimmer = false;
invalidate();
}
/** Return whether the shimmer drawable is visible. */
public boolean isShimmerVisible() {
return mShowShimmer;
}
public boolean isShimmerRunning() {
return mShimmerDrawable.isShimmerRunning();
}
@Override
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
final int width = getWidth();
final int height = getHeight();
mShimmerDrawable.setBounds(0, 0, width, height);
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
// View's constructor directly invokes this method, in which case no fields on
// this class have been fully initialized yet.
if (mShimmerDrawable == null) {
return;
}
if (visibility != View.VISIBLE) {
// GONE or INVISIBLE
if (isShimmerStarted()) {
stopShimmer();
mStoppedShimmerBecauseVisibility = true;
}
} else if (mStoppedShimmerBecauseVisibility) {
mShimmerDrawable.maybeStartShimmer();
mStoppedShimmerBecauseVisibility = false;
}
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
mShimmerDrawable.maybeStartShimmer();
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
stopShimmer();
}
@Override
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (mShowShimmer) {
mShimmerDrawable.draw(canvas);
}
}
@Override
protected boolean verifyDrawable(@NonNull Drawable who) {
return super.verifyDrawable(who) || who == mShimmerDrawable;
}
public void setStaticAnimationProgress(float value) {
mShimmerDrawable.setStaticAnimationProgress(value);
}
public void clearStaticAnimationProgress() {
mShimmerDrawable.clearStaticAnimationProgress();
}
}