开源项目: circular-progress-button

本文介绍了一种带有进度条显示的按钮实现方法,通过自定义Button类并使用GradientDrawable来实现不同状态间的平滑过渡动画,包括从初始状态到圆环状态再到完成状态的变化。
带进度条显示的按钮, 其效果如下所示:
091050537815484.gif

其由三部分动画组成: 初始状态->圆环状态->完成状态.
0. 实现从初始到圆环的简单实现:
091050546403354.gif

继承自button 类, 设置其背景

    
  1. public class CircleButton extends Button implements View.OnClickListener {
  2. private StateListDrawable mIdleStateDrawable;
  3. private StrokeGradientDrawable background;
  4. public CircleButton(Context context, AttributeSet attrs) {
  5. super(context, attrs);
  6. background = new StrokeGradientDrawable();
  7. // init();
  8. // setBackground(mIdleStateDrawable);
  9. setBackground(background.getGradientDrawable());
  10. setOnClickListener(this);
  11. }
  12. //.......
  13. }
其中的StrokeGradientDrawable(照搬源码) 如下:

    
  1. public class StrokeGradientDrawable {
  2. private int mStrokeColor; //描边颜色
  3. private int mStrokeWidth; //描边宽度
  4. public int getStrokeColor() {
  5. return mStrokeColor;
  6. }
  7. public void setStrokeColor(int strokeColor) {
  8. mStrokeColor = strokeColor;
  9. mGradientDrawable.setStroke(getStrokeWidth(), strokeColor);
  10. }
  11. public int getStrokeWidth() {
  12. return mStrokeWidth;
  13. }
  14. public void setStrokeWidth(int strokeWidth) {
  15. mStrokeWidth = strokeWidth;
  16. mGradientDrawable.setStroke(strokeWidth, getStrokeColor());
  17. }
  18. public StrokeGradientDrawable() {
  19. mGradientDrawable = new GradientDrawable();
  20. mGradientDrawable.setColor(0xff99cc00);
  21. mStrokeWidth = 5;
  22. }
  23. public GradientDrawable getGradientDrawable() {
  24. return mGradientDrawable;
  25. }
  26. public void setmGradientDrawable(GradientDrawable mGradientDrawable) {
  27. this.mGradientDrawable = mGradientDrawable;
  28. }
  29. private GradientDrawable mGradientDrawable;
  30. }
其中的GradientDrawable 为动画的关键所在
在该类中,可以设置其圆角, 填充颜色, 描边, 尺寸等. 因此,当点击按钮时,可通过动画的方式渐变到目的状态, 其实现如下(源码见MorphingAnimation 类):

    
  1. @Override
  2. public void onClick(View v) {
  3. final GradientDrawable gradientDrawable = background.getGradientDrawable();
  4. final int mFromWidth = getWidth();
  5. final int mToWidth = getHeight();
  6. //宽度动画
  7. ValueAnimator widthAnimation = ValueAnimator.ofInt(mFromWidth, mToWidth);
  8. widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  9. @Override
  10. public void onAnimationUpdate(ValueAnimator animation) {
  11. Integer value = (Integer) animation.getAnimatedValue();
  12. int leftOffset;
  13. int rightOffset;
  14. int padding;
  15. if (mFromWidth > mToWidth) {
  16. leftOffset = (mFromWidth - value) / 2;
  17. rightOffset = mFromWidth - leftOffset;
  18. } else {
  19. leftOffset = (mToWidth - value) / 2;
  20. rightOffset = mToWidth - leftOffset;
  21. }
  22. gradientDrawable.setBounds(leftOffset, 0, rightOffset, getHeight());
  23. }
  24. });
  25. //填充颜色
  26. ObjectAnimator bgColorAnimation = ObjectAnimator.ofInt(gradientDrawable, "color", 0xff99cc00, Color.WHITE);
  27. bgColorAnimation.setEvaluator(new ArgbEvaluator());
  28. //描边
  29. ObjectAnimator strokeColorAnimation =
  30. ObjectAnimator.ofInt(background, "strokeColor", 0xff99cc00, Color.GRAY);
  31. strokeColorAnimation.setEvaluator(new ArgbEvaluator());
  32. //圆角
  33. ObjectAnimator cornerAnimation =
  34. ObjectAnimator.ofFloat(gradientDrawable, "cornerRadius", 0, 30);
  35. AnimatorSet animatorSet = new AnimatorSet();
  36. animatorSet.setDuration(2000);
  37. animatorSet.playTogether(bgColorAnimation, cornerAnimation, widthAnimation,strokeColorAnimation);
  38. animatorSet.start();
  39. }
1. 源码分析(此处以Sample2Activity 为实例):
按钮的几种状态, [ , IDLE,   ,]


初始为IDLE 状态.
在Sample2Activity 中, 仅仅只是调用了setProgress()来实现其整个过程.其value的值从0-100 不断的递增

在setProgress()方法中,其中的从初始化进入加载的圆环状态如下

 在morphToProgress() 方法,实现了从初始化到圆环状态的过度.在setListener(mProgressStateListener)中当动画完成的时候将mState 置为Progress

在createProgressMorphing() 中为创建一个MorphingAnimation实例, 其主要设置动画的圆角,宽度,颜色等等 ,在0中的简单实现也用到了这个类中的一些代码( MorphingAnimation.start() 方法中的代码片段) .

在动画完成后,在setProgress 中将进行不断的界面刷新invalidate() 在调用此方法,则系统将进行重绘调用onDraw()方法


当setProgress() 到达100的时候则会调用到morphProgressToComplete() 来转换到完成的状态.

---------------------------------------------------END---------------------------------------------------------------------------------------
StateListDrawable 类, 在xml 中,通过设置<selector/> 来的background来进行按钮点击时候的背景的切换,此类则是该xml 的实现.
ColorStateList  类似.





转载于:https://www.cnblogs.com/lv-2012/p/4212762.html

package com.example.androidtest.view; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.ColorStateList; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.StateListDrawable; import android.os.Build; import android.os.Parcel; import android.os.Parcelable; import android.util.AttributeSet; import android.util.StateSet; import android.widget.Button; import com.example.androidtest.R; public class CircularProgressButton extends Button { /** * 状态代号 * 0为初始状态,-1失败状态,100为完成状态,50为不明确中间状态 */ public static final int IDLE_STATE_PROGRESS = 0; public static final int ERROR_STATE_PROGRESS = -1; public static final int SUCCESS_STATE_PROGRESS = 100; public static final int INDETERMINATE_STATE_PROGRESS = 50; /** * 背景StrokeGradientDrawable * A Drawable with a color gradient for buttons, backgrounds, etc. * It can be defined in an XML file with the element. */ private StrokeGradientDrawable background; /** * 环形动画背景 */ private CircularAnimatedDrawable mAnimatedDrawable; /** * 环形进度背景 */ private CircularProgressDrawable mProgressDrawable; /** * ColorStateList对象可以在XML中定义,像color一样使用,它能根据它应用到的View对象的状态实时改变颜色 * 当每次状态改变时,StateList都会从上到下遍历一次,第一个匹配当前状态的item将被使用——选择的过程不是基于“最佳匹配”, * 只是符合state的最低标准的第一个item。 */ private ColorStateList mIdleColorState; /** * 完成状态ColorStateList */ private ColorStateList mCompleteColorState; /** * 失败状态ColorStateList */ private ColorStateList mErrorColorState; /** * 用于根据状态改变drawable * 初始状态背景 */ private StateListDrawable mIdleStateDrawable;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值