欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义谷歌风格ProgressBar

程序员文章站 2023-12-19 23:07:16
本文实例为大家分享了谷歌风格progressbar的具体代码,供大家参考,具体内容如下 具体代码 package zms.demo.colorprogres...

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

Android自定义谷歌风格ProgressBar

具体代码

package zms.demo.colorprogress;

import android.content.context;
import android.content.res.resources;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.drawable.drawable;
import android.util.attributeset;
import android.view.animation.acceleratedecelerateinterpolator;
import android.view.animation.accelerateinterpolator;
import android.view.animation.decelerateinterpolator;
import android.view.animation.interpolator;
import android.view.animation.linearinterpolator;
import android.widget.progressbar;

public class googleprogressbar extends progressbar {

  private static final int interpolator_accelerate = 0;
  private static final int interpolator_linear = 1;
  private static final int interpolator_acceleratedecelerate = 2;
  private static final int interpolator_decelerate = 3;

  public googleprogressbar(context context) {
    this(context, null);
  }

  public googleprogressbar(context context, attributeset attrs) {
    this(context, attrs, r.attr.googleprogressstyle);
  }

  public googleprogressbar(context context, attributeset attrs, int defstyle) {
    super(context, attrs, defstyle);

    if (isineditmode()) {
      setindeterminatedrawable(new googleprogressdrawable.builder(
          context, true).build());
      return;
    }

    resources res = context.getresources();
    typedarray a = context.obtainstyledattributes(attrs,
        r.styleable.googlestyleprogressbar, defstyle, 0);

    final int color = a.getcolor(r.styleable.googlestyleprogressbar_color,
        res.getcolor(r.color.default_color));
    final int sectionscount = a.getinteger(
        r.styleable.googlestyleprogressbar_sections_count,
        res.getinteger(r.integer.default_sections_count));
    final int separatorlength = a
        .getdimensionpixelsize(
            r.styleable.googlestyleprogressbar_stroke_separator_length,
            res.getdimensionpixelsize(r.dimen.default_stroke_separator_length));
    final float strokewidth = a.getdimension(
        r.styleable.googlestyleprogressbar_stroke_width,
        res.getdimension(r.dimen.default_stroke_width));
    final float speed = a.getfloat(
        r.styleable.googlestyleprogressbar_speed,
        float.parsefloat(res.getstring(r.string.default_speed)));
    final float speedprogressivestart = a.getfloat(
        r.styleable.googlestyleprogressbar_progressivestart_speed,
        speed);
    final float speedprogressivestop = a
        .getfloat(
            r.styleable.googlestyleprogressbar_progressivestop_speed,
            speed);
    final int iinterpolator = a.getinteger(
        r.styleable.googlestyleprogressbar_interpolator, -1);
    final boolean reversed = a.getboolean(
        r.styleable.googlestyleprogressbar_reversed,
        res.getboolean(r.bool.default_reversed));
    final boolean mirrormode = a.getboolean(
        r.styleable.googlestyleprogressbar_mirror_mode,
        res.getboolean(r.bool.default_mirror_mode));
    final int colorsid = a.getresourceid(
        r.styleable.googlestyleprogressbar_colors, 0);
    final boolean progressivestartactivated = a.getboolean(
        r.styleable.googlestyleprogressbar_progressivestart_activated,
        res.getboolean(r.bool.default_progressivestart_activated));
    final drawable backgrounddrawable = a
        .getdrawable(r.styleable.googlestyleprogressbar_background);
    final boolean generatebackgroundwithcolors = a
        .getboolean(
            r.styleable.googlestyleprogressbar_generate_background_with_colors,
            false);
    final boolean gradients = a.getboolean(
        r.styleable.googlestyleprogressbar_gradients, false);
    a.recycle();

    // interpolator
    interpolator interpolator = null;
    if (iinterpolator == -1) {
      interpolator = getinterpolator();
    }
    if (interpolator == null) {
      switch (iinterpolator) {
      case interpolator_acceleratedecelerate:
        interpolator = new acceleratedecelerateinterpolator();
        break;
      case interpolator_decelerate:
        interpolator = new decelerateinterpolator();
        break;
      case interpolator_linear:
        interpolator = new linearinterpolator();
        break;
      case interpolator_accelerate:
      default:
        interpolator = new accelerateinterpolator();
      }
    }

    int[] colors = null;
    // colors
    if (colorsid != 0) {
      colors = res.getintarray(colorsid);
    }

    googleprogressdrawable.builder builder = new googleprogressdrawable.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);
  }

  public void applystyle(int styleresid) {
    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)) {
      int colorsid = 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)) {
      int iinterpolator = a.getinteger(
          r.styleable.googlestyleprogressbar_interpolator, -1);
      interpolator interpolator;
      switch (iinterpolator) {
      case interpolator_acceleratedecelerate:
        interpolator = new acceleratedecelerateinterpolator();
        break;
      case interpolator_decelerate:
        interpolator = new decelerateinterpolator();
        break;
      case interpolator_linear:
        interpolator = new linearinterpolator();
        break;
      case interpolator_accelerate:
        interpolator = new accelerateinterpolator();
        break;
      default:
        interpolator = null;
      }
      if (interpolator != null) {
        setinterpolator(interpolator);
      }
    }
    a.recycle();
  }

  @override
  protected synchronized void ondraw(canvas canvas) {
    super.ondraw(canvas);
    if (isindeterminate()
        && getindeterminatedrawable() instanceof googleprogressdrawable
        && !((googleprogressdrawable) getindeterminatedrawable())
            .isrunning()) {
      getindeterminatedrawable().draw(canvas);
    }
  }

  private googleprogressdrawable checkindeterminatedrawable() {
    drawable ret = getindeterminatedrawable();
    if (ret == null || !(ret instanceof googleprogressdrawable))
      throw new runtimeexception(
          "the drawable is not a smoothprogressdrawable");
    return (googleprogressdrawable) ret;
  }

  @override
  public void setinterpolator(interpolator interpolator) {
    super.setinterpolator(interpolator);
    drawable ret = getindeterminatedrawable();
    if (ret != null && (ret instanceof googleprogressdrawable))
      ((googleprogressdrawable) ret).setinterpolator(interpolator);
  }

  public void setsmoothprogressdrawableinterpolator(interpolator interpolator) {
    checkindeterminatedrawable().setinterpolator(interpolator);
  }

  public void setsmoothprogressdrawablecolors(int[] colors) {
    checkindeterminatedrawable().setcolors(colors);
  }

  public void setsmoothprogressdrawablecolor(int color) {
    checkindeterminatedrawable().setcolor(color);
  }

  public void setsmoothprogressdrawablespeed(float speed) {
    checkindeterminatedrawable().setspeed(speed);
  }

  public void setsmoothprogressdrawableprogressivestartspeed(float speed) {
    checkindeterminatedrawable().setprogressivestartspeed(speed);
  }

  public void setsmoothprogressdrawableprogressivestopspeed(float speed) {
    checkindeterminatedrawable().setprogressivestopspeed(speed);
  }

  public void setsmoothprogressdrawablesectionscount(int sectionscount) {
    checkindeterminatedrawable().setsectionscount(sectionscount);
  }

  public void setsmoothprogressdrawableseparatorlength(int separatorlength) {
    checkindeterminatedrawable().setseparatorlength(separatorlength);
  }

  public void setsmoothprogressdrawablestrokewidth(float strokewidth) {
    checkindeterminatedrawable().setstrokewidth(strokewidth);
  }

  public void setsmoothprogressdrawablereversed(boolean reversed) {
    checkindeterminatedrawable().setreversed(reversed);
  }

  public void setsmoothprogressdrawablemirrormode(boolean mirrormode) {
    checkindeterminatedrawable().setmirrormode(mirrormode);
  }

  public void setprogressivestartactivated(boolean progressivestartactivated) {
    checkindeterminatedrawable().setprogressivestartactivated(
        progressivestartactivated);
  }

  public void setsmoothprogressdrawablecallbacks(
      googleprogressdrawable.callbacks listener) {
    checkindeterminatedrawable().setcallbacks(listener);
  }

  public void setsmoothprogressdrawablebackgrounddrawable(drawable drawable) {
    checkindeterminatedrawable().setbackgrounddrawable(drawable);
  }

  public void setsmoothprogressdrawableusegradients(boolean usegradients) {
    checkindeterminatedrawable().setusegradients(usegradients);
  }

  public void progressivestart() {
    checkindeterminatedrawable().progressivestart();
  }

  public void progressivestop() {
    checkindeterminatedrawable().progressivestop();
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: