1、准备的包shimmer
包中的类Shimmer
package com.example.androidtx.shimmer;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Build;
import android.view.View;
public class Shimmer {
public static final int ANIMATION_DIRECTION_LTR = 0;
public static final int ANIMATION_DIRECTION_RTL = 1;
private static final int DEFAULT_REPEAT_COUNT = ValueAnimator.INFINITE;
private static final long DEFAULT_DURATION = 1000;
private static final long DEFAULT_START_DELAY = 0;
private static final int DEFAULT_DIRECTION = ANIMATION_DIRECTION_LTR;
private int repeatCount;
private long duration;
private long startDelay;
private int direction;
private Animator.AnimatorListener animatorListener;
private ObjectAnimator animator;
public Shimmer() {
repeatCount = DEFAULT_REPEAT_COUNT;
duration = DEFAULT_DURATION;
startDelay = DEFAULT_START_DELAY;
direction = DEFAULT_DIRECTION;
}
public int getRepeatCount() {
return repeatCount;
}
public Shimmer setRepeatCount(int repeatCount) {
this.repeatCount = repeatCount;
return this;
}
public long getDuration() {
return duration;
}
public Shimmer setDuration(long duration) {
this.duration = duration;
return this;
}
public long getStartDelay() {
return startDelay;
}
public Shimmer setStartDelay(long startDelay) {
this.startDelay = startDelay;
return this;
}
public int getDirection() {
return direction;
}
public Shimmer setDirection(int direction) {
if (direction != ANIMATION_DIRECTION_LTR && direction != ANIMATION_DIRECTION_RTL) {
throw new IllegalArgumentException("The animation direction must be either ANIMATION_DIRECTION_LTR or ANIMATION_DIRECTION_RTL");
}
this.direction = direction;
return this;
}
public Animator.AnimatorListener getAnimatorListener() {
return animatorListener;
}
public Shimmer setAnimatorListener(Animator.AnimatorListener animatorListener) {
this.animatorListener = animatorListener;
return this;
}
public <V extends View & ShimmerViewBase> void start(final V shimmerView) {
if (isAnimating()) {
return;
}
final Runnable animate = new Runnable() {
@Override
public void run() {
shimmerView.setShimmering(true);
float fromX = 0;
float toX = shimmerView.getWidth();
if (direction == ANIMATION_DIRECTION_RTL) {
fromX = shimmerView.getWidth();
toX = 0;
}
animator = ObjectAnimator.ofFloat(shimmerView, "gradientX", fromX, toX);
animator.setRepeatCount(repeatCount);
animator.setDuration(duration);
animator.setStartDelay(startDelay);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
shimmerView.setShimmering(false);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
shimmerView.postInvalidate();
} else {
shimmerView.postInvalidateOnAnimation();
}
animator = null;
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
if (animatorListener != null) {
animator.addListener(animatorListener);
}
animator.start();
}
};
if (!shimmerView.isSetUp()) {
shimmerView.setAnimationSetupCallback(new ShimmerViewHelper.AnimationSetupCallback() {
@Override
public void onSetupAnimation(final View target) {
animate.run();
}
});
} else {
animate.run();
}
}
public void cancel() {
if (animator != null) {
animator.cancel();
}
}
public boolean isAnimating() {
return animator != null && animator.isRunning();
}
}
ShimmerButton类
package com.example.androidtx.shimmer;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.Button;
public class ShimmerButton extends Button implements ShimmerViewBase {
private ShimmerViewHelper shimmerViewHelper;
public ShimmerButton(Context context) {
super(context);
shimmerViewHelper = new ShimmerViewHelper(this, getPaint(), null);
shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
}
public ShimmerButton(Context context, AttributeSet attrs) {
super(context, attrs);
shimmerViewHelper = new ShimmerViewHelper(this, getPaint(), attrs);
shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
}
public ShimmerButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
shimmerViewHelper = new ShimmerViewHelper(this, getPaint(), attrs);
shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
}
@Override
public float getGradientX() {
return shimmerViewHelper.getGradientX();
}
@Override
public void setGradientX(float gradientX) {
shimmerViewHelper.setGradientX(gradientX);
}
@Override
public boolean isShimmering() {
return shimmerViewHelper.isShimmering();
}
@Override
public void setShimmering(boolean isShimmering) {
shimmerViewHelper.setShimmering(isShimmering);
}
@Override
public boolean isSetUp() {
return shimmerViewHelper.isSetUp();
}
@Override
public void setAnimationSetupCallback(ShimmerViewHelper.AnimationSetupCallback callback) {
shimmerViewHelper.setAnimationSetupCallback(callback);
}
@Override
public int getPrimaryColor() {
return shimmerViewHelper.getPrimaryColor();
}
@Override
public void setPrimaryColor(int primaryColor) {
shimmerViewHelper.setPrimaryColor(primaryColor);
}
@Override
public int getReflectionColor() {
return shimmerViewHelper.getReflectionColor();
}
@Override
public void setReflectionColor(int reflectionColor) {
shimmerViewHelper.setReflectionColor(reflectionColor);
}
@Override
public void setTextColor(int color) {
super.setTextColor(color);
if (shimmerViewHelper != null) {
shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
}
}
@Override
public void setTextColor(ColorStateList colors) {
super.setTextColor(colors);
if (shimmerViewHelper != null) {
shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (shimmerViewHelper != null) {
shimmerViewHelper.onSizeChanged();
}
}
@Override
public void onDraw(Canvas canvas) {
if (shimmerViewHelper != null) {
shimmerViewHelper.onDraw();
}
super.onDraw(canvas);
}
}
ShimmerTextView类
package com.example.androidtx.shimmer;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;
public class ShimmerTextView extends TextView implements ShimmerViewBase {
private ShimmerViewHelper shimmerViewHelper;
public ShimmerTextView(Context context) {
super(context);
shimmerViewHelper = new ShimmerViewHelper(this, getPaint(), null);
shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
}
public ShimmerTextView(Context context, AttributeSet attrs) {
super(context, attrs);
shimmerViewHelper = new ShimmerViewHelper(this, getPaint(), attrs);
shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
}
public ShimmerTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
shimmerViewHelper = new ShimmerViewHelper(this, getPaint(), attrs);
shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
}
@Override
public float getGradientX() {
return shimmerViewHelper.getGradientX();
}
@Override
public void setGradientX(float gradientX) {
shimmerViewHelper.setGradientX(gradientX);
}
@Override
public boolean isShimmering() {
return shimmerViewHelper.isShimmering();
}
@Override
public void setShimmering(boolean isShimmering) {
shimmerViewHelper.setShimmering(isShimmering);
}
@Override
public boolean isSetUp() {
return shimmerViewHelper.isSetUp();
}
@Override
public void setAnimationSetupCallback(ShimmerViewHelper.AnimationSetupCallback callback) {
shimmerViewHelper.setAnimationSetupCallback(callback);
}
@Override
public int getPrimaryColor() {
return shimmerViewHelper.getPrimaryColor();
}
@Override
public void setPrimaryColor(int primaryColor) {
shimmerViewHelper.setPrimaryColor(primaryColor);
}
@Override
public int getReflectionColor() {
return shimmerViewHelper.getReflectionColor();
}
@Override
public void setReflectionColor(int reflectionColor) {
shimmerViewHelper.setReflectionColor(reflectionColor);
}
@Override
public void setTextColor(int color) {
super.setTextColor(color);
if (shimmerViewHelper != null) {
shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
}
}
@Override
public void setTextColor(ColorStateList colors) {
super.setTextColor(colors);
if (shimmerViewHelper != null) {
shimmerViewHelper.setPrimaryColor(getCurrentTextColor());
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (shimmerViewHelper != null) {
shimmerViewHelper.onSizeChanged();
}
}
@Override
public void onDraw(Canvas canvas) {
if (shimmerViewHelper != null) {
shimmerViewHelper.onDraw();
}
super.onDraw(canvas);
}
}
ShimmerViewBase类
public interface ShimmerViewBase {
public float getGradientX();
public void setGradientX(float gradientX);
public boolean isShimmering();
public void setShimmering(boolean isShimmering);
public boolean isSetUp();
public void setAnimationSetupCallback(ShimmerViewHelper.AnimationSetupCallback callback);
public int getPrimaryColor();
public void setPrimaryColor(int primaryColor);
public int getReflectionColor();
public void setReflectionColor(int reflectionColor);
}
ShimmerViewHelper类
package com.example.androidtx.shimmer;
import com.example.androidtx.R;
import android.content.res.TypedArray;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
public class ShimmerViewHelper {
public interface AnimationSetupCallback {
void onSetupAnimation(View target);
}
private static final int DEFAULT_REFLECTION_COLOR = 0xFFFFFFFF;
private View view;
private Paint paint;
// center position of the gradient
private float gradientX;
// shader applied on the text view
// only null until the first global layout
private LinearGradient linearGradient;
// shader's local matrix
// never null
private Matrix linearGradientMatrix;
private int primaryColor;
// shimmer reflection color
private int reflectionColor;
// true when animating
private boolean isShimmering;
// true after first global layout
private boolean isSetUp;
// callback called after first global layout
private AnimationSetupCallback callback;
public ShimmerViewHelper(View view, Paint paint, AttributeSet attributeSet) {
this.view = view;
this.paint = paint;
init(attributeSet);
}
public float getGradientX() {
return gradientX;
}
public void setGradientX(float gradientX) {
this.gradientX = gradientX;
view.invalidate();
}
public boolean isShimmering() {
return isShimmering;
}
public void setShimmering(boolean isShimmering) {
this.isShimmering = isShimmering;
}
public boolean isSetUp() {
return isSetUp;
}
public void setAnimationSetupCallback(AnimationSetupCallback callback) {
this.callback = callback;
}
public int getPrimaryColor() {
return primaryColor;
}
public void setPrimaryColor(int primaryColor) {
this.primaryColor = primaryColor;
if (isSetUp) {
resetLinearGradient();
}
}
public int getReflectionColor() {
return reflectionColor;
}
public void setReflectionColor(int reflectionColor) {
this.reflectionColor = reflectionColor;
if (isSetUp) {
resetLinearGradient();
}
}
private void init(AttributeSet attributeSet) {
reflectionColor = DEFAULT_REFLECTION_COLOR;
if (attributeSet != null) {
TypedArray a = view.getContext().obtainStyledAttributes(attributeSet, R.styleable.ShimmerView, 0, 0);
if (a != null) {
try {
reflectionColor = a.getColor(R.styleable.ShimmerView_reflectionColor, DEFAULT_REFLECTION_COLOR);
} catch (Exception e) {
android.util.Log.e("ShimmerTextView", "Error while creating the view:", e);
} finally {
a.recycle();
}
}
}
linearGradientMatrix = new Matrix();
}
private void resetLinearGradient() {
// our gradient is a simple linear gradient from textColor to reflectionColor. its axis is at the center
// when it's outside of the view, the outer color (textColor) will be repeated (Shader.TileMode.CLAMP)
// initially, the linear gradient is positioned on the left side of the view
linearGradient = new LinearGradient(-view.getWidth(), 0, 0, 0,
new int[]{
primaryColor,
reflectionColor,
primaryColor,
},
new float[]{
0,
0.5f,
1
},
Shader.TileMode.CLAMP
);
paint.setShader(linearGradient);
}
protected void onSizeChanged() {
resetLinearGradient();
if (!isSetUp) {
isSetUp = true;
if (callback != null) {
callback.onSetupAnimation(view);
}
}
}
/**
* content of the wrapping view's onDraw(Canvas)
* MUST BE CALLED BEFORE SUPER STATEMENT
*/
public void onDraw() {
// only draw the shader gradient over the text while animating
if (isShimmering) {
// first onDraw() when shimmering
if (paint.getShader() == null) {
paint.setShader(linearGradient);
}
// translate the shader local matrix
linearGradientMatrix.setTranslate(2 * gradientX, 0);
// this is required in order to invalidate the shader's position
linearGradient.setLocalMatrix(linearGradientMatrix);
} else {
// we're not animating, remove the shader from the paint
paint.setShader(null);
}
}
}
其中attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<declare-styleable name="ShimmerView">
<attr name="reflectionColor" format="color"/>
</declare-styleable>
</resources>
anim文件夹里fade_ins.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
在布局文件中
<com.example.androidtx.shimmer.ShimmerTextView
android:id="@+id/favShimmerTextView"
android:text="Loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#444"
android:textSize="50sp"
android:layout_centerInParent="true"
/>
activity
public class MainActivity extends Activity {
ShimmerTextView favShimmerTextView;
Shimmer shimmer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
favShimmerTextView = (ShimmerTextView) findViewById(R.id.favShimmerTextView);
if (shimmer == null) {
shimmer = new Shimmer();
}
shimmer.setRepeatCount(0).setDuration(800).setStartDelay(300)
.setDirection(Shimmer.ANIMATION_DIRECTION_LTR)
.setAnimatorListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
// main_content_frame.setVisibility(View.VISIBLE);
// main_content_frame.startAnimation(AnimationUtils.loadAnimation(DemoOfUiActivity.this,
// R.anim.fade_ins));
// favShimmerReaLayout.setVisibility(View.GONE);
Intent intent = new Intent(MainActivity.this,
OtherActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_ins, 0);
finish();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
shimmer.start(favShimmerTextView);
}
@Override
protected void onDestroy() {
if (shimmer.isAnimating()) {
shimmer.cancel();
}
super.onDestroy();
}
}