Esempio n. 1
0
    private void initView() {
      // 圆环
      mCirclePaint = new Paint();
      mCirclePaint.setAntiAlias(true);
      mCirclePaint.setColor(mBigCircleColor);
      mCirclePaint.setStrokeWidth(mBigCircleStroke);
      mCirclePaint.setStyle(Paint.Style.STROKE);

      // 小球
      mAccBallPaint = new Paint();
      mAccBallPaint.setAntiAlias(true);
      mAccBallPaint.setColor(mAccBallColor);
      mAccBallPaint.setStyle(Paint.Style.FILL);

      rectF = new RectF(0, 0, ScreenUtil.getDisplayWidth() >> 1, ScreenUtil.getDisplayWidth() >> 1);
      startRotate(2000);
    }
Esempio n. 2
0
  @TargetApi(Build.VERSION_CODES.M)
  private class LoadingView extends View {

    private static final int DEF_DURATION = 1000;

    // paint
    private Paint mCirclePaint;
    private Paint mAccBallPaint;
    private RectF rectF;

    private int mBigCircleColor = getResources().getColor(R.color.color_green_highlight);
    private int mAccBallColor = getResources().getColor(R.color.color_orange_assist);

    private int mBigCircleStroke = ScreenUtil.dp2px(1);
    private int mDuration = DEF_DURATION;

    private float mBitRadius = ScreenUtil.dp2px(50);
    private float mSmallRadius = ScreenUtil.dp2px(10);
    private float startAngle = 0.0f;

    public LoadingView(Context context) {
      this(context, null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
      this(context, attrs, 0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      initView();
    }

    private void initView() {
      // 圆环
      mCirclePaint = new Paint();
      mCirclePaint.setAntiAlias(true);
      mCirclePaint.setColor(mBigCircleColor);
      mCirclePaint.setStrokeWidth(mBigCircleStroke);
      mCirclePaint.setStyle(Paint.Style.STROKE);

      // 小球
      mAccBallPaint = new Paint();
      mAccBallPaint.setAntiAlias(true);
      mAccBallPaint.setColor(mAccBallColor);
      mAccBallPaint.setStyle(Paint.Style.FILL);

      rectF = new RectF(0, 0, ScreenUtil.getDisplayWidth() >> 1, ScreenUtil.getDisplayWidth() >> 1);
      startRotate(2000);
    }

    private void startRotate(long duration) {
      LinearAnimation animation = new LinearAnimation();
      animation.setDuration(duration);
      animation.setRepeatCount(Animation.INFINITE);
      animation.setInterpolator(new LinearInterpolator());
      animation.setLinearAnimationListener(
          new LinearAnimation.LinearAnimationListener() {
            @Override
            public void applyTans(float interpolatedTime) {
              startAngle = 360 * interpolatedTime;
              invalidate();
            }
          });
      startAnimation(animation);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
      //            drawSeekbar(canvas, 100);
      drawSlowIndicator(startAngle, canvas);
    }

    private void drawSlowIndicator(float startAngle, Canvas canvas) {
      Paint circlePaint = new Paint();
      circlePaint.setAntiAlias(true);
      circlePaint.setColor(Color.parseColor("#A8D7A7"));
      circlePaint.setStrokeWidth(7);
      circlePaint.setStyle(Paint.Style.STROKE);
      canvas.drawPath(getArcPath(), circlePaint);

      int restoreCount = canvas.save();
      canvas.translate(rectF.centerX(), rectF.centerY());
      circlePaint.setStyle(Paint.Style.FILL);
      canvas.drawPath(getBallPath(startAngle + 90), circlePaint);
      canvas.drawPath(getBallPath(startAngle + 90 + 30 + 90), circlePaint);
      canvas.drawPath(getBallPath(startAngle + 90 + 30 + 90 + 30 + 90), circlePaint);
      canvas.restoreToCount(restoreCount);
    }

    private Path getArcPath() {
      Path path = new Path();
      path.addArc(rectF, startAngle, 90);
      path.addArc(rectF, startAngle + 90 + 30, 90);
      path.addArc(rectF, startAngle + 90 + 90 + 30 + 30, 90);
      return path;
    }

    private void drawSeekbar(Canvas canvas, float startAngle) {
      Paint circlePaint = new Paint();
      circlePaint.setAntiAlias(true);
      circlePaint.setColor(Color.parseColor("#FF4444"));
      circlePaint.setStrokeWidth(7);
      circlePaint.setStyle(Paint.Style.STROKE);

      Path path = new Path();
      path.addArc(rectF, 0, startAngle);
      canvas.drawPath(path, circlePaint);

      int restoreCount = canvas.save();

      canvas.translate(rectF.centerX(), rectF.centerY());
      circlePaint.setStyle(Paint.Style.FILL);
      canvas.drawPath(getBallPath(startAngle), circlePaint);

      canvas.restoreToCount(restoreCount);
    }

    private Path getBallPath(float startAngle) {
      double sweepAngle = Math.PI / 180 * startAngle;
      Path path = new Path();
      float x = (float) Math.cos(sweepAngle) * (rectF.width() / 2);
      float y = (float) Math.sin(sweepAngle) * (rectF.width() / 2);
      path.moveTo(x, y);
      path.addCircle(x, y, 10, Path.Direction.CCW);
      return path;
    }
  }