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 realize horizontal sliding springback with android ScrollView

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

Share

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

This article mainly introduces the relevant knowledge of "how to achieve horizontal sliding springback in android ScrollView". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to achieve horizontal sliding springback in android ScrollView" can help you solve the problem.

Effect picture:

Main code:

Import android.content.Context;import android.graphics.Rect;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.animation.TranslateAnimation;import android.widget.HorizontalScrollView; / * ScrollView horizontal slide springback * * @ author qhg * @ date March 12, 2014 * * / public class MHorizontalScrollView extends HorizontalScrollView {private View view; / * increment when moving * / private static final int deltaX = 1 Private Rect normalRt = new Rect (); public MHorizontalScrollView (Context context) {super (context);} public MHorizontalScrollView (Context context, AttributeSet attrs) {super (context, attrs);} / * * execute this method after the xml layout is executed * / protected void onFinishInflate () {if (getChildCount () > 0) {view = getChildAt (0) } @ Override public boolean onTouchEvent (MotionEvent event) {if (view! = null) {onTouchEventImpl (event);} return super.onTouchEvent (event) } private void onTouchEventImpl (MotionEvent event) {switch (event.getAction ()) {case MotionEvent.ACTION_MOVE: / / the content of the current view continues to be offset by (xmemy) units, and the display (visual) area is also offset by (xmemy) units scrollBy (deltaX, 0) / / when scrolling to the far left or right, it will not scroll again. When moving the layout to achieve a rebound effect, if (isLayoutMove ()) {if (normalRt.isEmpty ()) {/ / saves the current normal layout position. Pull too much to bounce back to the normal position normalRt.set (view.getLeft (), view.getTop (), view.getRight (), view.getBottom ()) } / / Mobile layout view.layout (view.getLeft ()-deltaX, view.getTop (), view.getRight ()-deltaX, view.getBottom ());} break; case MotionEvent.ACTION_UP: if (isNeedAnimation ()) {animationImpl () } break; default: break;}} / * * Animated Mobile * / private void animationImpl () {/ / Mobile Animated TranslateAnimation ta = new TranslateAnimation (view.getLeft (), normalRt.left, 0,0) / / Animation duration ta.setDuration (50); view.startAnimation (ta); / / return to the current normal layout position view.layout (normalRt.left, normalRt.top, normalRt.right, normalRt.bottom); normalRt.setEmpty () } / * * whether you need to enable animation * * @ return * / private boolean isNeedAnimation () {return! normalRt.isEmpty ();} / * * whether you need to move the layout * * @ return * / private boolean isLayoutMove () {int offset = view.getMeasuredWidth ()-getWidth (); if (offset

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