Android custom control: Android L control click the implementation of water ripple (source code + Demo), androiddemo
Implementation idea comes fromSingwhatiwanna
Http://blog.csdn.net/singwhatiwanna/article/details/42614953
Demo:
I. Control Process:
The general principle is as follows: You can draw pictures by yourself if there are some deviations.
RevealLayout () ---> init () ---> onMeasure () ---> onLayout () ---> onDraw () ---> dispatchTouchEvent () ---> getTargetView () ---> isTouchPointInView () --->-initParametersForChild () --> dispatchDraw ()
Ii. design ideas:
1. Why LinearLayout?
One of the major reasons for implementing this effect from Layout is to reduce code reuse. We know that if we only rewrite controls such as Button and TextView, a lot of code is actually similar (almost the same) in this way, we are constantly writing the same thing, which wastes time. Therefore, in order to reuse the principle, we rewrite a Layout.
From the design in dispatchDraw (), we can know that the water ripple effect is to constantly refresh the control, increase the radius of the circle drawn by the canvas, and increase the visual circle.
If we use another layout, on the one hand, it may be that the drawn content is more complex (RelativeLayout), and on the other hand it may be AbsoluteLayout ). LinearLayout can be used in combination with Relative and other layout to implement complicated layout. Therefore, we should choose LinearLayout to implement it comprehensively.
2. Why is dispatchTouchEvent ()
In Android, when a TouchEvent occurs, the Activity first passes the TouchEvent to the top-level View. The TouchEvent first reaches the dispatchTouchEvent of the top-level view, and then is distributed by the dispatchTouchEvent method, if the dispatchTouchEvent returns true, it is handed over to the onTouchEvent of the view for processing. If the dispatchTouchEvent returns false, it is handed over to the interceptTouchEvent method of the view to determine whether to interceptTouchEvent is required, if interceptTouchEvent returns false, it is passed to the sub-view, which is the dispatchTouchEven of the sub-view. T to start distributing the event. If the event is passed to the onTouchEvent of the subview of a certain layer, the method returns false, and the event will be passed up from this view, which is received by onTouchEvent. If false is returned when the onTouchEvent is passed to the top, the event will disappear and the next event will not be received.
3. Why use getRawX and getRawY to obtain coordinates in dispatchTouchEvent ()?
GetX and getY obtain the coordinates relative to the clicked View, while getRawX obtains the coordinates relative to the screen. The ripple effect we want to see is to scatter water from the point clicked in the screen.
4. What is mMaxRadius in initParametersForChild () and mTransformedCenterX?
I think it is very understandable to use a picture.
5. How does dispatchDraw () draw water Ripple?
Based on the previous basics, we constantly refresh the page in dispatchDraw (). We refresh the page and draw new circles in the canvas before mRadius reaches the mMaxRadius. Delayed operations prevent Stack Overflow and UI thread blocking.
3. Download source code
Click Here!