11package co .lujun .androidtagview ;
22
3+ import android .animation .ValueAnimator ;
4+ import android .annotation .TargetApi ;
35import android .content .Context ;
46import android .graphics .Canvas ;
57import android .graphics .Paint ;
8+ import android .graphics .Path ;
69import android .graphics .RectF ;
10+ import android .graphics .Region ;
711import android .graphics .Typeface ;
12+ import android .os .Build ;
813import android .support .v4 .widget .ViewDragHelper ;
914import android .text .TextUtils ;
1015import android .view .MotionEvent ;
@@ -64,7 +69,7 @@ public class TagView extends View {
6469 /** The distance between baseline and descent*/
6570 private float bdDistance ;
6671
67- private Paint mPaint ;
72+ private Paint mPaint , mRipplePaint ;
6873
6974 private RectF mRectF ;
7075
@@ -75,9 +80,24 @@ public class TagView extends View {
7580 private int mLastX , mLastY ;
7681
7782 private float fontH , fontW ;
83+
84+ private float mTouchX , mTouchY ;
85+
86+ /** The ripple effect duration(default 1000ms)*/
87+ private int mRippleDuration = 1000 ;
88+
89+ private float mRippleRadius ;
90+
91+ private int mRippleColor ;
92+
93+ private int mRippleAlpha ;
94+
95+ private Path mPath ;
7896
7997 private Typeface mTypeface ;
8098
99+ private ValueAnimator mRippleValueAnimator ;
100+
81101 private Runnable mLongClickHandle = new Runnable () {
82102 @ Override
83103 public void run () {
@@ -98,7 +118,10 @@ public TagView(Context context, String text){
98118
99119 private void init (String text ){
100120 mPaint = new Paint (Paint .ANTI_ALIAS_FLAG );
121+ mRipplePaint = new Paint (Paint .ANTI_ALIAS_FLAG );
122+ mRipplePaint .setStyle (Paint .Style .FILL );
101123 mRectF = new RectF ();
124+ mPath = new Path ();
102125 mOriginText = text == null ? "" : text ;
103126 }
104127
@@ -162,6 +185,9 @@ protected void onDraw(Canvas canvas) {
162185 canvas .drawText (mAbstractText , getWidth () / 2 - fontW / 2 ,
163186 getHeight () / 2 + fontH / 2 - bdDistance , mPaint );
164187 }
188+
189+ // draw ripple for TagView
190+ drawRipple (canvas );
165191 }
166192
167193 @ Override
@@ -192,6 +218,13 @@ public boolean dispatchTouchEvent(MotionEvent event) {
192218
193219 @ Override
194220 public boolean onTouchEvent (MotionEvent event ) {
221+ if (event .getAction () == MotionEvent .ACTION_DOWN ) {
222+ mRippleRadius = 0.0f ;
223+ mTouchX = event .getX ();
224+ mTouchY = event .getY ();
225+ splashRipple ();
226+ }
227+
195228 if (isViewClickable && mOnTagClickListener != null ){
196229 int x = (int ) event .getX ();
197230 int y = (int ) event .getY ();
@@ -227,6 +260,42 @@ public boolean onTouchEvent(MotionEvent event) {
227260 return super .onTouchEvent (event );
228261 }
229262
263+ @ TargetApi (Build .VERSION_CODES .HONEYCOMB )
264+ private void drawRipple (Canvas canvas ){
265+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .HONEYCOMB && canvas != null ){
266+ canvas .save ();
267+ mPath .reset ();
268+
269+ canvas .clipPath (mPath );
270+ mPath .addRoundRect (mRectF , mBorderRadius , mBorderRadius , Path .Direction .CCW );
271+
272+ canvas .clipPath (mPath , Region .Op .REPLACE );
273+ canvas .drawCircle (mTouchX , mTouchY , mRippleRadius , mRipplePaint );
274+ canvas .restore ();
275+ }
276+ }
277+
278+ @ TargetApi (Build .VERSION_CODES .HONEYCOMB )
279+ private void splashRipple (){
280+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .HONEYCOMB && mTouchX > 0 && mTouchY > 0 ){
281+ mRipplePaint .setColor (mRippleColor );
282+ mRipplePaint .setAlpha (mRippleAlpha );
283+ final float maxDis = Math .max (Math .max (Math .max (mTouchX , mTouchY ),
284+ Math .abs (getMeasuredWidth () - mTouchX )), Math .abs (getMeasuredHeight () - mTouchY ));
285+
286+ mRippleValueAnimator = ValueAnimator .ofFloat (0.0f , maxDis ).setDuration (mRippleDuration );
287+ mRippleValueAnimator .addUpdateListener (new ValueAnimator .AnimatorUpdateListener () {
288+ @ Override
289+ public void onAnimationUpdate (ValueAnimator animation ) {
290+ float animValue = (float ) animation .getAnimatedValue ();
291+ mRippleRadius = animValue >= maxDis ? 0 : animValue ;
292+ postInvalidate ();
293+ }
294+ });
295+ mRippleValueAnimator .start ();
296+ }
297+ }
298+
230299 public String getText (){
231300 return mOriginText ;
232301 }
@@ -299,6 +368,18 @@ public void setTypeface(Typeface typeface) {
299368 onDealText ();
300369 }
301370
371+ public void setRippleAlpha (int mRippleAlpha ) {
372+ this .mRippleAlpha = mRippleAlpha ;
373+ }
374+
375+ public void setRippleColor (int mRippleColor ) {
376+ this .mRippleColor = mRippleColor ;
377+ }
378+
379+ public void setRippleDuration (int mRippleDuration ) {
380+ this .mRippleDuration = mRippleDuration ;
381+ }
382+
302383 public void setBdDistance (float bdDistance ) {
303384 this .bdDistance = bdDistance ;
304385 }
0 commit comments