一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Android自定义谷歌风格ProgressBar

时间:2017-04-13 编辑:简简单单 来源:一聚教程网

本文实例为大家分享了谷歌风格ProgressBar的具体代码,供大家参考,具体内容如下

具体代码

 

 代码如下复制代码

packagezms.demo.colorprogress;

 

importandroid.content.Context;

importandroid.content.res.Resources;

importandroid.content.res.TypedArray;

importandroid.graphics.Canvas;

importandroid.graphics.drawable.Drawable;

importandroid.util.AttributeSet;

importandroid.view.animation.AccelerateDecelerateInterpolator;

importandroid.view.animation.AccelerateInterpolator;

importandroid.view.animation.DecelerateInterpolator;

importandroid.view.animation.Interpolator;

importandroid.view.animation.LinearInterpolator;

importandroid.widget.ProgressBar;

 

publicclassGoogleProgressBarextendsProgressBar {

 

  privatestaticfinalintINTERPOLATOR_ACCELERATE =0;

  privatestaticfinalintINTERPOLATOR_LINEAR =1;

  privatestaticfinalintINTERPOLATOR_ACCELERATEDECELERATE =2;

  privatestaticfinalintINTERPOLATOR_DECELERATE =3;

 

  publicGoogleProgressBar(Context context) {

    this(context,null);

  }

 

  publicGoogleProgressBar(Context context, AttributeSet attrs) {

    this(context, attrs, R.attr.googleProgressStyle);

  }

 

  publicGoogleProgressBar(Context context, AttributeSet attrs,intdefStyle) {

    super(context, attrs, defStyle);

 

    if(isInEditMode()) {

      setIndeterminateDrawable(newGoogleProgressDrawable.Builder(

          context,true).build());

      return;

    }

 

    Resources res = context.getResources();

    TypedArray a = context.obtainStyledAttributes(attrs,

        R.styleable.GoogleStyleProgressBar, defStyle,0);

 

    finalintcolor = a.getColor(R.styleable.GoogleStyleProgressBar_color,

        res.getColor(R.color.default_color));

    finalintsectionsCount = a.getInteger(

        R.styleable.GoogleStyleProgressBar_sections_count,

        res.getInteger(R.integer.default_sections_count));

    finalintseparatorLength = a

        .getDimensionPixelSize(

            R.styleable.GoogleStyleProgressBar_stroke_separator_length,

            res.getDimensionPixelSize(R.dimen.default_stroke_separator_length));

    finalfloatstrokeWidth = a.getDimension(

        R.styleable.GoogleStyleProgressBar_stroke_width,

        res.getDimension(R.dimen.default_stroke_width));

    finalfloatspeed = a.getFloat(

        R.styleable.GoogleStyleProgressBar_speed,

        Float.parseFloat(res.getString(R.string.default_speed)));

    finalfloatspeedProgressiveStart = a.getFloat(

        R.styleable.GoogleStyleProgressBar_progressiveStart_speed,

        speed);

    finalfloatspeedProgressiveStop = a

        .getFloat(

            R.styleable.GoogleStyleProgressBar_progressiveStop_speed,

            speed);

    finalintiInterpolator = a.getInteger(

        R.styleable.GoogleStyleProgressBar_interpolator, -1);

    finalbooleanreversed = a.getBoolean(

        R.styleable.GoogleStyleProgressBar_reversed,

        res.getBoolean(R.bool.default_reversed));

    finalbooleanmirrorMode = a.getBoolean(

        R.styleable.GoogleStyleProgressBar_mirror_mode,

        res.getBoolean(R.bool.default_mirror_mode));

    finalintcolorsId = a.getResourceId(

        R.styleable.GoogleStyleProgressBar_colors,0);

    finalbooleanprogressiveStartActivated = a.getBoolean(

        R.styleable.GoogleStyleProgressBar_progressiveStart_activated,

        res.getBoolean(R.bool.default_progressiveStart_activated));

    finalDrawable backgroundDrawable = a

        .getDrawable(R.styleable.GoogleStyleProgressBar_background);

    finalbooleangenerateBackgroundWithColors = a

        .getBoolean(

            R.styleable.GoogleStyleProgressBar_generate_background_with_colors,

            false);

    finalbooleangradients = a.getBoolean(

        R.styleable.GoogleStyleProgressBar_gradients,false);

    a.recycle();

 

    // interpolator

    Interpolator interpolator =null;

    if(iInterpolator == -1) {

      interpolator = getInterpolator();

    }

    if(interpolator ==null) {

      switch(iInterpolator) {

      caseINTERPOLATOR_ACCELERATEDECELERATE:

        interpolator =newAccelerateDecelerateInterpolator();

        break;

      caseINTERPOLATOR_DECELERATE:

        interpolator =newDecelerateInterpolator();

        break;

      caseINTERPOLATOR_LINEAR:

        interpolator =newLinearInterpolator();

        break;

      caseINTERPOLATOR_ACCELERATE:

      default:

        interpolator =newAccelerateInterpolator();

      }

    }

 

    int[] colors =null;

    // colors

    if(colorsId !=0) {

      colors = res.getIntArray(colorsId);

    }

 

    GoogleProgressDrawable.Builder builder =newGoogleProgressDrawable.Builder(

        context).speed(speed)

        .progressiveStartSpeed(speedProgressiveStart)

        .progressiveStopSpeed(speedProgressiveStop)

        .interpolator(interpolator).sectionsCount(sectionsCount)

        .separatorLength(separatorLength).strokeWidth(strokeWidth)

        .reversed(reversed).mirrorMode(mirrorMode)

        .progressiveStart(progressiveStartActivated)

        .gradients(gradients);

 

    if(backgroundDrawable !=null) {

      builder.backgroundDrawable(backgroundDrawable);

    }

 

    if(generateBackgroundWithColors) {

      builder.generateBackgroundUsingColors();

    }

 

    if(colors !=null&& colors.length >0)

      builder.colors(colors);

    else

      builder.color(color);

 

    GoogleProgressDrawable d = builder.build();

    setIndeterminateDrawable(d);

  }

 

  publicvoidapplyStyle(intstyleResId) {

    TypedArray a = getContext().obtainStyledAttributes(null,

        R.styleable.GoogleStyleProgressBar,0, styleResId);

 

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_color)) {

      setSmoothProgressDrawableColor(a.getColor(

          R.styleable.GoogleStyleProgressBar_color,0));

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_colors)) {

      intcolorsId = a.getResourceId(

          R.styleable.GoogleStyleProgressBar_colors,0);

      if(colorsId !=0) {

        int[] colors = getResources().getIntArray(colorsId);

        if(colors !=null&& colors.length >0)

          setSmoothProgressDrawableColors(colors);

      }

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_sections_count)) {

      setSmoothProgressDrawableSectionsCount(a.getInteger(

          R.styleable.GoogleStyleProgressBar_sections_count,0));

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_stroke_separator_length)) {

      setSmoothProgressDrawableSeparatorLength(a.getDimensionPixelSize(

          R.styleable.GoogleStyleProgressBar_stroke_separator_length,

          0));

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_stroke_width)) {

      setSmoothProgressDrawableStrokeWidth(a.getDimension(

          R.styleable.GoogleStyleProgressBar_stroke_width,0));

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_speed)) {

      setSmoothProgressDrawableSpeed(a.getFloat(

          R.styleable.GoogleStyleProgressBar_speed,0));

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_progressiveStart_speed)) {

      setSmoothProgressDrawableProgressiveStartSpeed(a.getFloat(

          R.styleable.GoogleStyleProgressBar_progressiveStart_speed,

          0));

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_progressiveStop_speed)) {

      setSmoothProgressDrawableProgressiveStopSpeed(a

          .getFloat(

              R.styleable.GoogleStyleProgressBar_progressiveStop_speed,

              0));

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_reversed)) {

      setSmoothProgressDrawableReversed(a.getBoolean(

          R.styleable.GoogleStyleProgressBar_reversed,false));

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_mirror_mode)) {

      setSmoothProgressDrawableMirrorMode(a.getBoolean(

          R.styleable.GoogleStyleProgressBar_mirror_mode,false));

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_progressiveStart_activated)) {

      setProgressiveStartActivated(a

          .getBoolean(

              R.styleable.GoogleStyleProgressBar_progressiveStart_activated,

              false));

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_progressiveStart_activated)) {

      setProgressiveStartActivated(a

          .getBoolean(

              R.styleable.GoogleStyleProgressBar_progressiveStart_activated,

              false));

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_gradients)) {

      setSmoothProgressDrawableUseGradients(a.getBoolean(

          R.styleable.GoogleStyleProgressBar_gradients,false));

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_generate_background_with_colors)) {

      if(a.getBoolean(

          R.styleable.GoogleStyleProgressBar_generate_background_with_colors,

          false)) {

        setSmoothProgressDrawableBackgroundDrawable(GoogleProgressBarUtils

            .generateDrawableWithColors(

                checkIndeterminateDrawable().getColors(),

                checkIndeterminateDrawable().getStrokeWidth()));

      }

    }

    if(a.hasValue(R.styleable.GoogleStyleProgressBar_interpolator)) {

      intiInterpolator = a.getInteger(

          R.styleable.GoogleStyleProgressBar_interpolator, -1);

      Interpolator interpolator;

      switch(iInterpolator) {

      caseINTERPOLATOR_ACCELERATEDECELERATE:

        interpolator =newAccelerateDecelerateInterpolator();

        break;

      caseINTERPOLATOR_DECELERATE:

        interpolator =newDecelerateInterpolator();

        break;

      caseINTERPOLATOR_LINEAR:

        interpolator =newLinearInterpolator();

        break;

      caseINTERPOLATOR_ACCELERATE:

        interpolator =newAccelerateInterpolator();

        break;

      default:

        interpolator =null;

      }

      if(interpolator !=null) {

        setInterpolator(interpolator);

      }

    }

    a.recycle();

  }

 

  @Override

  protectedsynchronizedvoidonDraw(Canvas canvas) {

    super.onDraw(canvas);

    if(isIndeterminate()

        && getIndeterminateDrawable()instanceofGoogleProgressDrawable

        && !((GoogleProgressDrawable) getIndeterminateDrawable())

            .isRunning()) {

      getIndeterminateDrawable().draw(canvas);

    }

  }

 

  privateGoogleProgressDrawable checkIndeterminateDrawable() {

    Drawable ret = getIndeterminateDrawable();

    if(ret ==null|| !(retinstanceofGoogleProgressDrawable))

      thrownewRuntimeException(

          "The drawable is not a SmoothProgressDrawable");

    return(GoogleProgressDrawable) ret;

  }

 

  @Override

  publicvoidsetInterpolator(Interpolator interpolator) {

    super.setInterpolator(interpolator);

    Drawable ret = getIndeterminateDrawable();

    if(ret !=null&& (retinstanceofGoogleProgressDrawable))

      ((GoogleProgressDrawable) ret).setInterpolator(interpolator);

  }

 

  publicvoidsetSmoothProgressDrawableInterpolator(Interpolator interpolator) {

    checkIndeterminateDrawable().setInterpolator(interpolator);

  }

 

  publicvoidsetSmoothProgressDrawableColors(int[] colors) {

    checkIndeterminateDrawable().setColors(colors);

  }

 

  publicvoidsetSmoothProgressDrawableColor(intcolor) {

    checkIndeterminateDrawable().setColor(color);

  }

 

  publicvoidsetSmoothProgressDrawableSpeed(floatspeed) {

    checkIndeterminateDrawable().setSpeed(speed);

  }

 

  publicvoidsetSmoothProgressDrawableProgressiveStartSpeed(floatspeed) {

    checkIndeterminateDrawable().setProgressiveStartSpeed(speed);

  }

 

  publicvoidsetSmoothProgressDrawableProgressiveStopSpeed(floatspeed) {

    checkIndeterminateDrawable().setProgressiveStopSpeed(speed);

  }

 

  publicvoidsetSmoothProgressDrawableSectionsCount(intsectionsCount) {

    checkIndeterminateDrawable().setSectionsCount(sectionsCount);

  }

 

  publicvoidsetSmoothProgressDrawableSeparatorLength(intseparatorLength) {

    checkIndeterminateDrawable().setSeparatorLength(separatorLength);

  }

 

  publicvoidsetSmoothProgressDrawableStrokeWidth(floatstrokeWidth) {

    checkIndeterminateDrawable().setStrokeWidth(strokeWidth);

  }

 

  publicvoidsetSmoothProgressDrawableReversed(booleanreversed) {

    checkIndeterminateDrawable().setReversed(reversed);

  }

 

  publicvoidsetSmoothProgressDrawableMirrorMode(booleanmirrorMode) {

    checkIndeterminateDrawable().setMirrorMode(mirrorMode);

  }

 

  publicvoidsetProgressiveStartActivated(booleanprogressiveStartActivated) {

    checkIndeterminateDrawable().setProgressiveStartActivated(

        progressiveStartActivated);

  }

 

  publicvoidsetSmoothProgressDrawableCallbacks(

      GoogleProgressDrawable.Callbacks listener) {

    checkIndeterminateDrawable().setCallbacks(listener);

  }

 

  publicvoidsetSmoothProgressDrawableBackgroundDrawable(Drawable drawable) {

    checkIndeterminateDrawable().setBackgroundDrawable(drawable);

  }

 

  publicvoidsetSmoothProgressDrawableUseGradients(booleanuseGradients) {

    checkIndeterminateDrawable().setUseGradients(useGradients);

  }

 

  publicvoidprogressiveStart() {

    checkIndeterminateDrawable().progressiveStart();

  }

 

  publicvoidprogressiveStop() {

    checkIndeterminateDrawable().progressiveStop();

  }

}

 

热门栏目