Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize ScrollView to realize damping springback by Android

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how to customize Android ScrollView to achieve damping springback related knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

The solution is as follows:

ScrollView is required to have one and only one child View inside. When the ScrollView slides to the boundary, let the sub-View translate with the fingers according to certain rules in the ScrollView to simulate the stretching effect. When the finger is released, the sub-View is restored to its pre-stretch position to simulate the springback effect.

The complete code is as follows. For detailed principles, please see the notes.

Public class StretchScrollView extends NestedScrollView {/ / subView private View innerView; / / the y coordinate of the last gesture event private float mLastY; / / record the normal position of the subView private Rect normal = new Rect (); public StretchScrollView (Context context, AttributeSet attrs) {super (context, attrs);} @ Override protected void onFinishInflate () {initView (); super.onFinishInflate () } / * get the sublayout of ScrollView * / private void initView () {/ / remove the shadow effect setOverScrollMode (OVER_SCROLL_NEVER) when the original ScrollView scrolls to the boundary; if (getChildAt (0)! = null) {innerView = getChildAt (0) } @ Override public boolean onTouchEvent (MotionEvent ev) {switch (ev.getAction ()) {case MotionEvent.ACTION_UP: / / release the finger to recover if (! normal.isEmpty ()) {planAnimation (); normal.setEmpty (); mLastY = 0 } break; case MotionEvent.ACTION_MOVE: float currentY = ev.getY (); / / sliding distance int distanceY = (int) (mLastY-currentY) / / handle the scrolling event of the Y axis. When you scroll to the top or bottom, you need to move the layout / / this event will also be triggered when the finger touches the screen. When the value of mLastY is still 0, a large movement will be triggered immediately. Filter out this situation here if (isNeedTranslate () & & mLastY! = 0) {if (normal.isEmpty ()) {/ / save the normal layout location normal.set (innerView.getLeft (), innerView.getTop (), innerView.getRight (), innerView.getBottom () } / / move the layout to prevent distance / 2 from translating too fast innerView.layout (innerView.getLeft (), innerView.getTop ()-distanceY / 2, innerView.getRight (), innerView.getBottom ()-distanceY / 2);} mLastY = currentY Break;} return super.onTouchEvent (ev);} / * * retractive animation * / public void planAnimation () {/ / enable mobile animation TranslateAnimation animation = new TranslateAnimation (0,0, innerView.getTop (), normal.top); animation.setDuration (200C); innerView.startAnimation (animation) / / the motion tween does not really modify the position of innerView. Here you need to set the innerView to return to its normal layout position innerView.layout (normal.left, normal.top, normal.right, normal.bottom);} / * whether Y mobile layout is required * / public boolean isNeedTranslate () {int offset = innerView.getMeasuredHeight ()-getHeight (); int scrollY = getScrollY () / / Top or bottom return scrollY = = 0 | | scrollY = = offset;}} above is all the content of the article "how to customize ScrollView to achieve damping springback by Android". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report