kevin2458

RippleView.java

Jan 30th, 2018
156
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.wavemeasure.wavemeasure.ui.widget;
  2.  
  3. import android.animation.Animator;
  4. import android.animation.ValueAnimator;
  5. import android.content.Context;
  6. import android.graphics.Canvas;
  7. import android.graphics.Paint;
  8. import android.support.annotation.ColorInt;
  9. import android.util.AttributeSet;
  10. import android.view.View;
  11. import android.view.animation.AccelerateDecelerateInterpolator;
  12.  
  13. import com.wavemeasure.wavemeasure.R;
  14. import com.wavemeasure.wavemeasure.ui.util.ResourcesUtils;
  15.  
  16. public class RippleView extends View
  17.         implements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {
  18.  
  19.     private static final int RADIUS = 48;
  20.  
  21.  
  22.     private Paint mInPaint = new Paint();
  23.  
  24.     private Paint mInStrokePaint = new Paint();
  25.  
  26.     private Paint mOutStrokePaint = new Paint();
  27.  
  28.  
  29.     private RippleStateListener mRippleStateListener;
  30.  
  31.     private ValueAnimator mValueAnimator;
  32.  
  33.  
  34.     private float mCx;
  35.  
  36.     private float mCy;
  37.  
  38.  
  39.     private int mChangeRadius;
  40.  
  41.     private int mRadius;
  42.  
  43.  
  44.     private int mDuration = 3000;
  45.  
  46.     private int mRepeatCount = 1;
  47.  
  48.     private int mStrokeWidth = 1;
  49.  
  50.  
  51.     public RippleView(Context context) {
  52.         super(context);
  53.  
  54.         initComponents();
  55.     }
  56.  
  57.     public RippleView(Context context, AttributeSet attrs) {
  58.         super(context, attrs);
  59.  
  60.         initComponents();
  61.     }
  62.  
  63.     public RippleView(Context context, AttributeSet attrs, int defStyleAttr) {
  64.         super(context, attrs, defStyleAttr);
  65.  
  66.         initComponents();
  67.     }
  68.  
  69.  
  70.     @Override
  71.     public void onAnimationCancel(Animator animation) {
  72.         mChangeRadius = mRadius;
  73.     }
  74.  
  75.     @Override
  76.     public void onAnimationEnd(Animator animation) {
  77.         if (mRippleStateListener != null) {
  78.             mRippleStateListener.stopRipple();
  79.         }
  80.  
  81.         mChangeRadius = mRadius;
  82.     }
  83.  
  84.     @Override
  85.     public void onAnimationRepeat(Animator animation) {
  86.  
  87.     }
  88.  
  89.     @Override
  90.     public void onAnimationStart(Animator animation) {
  91.         if (mRippleStateListener != null) {
  92.             mRippleStateListener.startRipple();
  93.         }
  94.     }
  95.  
  96.     @Override
  97.     public void onAnimationUpdate(ValueAnimator animation) {
  98.         mChangeRadius = (int) animation.getAnimatedValue();
  99.  
  100.         postInvalidate();
  101.  
  102.         if (mRippleStateListener != null) {
  103.             mRippleStateListener.onRippleUpdate(animation);
  104.         }
  105.     }
  106.  
  107.  
  108.     @Override
  109.     protected void onDraw(Canvas canvas) {
  110.         super.onDraw(canvas);
  111.  
  112.         canvas.drawCircle(mCx, mCy, mChangeRadius, mOutStrokePaint);
  113.  
  114.         if (mChangeRadius >= mRadius * 1.5) {
  115.             canvas.drawCircle(mCx, mCy, mChangeRadius - (mRadius / 2), mOutStrokePaint);
  116.         }
  117.  
  118.         if (mChangeRadius >= mRadius * 2) {
  119.             canvas.drawCircle(mCx, mCy, mChangeRadius - (mRadius), mOutStrokePaint);
  120.         }
  121.  
  122.         // canvas.drawCircle(mCx, mCy, mRadius, mInPaint);
  123.  
  124.         // canvas.drawCircle(mCx, mCy, mRadius, mInStrokePaint);
  125.     }
  126.  
  127.     @Override
  128.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  129.         super.onSizeChanged(w, h, oldw, oldh);
  130.  
  131.         mCx = getMeasuredWidth() / 2;
  132.  
  133.         mCy = getMeasuredHeight() / 2;
  134.     }
  135.  
  136.  
  137.     public void startRipple() {
  138.         if (mValueAnimator == null) {
  139.             mValueAnimator = new ValueAnimator();
  140.  
  141.             mValueAnimator.setDuration(mDuration);
  142.  
  143.             mValueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
  144.  
  145.             mValueAnimator.setIntValues(mRadius, (int) (mCx > mCy ? mCx * 1.4 : mCy * 1.4));
  146.  
  147.             mValueAnimator.setRepeatCount(mRepeatCount);
  148.  
  149.             mValueAnimator.addListener(this);
  150.  
  151.             mValueAnimator.addUpdateListener(this);
  152.  
  153.             mValueAnimator.start();
  154.         } else {
  155.             if (!mValueAnimator.isRunning()) {
  156.                 mValueAnimator.start();
  157.             }
  158.         }
  159.     }
  160.  
  161.     public void stopRipple() {
  162.         if (mValueAnimator != null) {
  163.             mValueAnimator.end();
  164.         }
  165.     }
  166.  
  167.  
  168.     public void setCirclePoint(float x, float y) {
  169.         mCx = x;
  170.  
  171.         mCy = y;
  172.     }
  173.  
  174.  
  175.     public int getDuration() {
  176.         return mDuration;
  177.     }
  178.  
  179.     public void setDuration(int duration) {
  180.         mDuration = duration;
  181.     }
  182.  
  183.  
  184.     public void setInPaintColor(int color) {
  185.         mInPaint.setColor(color);
  186.     }
  187.  
  188.     public void setInStrokePaintColor(int color) {
  189.         mInStrokePaint.setColor(color);
  190.     }
  191.  
  192.     public void setOutStrokePaintColor(int color) {
  193.         mOutStrokePaint.setColor(color);
  194.     }
  195.  
  196.     public void setRadius(int radius) {
  197.         mRadius = radius;
  198.  
  199.         mChangeRadius = radius;
  200.     }
  201.  
  202.     public void setRepeatCount(int repeatCount) {
  203.         mRepeatCount = repeatCount;
  204.     }
  205.  
  206.     public void setRippleStateListener(RippleStateListener listener) {
  207.         mRippleStateListener = listener;
  208.     }
  209.  
  210.     public void setStrokeWidth(int strokeWidth) {
  211.         mStrokeWidth = strokeWidth;
  212.     }
  213.  
  214.  
  215.     private void initComponents() {
  216.         @ColorInt
  217.         final int color = ResourcesUtils.getColor(getContext(), R.color.colorYellow);
  218.  
  219.  
  220.         mRadius = dp2px(RADIUS);
  221.  
  222.         mChangeRadius = mRadius;
  223.  
  224.  
  225.         mInPaint.setColor(color);
  226.  
  227.         mInPaint.setAntiAlias(true);
  228.  
  229.         mInPaint.setStyle(Paint.Style.FILL);
  230.  
  231.  
  232.         mInStrokePaint.setColor(color);
  233.  
  234.         mInStrokePaint.setAntiAlias(true);
  235.  
  236.         mInStrokePaint.setStyle(Paint.Style.STROKE);
  237.  
  238.         mInStrokePaint.setStrokeWidth(dp2px(mStrokeWidth));
  239.  
  240.  
  241.         mOutStrokePaint.setColor(color);
  242.  
  243.         mOutStrokePaint.setAntiAlias(true);
  244.  
  245.         mOutStrokePaint.setStyle(Paint.Style.STROKE);
  246.  
  247.         mOutStrokePaint.setStrokeWidth(dp2px(mStrokeWidth));
  248.     }
  249.  
  250.     private int dp2px(float dpValue) {
  251.         float scale = getContext().getResources().getDisplayMetrics().density;
  252.  
  253.         return (int) (dpValue * scale + 0.5f);
  254.     }
  255.  
  256.  
  257.     public interface RippleStateListener {
  258.  
  259.         void startRipple();
  260.  
  261.         void stopRipple();
  262.  
  263.         void onRippleUpdate(ValueAnimator animation);
  264.  
  265.     }
  266.  
  267. }
RAW Paste Data