Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use ScrollView to realize the springback effect of horizontal and Vertical drag in Android

Shulou Source: shulou.com Published: 2022-05-31 21:22:32 09月18日 Update

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!

Tags: Effects horizontal damping learning content methods examples coefficients principles that is ideas situations articles more versions knowledge knowledge points articles follow questions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi Shulou Information MariaDB macOS