Android Custom controls: Android L control tap water ripple implementation (source + Demo)

Source: Internet
Author: User

The realization idea comes from Singwhatiwanna

http://blog.csdn.net/singwhatiwanna/article/details/42614953


Demo:



First, the control process:

In general, there are some deviations that you can draw yourself.

Reveallayout ()--->init ()--->onmeasure ()--->onlayout ()--->ondraw ()--->dispatchtouchevent ()--- >gettargetview ()--->istouchpointinview ()--->-initparametersforchild ()-->dispatchdraw ()


Second, design ideas:

1, why choose to use LinearLayout

One reason to do this from layout is to reduce code reuse, and we know that if you rewrite only controls like Button,textview, a lot of the code is actually similar (almost the same) so that we're constantly writing the same thing and wasting time, so for the principle of reusability, Let's rewrite a layout.


From the design of Dispatchdraw () we can know, we realize the effect of water ripple in this, is to constantly refresh the control, increase the radius of the circle drawn by the canvas, realize the effect of increasing the visual circle.


If we choose other layout, it may be more complex to draw (Relativelayout), on the other hand it may be low applicability (absolutelayout). and linearlayout actually can and relative and so on layout combination use, realize complex layout, so synthesize to consider we choose LinearLayout to realize.


2, why is Dispatchtouchevent ()

In Android, when touchevent occurs, the activity first passes the touchevent to the topmost view, TouchEvent first to the dispatchtouchevent of the topmost view, and then by the The Dispatchtouchevent method is distributed, and if Dispatchtouchevent returns True, the ontouchevent processing of the view is given, and if Dispatchtouchevent returns false, the View's Intercepttouchevent method to decide whether or not to intercept this event, if Intercepttouchevent returns True, that is, intercept it, then give it the ontouchevent to handle, if Intercepttouchevent returns FALSE, it is passed to the child view, and the dispatchtouchevent of the sub view begins the distribution of the event. If the event is passed to the ontouchevent of a child view of a layer, this method returns false, and the event is passed from this view to the ontouchevent to receive. If the ontouchevent passed to the top also returns false, the event "disappears" and the next event is not received.


3. Why use GETRAWX and Getrawy to obtain coordinates in dispatchtouchevent ()

Getx and gety get the coordinates relative to the clicked view, and Getrawx gets the coordinates relative to the screen. And we want to see the ripple effect of water, should be from the screen relative to the point is clicked to diffuse the waves.


4, Initparametersforchild () in the Mmaxradius is what, Mtransformedcenterx is what

I think it would be very understood to explain it in a picture.


5, Dispatchdraw () is how to draw the water ripple

Based on the previous basis, we are constantly refreshing the page in Dispatchdraw (), refreshing and drawing a new circle in the canvas before Mradius reaches Mmaxradius. The delay action is to prevent stack overflow and UI thread blocking.


Third, the source code download

Click here!

Android Custom controls: Android L control tap water ripple implementation (source + Demo)

Related Article

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.