牛逼的loading加载效果

牛逼的loading加载效果

介绍:

AnimatedCircleLoadingView一个不错的loading加载效果,自定义AnimatedCircleLoadingView设置startDeterminate()
方法启动loading页面动画setPercent()设置loading进度 ,重置resetLoading(),等几个重要方法实现。
本例子来自:http://www.itlanbao.com/code/20151208/10000/100681.html
本例子主要由TopCircleBorderView ,FinishedOkView,FinishedFailureView等实现。


主要代码实现类:

package com.github.jlmd.animatedcircleloadingview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import com.github.jlmd.animatedcircleloadingview.animator.ViewAnimator;
import com.github.jlmd.animatedcircleloadingview.component.InitialCenterCircleView;
import com.github.jlmd.animatedcircleloadingview.component.MainCircleView;
import com.github.jlmd.animatedcircleloadingview.component.PercentIndicatorView;
import com.github.jlmd.animatedcircleloadingview.component.RightCircleView;
import com.github.jlmd.animatedcircleloadingview.component.SideArcsView;
import com.github.jlmd.animatedcircleloadingview.component.TopCircleBorderView;
import com.github.jlmd.animatedcircleloadingview.component.finish.FinishedFailureView;
import com.github.jlmd.animatedcircleloadingview.component.finish.FinishedOkView;

/**
 * @author jlmd
 */
public class AnimatedCircleLoadingView extends FrameLayout {

  private static final String DEFAULT_HEX_MAIN_COLOR = "#FF9A00";
  private static final String DEFAULT_HEX_SECONDARY_COLOR = "#BDBDBD";
  private final Context context;
  private InitialCenterCircleView initialCenterCircleView;
  private MainCircleView mainCircleView;
  private RightCircleView rightCircleView;
  private SideArcsView sideArcsView;
  private TopCircleBorderView topCircleBorderView;
  private FinishedOkView finishedOkView;
  private FinishedFailureView finishedFailureView;
  private PercentIndicatorView percentIndicatorView;
  private ViewAnimator viewAnimator;
  private boolean startAnimationIndeterminate;
  private boolean startAnimationDeterminate;
  private boolean stopAnimationOk;
  private boolean stopAnimationFailure;
  private int mainColor;
  private int secondaryColor;

  public AnimatedCircleLoadingView(Context context) {
    super(context);
    this.context = context;
  }

  public AnimatedCircleLoadingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    initAttributes(attrs);
  }

  public AnimatedCircleLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;
    initAttributes(attrs);
  }

  private void initAttributes(AttributeSet attrs) {
    TypedArray attributes =
        getContext().obtainStyledAttributes(attrs, R.styleable.AnimatedCircleLoadingView);
    mainColor = attributes.getColor(R.styleable.AnimatedCircleLoadingView_mainColor,
        Color.parseColor(DEFAULT_HEX_MAIN_COLOR));
    secondaryColor = attributes.getColor(R.styleable.AnimatedCircleLoadingView_secondaryColor,
        Color.parseColor(DEFAULT_HEX_SECONDARY_COLOR));
    attributes.recycle();
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    init();
    startAnimation();
  }

  private void startAnimation() {
    if (getWidth() != 0 && getHeight() != 0) {
      if (startAnimationIndeterminate) {
        viewAnimator.startAnimator();
        startAnimationIndeterminate = false;
      }
      if (startAnimationDeterminate) {
        addView(percentIndicatorView);
        viewAnimator.startAnimator();
        startAnimationDeterminate = false;
      }
      if (stopAnimationOk) {
        stopOk();
      }
      if (stopAnimationFailure) {
        stopFailure();
      }
    }
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // Force view to be a square
    super.onMeasure(widthMeasureSpec, widthMeasureSpec);
  }

  private void init() {
    initComponents();
    addComponentsViews();
    initAnimatorHelper();
  }

  private void initComponents() {
    int width = getWidth();
    initialCenterCircleView =
        new InitialCenterCircleView(context, width, mainColor, secondaryColor);
    rightCircleView = new RightCircleView(context, width, mainColor, secondaryColor);
    sideArcsView = new SideArcsView(context, width, mainColor, secondaryColor);
    topCircleBorderView = new TopCircleBorderView(context, width, mainColor, secondaryColor);
    mainCircleView = new MainCircleView(context, width, mainColor, secondaryColor);
    finishedOkView = new FinishedOkView(context, width, mainColor, secondaryColor);
    finishedFailureView = new FinishedFailureView(context, width, mainColor, secondaryColor);
    percentIndicatorView = new PercentIndicatorView(context, width);
  }

  private void addComponentsViews() {
    addView(initialCenterCircleView);
    addView(rightCircleView);
    addView(sideArcsView);
    addView(topCircleBorderView);
    addView(mainCircleView);
    addView(finishedOkView);
    addView(finishedFailureView);
  }

  private void initAnimatorHelper() {
    viewAnimator = new ViewAnimator();
    viewAnimator.setComponentViewAnimations(initialCenterCircleView, rightCircleView, sideArcsView,
        topCircleBorderView, mainCircleView, finishedOkView, finishedFailureView,
        percentIndicatorView);
  }

  public void startIndeterminate() {
    startAnimationIndeterminate = true;
    startAnimation();
  }

  public void startDeterminate() {
    startAnimationDeterminate = true;
    startAnimation();
  }

  public void setPercent(int percent) {
    if (percentIndicatorView != null) {
      percentIndicatorView.setPercent(percent);
      if (percent == 100) {
        viewAnimator.finishOk();
      }
    }
  }

  public void stopOk() {
    if (viewAnimator == null) {
      stopAnimationOk = true;
    } else {
      viewAnimator.finishOk();
    }
  }

  public void stopFailure() {
    if (viewAnimator == null) {
      stopAnimationFailure = true;
    } else {
      viewAnimator.finishFailure();
    }
  }

  public void resetLoading() {
    viewAnimator.resetAnimator();
    setPercent(0);
  }
}

///代码调用

public class MainActivity extends Activity {

  private AnimatedCircleLoadingView animatedCircleLoadingView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    animatedCircleLoadingView = (AnimatedCircleLoadingView) findViewById(R.id.circle_loading_view);
    startLoading();
    startPercentMockThread();
  }

  private void startLoading() {
    animatedCircleLoadingView.startDeterminate();
  }

  private void startPercentMockThread() {
    Runnable runnable = new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(1500);
          for (int i = 0; i <= 100; i++) {
            Thread.sleep(65);
            changePercent(i);
          }
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    };
    new Thread(runnable).start();
  }

  private void changePercent(final int percent) {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        animatedCircleLoadingView.setPercent(percent);
      }
    });
  }

  public void resetLoading() {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        animatedCircleLoadingView.resetLoading();
      }
    });
  }
}

猜你喜欢

转载自wuchengyi2015106.iteye.com/blog/2262626