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 use ScrollView to realize the springback effect of horizontal and Vertical drag in Android

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use ScrollView in Android to achieve horizontal and vertical drag springback effect", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "how to use ScrollView in Android to achieve horizontal and vertical drag springback effect" bar!

Principle

In the android2.3 version, a new method has been added to the View class: overScrollBy. By covering this method, the effect of damping springback can be achieved.

Example 1. Vertical scrolling

Public class ReboundScrollView extends ScrollView {private static final int MAX_SCROLL = 200; private static final float SCROLL_RATIO = 0.5f impact / damping coefficient public ReboundScrollView (Context context) {super (context);} public ReboundScrollView (Context context, AttributeSet attrs) {super (context, attrs);} public ReboundScrollView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle) } @ Override protected boolean overScrollBy (int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {int newDeltaY = deltaY; int delta = (int) (deltaY * SCROLL_RATIO); if ((scrollY+deltaY) = = 0 | | (scrollY-scrollRangeY+deltaY) = 0) {newDeltaY = deltaY; / / rebound last scroll, reset} else {newDeltaY = delta / / add damping effect} return super.overScrollBy (deltaX, newDeltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, MAX_SCROLL, isTouchEvent);}}

Example 2. Scroll horizontally

Public class ReboundHScrollView extends HorizontalScrollView {private static final int MAX_SCROLL = 200; private static final float SCROLL_RATIO = 0.5f impact / damping coefficient public ReboundHScrollView (Context context) {super (context);} public ReboundHScrollView (Context context, AttributeSet attrs) {super (context, attrs);} public ReboundHScrollView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle) } @ Override protected boolean overScrollBy (int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {int newDeltaX = deltaX; int delta = (int) (deltaX * SCROLL_RATIO); if ((scrollX+deltaX) = = 0 | | (scrollX-scrollRangeX+deltaX) = 0) {newDeltaX = deltaX; / / rebound last scroll, reset} else {newDeltaX = delta / / add damping effect} return super.overScrollBy (newDeltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, MAX_SCROLL, maxOverScrollY, isTouchEvent) }} Thank you for your reading, the above is the content of "how to use ScrollView to achieve horizontal and vertical drag springback effect in Android". After the study of this article, I believe you have a deeper understanding of how to use ScrollView to achieve horizontal and vertical drag springback effect in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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