Android Ripple button Water ripple effect (i)

Source: Internet
Author: User

See Android 5.0 has a button click effect Very good, first look:

But this effect can only be achieved on 5.0 of the system, how to achieve in the lower version?
This effect has also been realized on the Internet,
Blog Address http://blog.csdn.net/singwhatiwanna/article/details/42614953

OK, go directly to the topic,
To achieve this kind of animation effect is not difficult, the principle can be summed up in one sentence: that is, when we press the view, from the position of the press start to draw the circle, the radius of the circle has been increased until the view is all covered out.

Through the implementation of the principle we can analyze, to achieve this effect, we need to rewrite the OnDraw method, Ontouchevent method, if necessary also need to onmeasure method to get the size of the view

1, OnDraw method, is to constantly draw the circle, here need to loop call,
2, Ontouchevent method: Through Motionevent.action_down and motionevent.action_up to do the corresponding operation and trigger the OnDraw method
3. Onmeasure calculates the size of the view, calculates the maximum radius of the circle by size plus Motionevent.action_down start point

Code implementation:
Saves the size of the view to calculate the maximum radius of the drawn circle

@OverrideprotectedvoidonMeasure(intint heightMeasureSpec) {   super.onMeasure(widthMeasureSpec, heightMeasureSpec);   mRect.set(00, getMeasuredWidth(), getMeasuredHeight());}

When the finger presses the view, calculates the position of the pressed point and triggers the OnDraw method

 PublicBooleanontouchevent(motioneventEvent) {FinalintAction = motioneventcompat.getactionmasked (Event);Switch(action) { CaseMotionevent.action_down: {mdrawfinish =false;intindex = Motioneventcompat.getactionindex (Event);intEventId = Motioneventcompat.getpointerid (Event, index);if(EventId! =-1) {MINITX = (int) Motioneventcompat.getx (Event, index); Minity = (int) Motioneventcompat.gety (Event, index);                    Updatedrawdata (); Viewcompat.postinvalidateonanimation ( This); }return true; }

Update the data to recalculate the radius and stride size of the circle

private void Updatedrawdata () {int radiuslefttop = (int) Math. sqrt((Mrect. Left-MINITX) * (mrect. Left-MINITX) + (mrect. Top-minity) * (mrect. Top-minity));int radiusrighttop = (int) Math. sqrt((Mrect. Right-MINITX) * (mrect. Right-MINITX) + (mrect. Top-minity) * (mrect. Top-minity));int radiusleftbottom = (int) Math. sqrt((Mrect. Left-MINITX) * (mrect. Left-MINITX) + (mrect. Bottom-minity) * (mrect. Bottom-minity));int radiusrightbottom = (int) Math. sqrt((Mrect. Right-MINITX) * (mrect. Right-MINITX) + (mrect. Bottom-minity) * (mrect. Bottom-minity));Mradius = Getmax (Radiuslefttop, Radiusrighttop, Radiusleftbottom, Radiusrightbottom);Mstep = mradius/mcycle;}

You need to do when your finger is lifted, increase the step by 2.5 times times, and speed up the drawing of the circle.

  @Override  public  boolean  ontouchevent  (Motionevent event) {final  int  action = Motioneventcompat.getactio        Nmasked (event); switch  (action)            {//:  case   Motionevent.action_cancel: case  MotionEvent.ACTION_UP:mStep = (int )                (2.5  f * mstep);                Mdrawback = true ;        break ;    } return  super . Ontouchevent (event); }

The final is to draw the circle, here to make a judgment, if the current drawn circle radius does not reach the maximum radius of our calculation, we need to continue to draw

@Override    protected void OnDraw(Canvas canvas) {if(Mdrawfinish) {Super. OnDraw (canvas);return; } canvas.drawcolor (0x08000000);Super. OnDraw (canvas);if(Mstep = =0) {return; } Mdrawradius = Mdrawradius + mstep;if(Mdrawradius > Mradius) {Mdrawradius =0;            Canvas.drawcircle (MINITX, minity, Mradius, Mrevealpaint); Mdrawfinish =true; Viewcompat.postinvalidateonanimation ( This);return;        } canvas.drawcircle (Minitx, minity, Mdrawradius, Mrevealpaint); Viewcompat.postinvalidateonanimation ( This); }

The whole effect of the implementation, relatively simple, just very boring has been painted round.
Finally, if you want to do this effect on TextView, then inherit TextView can, linearlayout and other components of the same;

Demo download

Android Ripple button Water ripple effect (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.