网上例子不少 当然我也是看了那个AV什么什么的里面写法 我自己这个就是简单罢了 正好项目用 我就弄出来了 顺便写这上面留作备用吧
/**
* @ProjectName: BigDemo
* @Package: com.example.bigdemo
* @ClassName: CustomLoadingView
* @Description: 自定义loadingView
* @Author: 郑伟
* @CreateDate: 2019/8/23 18:38
* @UpdateUser: 更新者
* @UpdateDate: 2019/8/23 18:38
* @UpdateRemark: 更新说明
* @Version: 1.0
*/
public class CustomLoadingView extends View {
public static final float SCALE = 1.0f;
public static final int ALPHA = 255;
private Paint mPaint;
private ArrayList<ValueAnimator> mAnimators;
private HashMap<ValueAnimator, ValueAnimator.AnimatorUpdateListener> mUpdateListeners = new HashMap<>();
private float[] scaleFloats = new float[]{SCALE,
SCALE,
SCALE,
SCALE,
SCALE,
SCALE,
SCALE,
SCALE};
private int[] alphas = new int[]{ALPHA,
ALPHA,
ALPHA,
ALPHA,
ALPHA,
ALPHA,
ALPHA,
ALPHA};
public CustomLoadingView(Context context) {
super(context);
init(context);
}
public CustomLoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
mPaint = new Paint();
mPaint.setColor(0x642700);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
onCreateAnimators();
startAnimators();
}
/**
* 启动动画
*/
public void startAnimators() {
for (int i = 0; i < mAnimators.size(); i++) {
ValueAnimator animator = mAnimators.get(i);
ValueAnimator.AnimatorUpdateListener updateListener = mUpdateListeners.get(animator);
if (updateListener != null) {
animator.addUpdateListener(updateListener);
}
animator.start();
}
}
/**
* 停止动画
*/
public void stopAnimators() {
if (mAnimators != null) {
for (ValueAnimator animator : mAnimators) {
if (animator != null && animator.isStarted()) {
animator.removeAllUpdateListeners();
animator.end();
}
}
}
}
public void setColor(int color){
mPaint.setColor(color);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthMode == MeasureSpec.AT_MOST) {
widthSize = 50;
}
if (heightMode == MeasureSpec.AT_MOST) {
heightSize = 50;
}
setMeasuredDimension(widthSize, heightSize);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float radius = getWidth() / 10;
for (int i = 0; i < 8; i++) {
canvas.save();
Point point = circleAt(getWidth(), getHeight(), getWidth() / 2 - radius, i * (Math.PI / 4));
canvas.translate(point.x, point.y);
canvas.scale(scaleFloats[i], scaleFloats[i]);
mPaint.setAlpha(alphas[i]);
canvas.drawCircle(0, 0, radius, mPaint);
canvas.restore();
}
}
private Point circleAt(int width, int height, float radius, double angle) {
float x = (float) (width / 2 + radius * (Math.cos(angle)));
float y = (float) (height / 2 + radius * (Math.sin(angle)));
return new Point(x, y);
}
private final class Point {
private float x;
private float y;
private Point(float x, float y) {
this.x = x;
this.y = y;
}
}
private void addUpdateListener(ValueAnimator animator, ValueAnimator.AnimatorUpdateListener updateListener) {
mUpdateListeners.put(animator, updateListener);
}
private void onCreateAnimators() {
ArrayList<ValueAnimator> animators = new ArrayList<>();
int[] delays = {0, 120, 240, 360, 480, 600, 720, 780, 840};
for (int i = 0; i < 8; i++) {
final int index = i;
ValueAnimator scaleAnim = ValueAnimator.ofFloat(1, 0.4f, 1);
scaleAnim.setDuration(1000);
scaleAnim.setRepeatCount(-1);
scaleAnim.setStartDelay(delays[i]);
addUpdateListener(scaleAnim, new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
scaleFloats[index] = (float) animation.getAnimatedValue();
postInvalidate();
}
});
ValueAnimator alphaAnim = ValueAnimator.ofInt(255, 77, 255);
alphaAnim.setDuration(1000);
alphaAnim.setRepeatCount(-1);
alphaAnim.setStartDelay(delays[i]);
addUpdateListener(alphaAnim, new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
alphas[index] = (int) animation.getAnimatedValue();
postInvalidate();
}
});
animators.add(scaleAnim);
animators.add(alphaAnim);
}
mAnimators = animators;
}
}