|
| 1 | +package net.avenwu.support.widget; |
| 2 | + |
| 3 | +import android.animation.AnimatorSet; |
| 4 | +import android.animation.ObjectAnimator; |
| 5 | +import android.animation.ValueAnimator; |
| 6 | +import android.content.Context; |
| 7 | +import android.content.res.TypedArray; |
| 8 | +import android.graphics.Canvas; |
| 9 | +import android.graphics.Paint; |
| 10 | +import android.graphics.RectF; |
| 11 | +import android.util.AttributeSet; |
| 12 | +import android.util.Log; |
| 13 | +import android.util.Property; |
| 14 | +import android.util.TypedValue; |
| 15 | +import android.view.View; |
| 16 | +import android.view.animation.LinearInterpolator; |
| 17 | + |
| 18 | +import net.avenwu.support.R; |
| 19 | + |
| 20 | +/** |
| 21 | + * Created by chaobin on 4/6/15. |
| 22 | + */ |
| 23 | +@SuppressWarnings("NewApi") |
| 24 | +public class BreathingDelegate { |
| 25 | + private Paint mPaint; |
| 26 | + private RectF mRippleRect = new RectF(); |
| 27 | + private float mRippleRadius; |
| 28 | + private float mEndRadius; |
| 29 | + private int mRippleAlpha = 0xff; |
| 30 | + private int mDuration; |
| 31 | + private RectF mBorderRect = new RectF(); |
| 32 | + private AnimatorSet mAnimatorSet = new AnimatorSet(); |
| 33 | + private boolean mAutoStart; |
| 34 | + private int mRippleColor; |
| 35 | + private View mTarget; |
| 36 | + |
| 37 | + public BreathingDelegate(Context context, AttributeSet attrs, View view) { |
| 38 | + mTarget = view; |
| 39 | + mTarget.setWillNotDraw(false); |
| 40 | + mTarget.setDrawingCacheEnabled(false); |
| 41 | + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BreathingLayout); |
| 42 | + mAutoStart = a.getBoolean(R.styleable.BreathingLayout_autoStart, true); |
| 43 | + mRippleColor = a.getColor(R.styleable.BreathingLayout_rippleColor, 0xFF0099CC); |
| 44 | + mRippleRadius = a.getDimensionPixelSize(R.styleable.BreathingLayout_rippleStartRadius, -1); |
| 45 | + if (mRippleRadius == -1) { |
| 46 | + mRippleRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getContext().getResources().getDisplayMetrics()); |
| 47 | + } |
| 48 | + mEndRadius = a.getDimensionPixelSize(R.styleable.BreathingLayout_rippleEndRadius, -1); |
| 49 | + if (mEndRadius == -1) { |
| 50 | + mEndRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getContext().getResources().getDisplayMetrics()); |
| 51 | + } |
| 52 | + if (mEndRadius <= mRippleRadius) { |
| 53 | + throw new IllegalStateException("rippleEndRadius can not be smaller than rippleStartRadius"); |
| 54 | + } |
| 55 | + mDuration = a.getInt(R.styleable.BreathingLayout_rippleTime, 3000); |
| 56 | + a.recycle(); |
| 57 | + mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 58 | + mPaint.setColor(mRippleColor); |
| 59 | + mPaint.setStyle(Paint.Style.FILL); |
| 60 | + prepareAnimation(); |
| 61 | + if (mAutoStart) { |
| 62 | + start(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + Property<BreathingDelegate, Float> mRadiusProperty = new Property<BreathingDelegate, Float>(Float.class, "mRippleRadius") { |
| 67 | + @Override |
| 68 | + public Float get(BreathingDelegate object) { |
| 69 | + return object.getRadius(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void set(BreathingDelegate object, Float value) { |
| 74 | + object.setRadius(value); |
| 75 | + } |
| 76 | + }; |
| 77 | + Property<BreathingDelegate, Integer> mAlphaProperty = new Property<BreathingDelegate, Integer>(Integer.class, "mRippleAlpha") { |
| 78 | + @Override |
| 79 | + public Integer get(BreathingDelegate object) { |
| 80 | + return object.getRippleAlpha(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void set(BreathingDelegate object, Integer value) { |
| 85 | + object.setRippleAlpha(value); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + private float getRadius() { |
| 90 | + return mRippleRadius; |
| 91 | + } |
| 92 | + |
| 93 | + private void setRadius(float radius) { |
| 94 | + this.mRippleRadius = radius; |
| 95 | + } |
| 96 | + |
| 97 | + public int getRippleAlpha() { |
| 98 | + return mRippleAlpha; |
| 99 | + } |
| 100 | + |
| 101 | + public void setRippleAlpha(int rippleAlpha) { |
| 102 | + this.mRippleAlpha = rippleAlpha; |
| 103 | + } |
| 104 | + |
| 105 | + private void prepareAnimation() { |
| 106 | + ObjectAnimator animator = ObjectAnimator.ofFloat(this, mRadiusProperty, mRippleRadius, mEndRadius); |
| 107 | + animator.setDuration(mDuration); |
| 108 | + animator.setRepeatCount(ValueAnimator.INFINITE); |
| 109 | + animator.setRepeatMode(ValueAnimator.REVERSE); |
| 110 | + animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 111 | + @Override |
| 112 | + public void onAnimationUpdate(ValueAnimator animation) { |
| 113 | + mTarget.invalidate(); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + ObjectAnimator alpha = ObjectAnimator.ofInt(this, mAlphaProperty, mRippleAlpha, 0x00); |
| 118 | + alpha.setDuration(mDuration); |
| 119 | + alpha.setRepeatCount(ValueAnimator.INFINITE); |
| 120 | + alpha.setRepeatMode(ValueAnimator.RESTART); |
| 121 | + mAnimatorSet.setInterpolator(new LinearInterpolator()); |
| 122 | + mAnimatorSet.playTogether(animator, alpha); |
| 123 | + } |
| 124 | + |
| 125 | + public void toggle() { |
| 126 | + if (mAnimatorSet.isRunning()) { |
| 127 | + stop(); |
| 128 | + }/* else if (mAnimatorSet.isPaused()) { |
| 129 | + mAnimatorSet.resume(); |
| 130 | + } */ else { |
| 131 | + mAnimatorSet.start(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public void start() { |
| 136 | + mAnimatorSet.start(); |
| 137 | + } |
| 138 | + |
| 139 | + public void stop() { |
| 140 | + mAnimatorSet.cancel(); |
| 141 | + } |
| 142 | + |
| 143 | + private Context getContext() { |
| 144 | + return mTarget.getContext(); |
| 145 | + } |
| 146 | + |
| 147 | + public void onDraw(Canvas canvas) { |
| 148 | + mRippleRect.set(mBorderRect.centerX() - mRippleRadius, mBorderRect.centerY() - mRippleRadius, |
| 149 | + mBorderRect.centerX() + mRippleRadius, mBorderRect.centerY() + mRippleRadius); |
| 150 | + canvas.drawOval(mRippleRect, mPaint); |
| 151 | + Log.d("BreathingLayout", "onDraw=" + mRippleRect.toString()); |
| 152 | + } |
| 153 | + |
| 154 | + public void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 155 | + mBorderRect.set(left, top, right, bottom); |
| 156 | + Log.d("BreathingLayout", "onLayout:" + mBorderRect.toString()); |
| 157 | + } |
| 158 | + |
| 159 | + public void onDetachedFromWindow() { |
| 160 | + mAnimatorSet.cancel(); |
| 161 | + Log.d("BreathingLayout", "onDetachedFromWindow"); |
| 162 | + } |
| 163 | +} |
0 commit comments