|
| 1 | +package com.github.nikartm.support; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Canvas; |
| 5 | +import android.graphics.drawable.Animatable; |
| 6 | +import android.support.v7.widget.AppCompatButton; |
| 7 | +import android.util.AttributeSet; |
| 8 | + |
| 9 | +import com.github.nikartm.support.constant.Constants; |
| 10 | + |
| 11 | +/** |
| 12 | + * Striped process button |
| 13 | + * @author Ivan V on 29.03.2018. |
| 14 | + * @version 1.0 |
| 15 | + */ |
| 16 | +public class StripedProcessButton extends AppCompatButton implements Animatable { |
| 17 | + |
| 18 | + private AnimatedStripedDrawable stripedDrawable; |
| 19 | + |
| 20 | + private long startAnimDuration = Constants.NO_INIT; |
| 21 | + private long stopAnimDuration = Constants.NO_INIT; |
| 22 | + private boolean buttonAnimation = Constants.DEF_BUTTON_ANIM; |
| 23 | + private String defaultText; |
| 24 | + private String loadingText; |
| 25 | + |
| 26 | + public StripedProcessButton(Context context) { |
| 27 | + super(context); |
| 28 | + initAttrs(null); |
| 29 | + } |
| 30 | + |
| 31 | + public StripedProcessButton(Context context, AttributeSet attrs) { |
| 32 | + super(context, attrs); |
| 33 | + initAttrs(attrs); |
| 34 | + } |
| 35 | + |
| 36 | + public StripedProcessButton(Context context, AttributeSet attrs, int defStyleAttr) { |
| 37 | + super(context, attrs, defStyleAttr); |
| 38 | + initAttrs(attrs); |
| 39 | + } |
| 40 | + |
| 41 | + private void initAttrs(AttributeSet attrs) { |
| 42 | + AttributeController attrController = new AttributeController(getContext(), attrs); |
| 43 | + stripedDrawable = attrController.getStripedDrawable(); |
| 44 | + loadingText = attrController.getLoadingText(); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + protected void onAttachedToWindow() { |
| 49 | + super.onAttachedToWindow(); |
| 50 | + defaultText = getText() != null ? getText().toString() : ""; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void onDraw(Canvas canvas) { |
| 55 | + setBackground(stripedDrawable); |
| 56 | + setEnabled(!isRunning()); |
| 57 | + super.onDraw(canvas); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void start() { |
| 62 | + if (isRunning()) { |
| 63 | + return; |
| 64 | + } |
| 65 | + setEnabled(isRunning()); |
| 66 | + stripedDrawable.start(); |
| 67 | + animateButton(isRunning()); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void stop() { |
| 72 | + if (!isRunning()) { |
| 73 | + return; |
| 74 | + } |
| 75 | + setEnabled(isRunning()); |
| 76 | + stripedDrawable.stop(); |
| 77 | + animateButton(isRunning()); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public boolean isRunning() { |
| 82 | + return stripedDrawable.isRunning(); |
| 83 | + } |
| 84 | + |
| 85 | + private void animateButton(boolean start) { |
| 86 | + if (isButtonAnimation()) { |
| 87 | + long duration; |
| 88 | + if (start) { |
| 89 | + duration = startAnimDuration == Constants.NO_INIT |
| 90 | + ? Constants.DEF_START_ANIM_DURATION |
| 91 | + : startAnimDuration; |
| 92 | + } else { |
| 93 | + duration = stopAnimDuration == Constants.NO_INIT |
| 94 | + ? Constants.DEF_STOP_ANIM_DURATION |
| 95 | + : stopAnimDuration; |
| 96 | + } |
| 97 | + setCurrentText(start); |
| 98 | + Util.Animation.animateView(this, start, duration); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void setCurrentText(boolean start) { |
| 103 | + String currentText; |
| 104 | + if (start) { |
| 105 | + currentText = loadingText == null ? defaultText : loadingText; |
| 106 | + } else { |
| 107 | + currentText = defaultText; |
| 108 | + } |
| 109 | + setText(currentText); |
| 110 | + } |
| 111 | + |
| 112 | + public AnimatedStripedDrawable adjustButton() { |
| 113 | + return stripedDrawable; |
| 114 | + } |
| 115 | + |
| 116 | + public long getStartAnimDuration() { |
| 117 | + return startAnimDuration; |
| 118 | + } |
| 119 | + |
| 120 | + public StripedProcessButton setStartAnimDuration(long startAnimDuration) { |
| 121 | + this.startAnimDuration = startAnimDuration; |
| 122 | + invalidate(); |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + public long getStopAnimDuration() { |
| 127 | + return stopAnimDuration; |
| 128 | + } |
| 129 | + |
| 130 | + public StripedProcessButton setStopAnimDuration(long stopAnimDuration) { |
| 131 | + this.stopAnimDuration = stopAnimDuration; |
| 132 | + invalidate(); |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + public String getLoadingText() { |
| 137 | + return loadingText; |
| 138 | + } |
| 139 | + |
| 140 | + public StripedProcessButton setLoadingText(String loadingText) { |
| 141 | + this.loadingText = loadingText; |
| 142 | + invalidate(); |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + public boolean isButtonAnimation() { |
| 147 | + return buttonAnimation; |
| 148 | + } |
| 149 | + |
| 150 | + public StripedProcessButton setButtonAnimation(boolean buttonAnimation) { |
| 151 | + this.buttonAnimation = buttonAnimation; |
| 152 | + invalidate(); |
| 153 | + return this; |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments