Android Reboundscrollview Imitation iOS drag rebound effect _android

Source: Internet
Author: User
Tags gety

Original intention:

In fact, there are many such scrollview items on the GitHub, but have to say that the function too much too messy, I just want a simple effect of the scrollview, another monitoring under the sliding distance just, think about it or write a.

Here is the idea, if you do not want to see a friend can skip this step, look at the following code:

Android native ScrollView is not supported pull out of the screen, and there is no rebound effect, user-friendly but not too good, do not know why not so design.
What I want to do is as stated above:

1. Hope to pull out of the screen
2. Want the control to bounce after letting go

My idea is to operate on ScrollView's child view.

All view sliding control must be controlled by the ontouchevent, so, to be sure, I would like to focus on, that is, ontouchevent this method.

The rebound effect, which involves the calculation of the position, here my idea is to use a simple translateanimation to achieve.

The general idea is this, first to determine the idea, and then the code is always grinding out.

Paste 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.ScrollView; public class Reboundscrollview extends ScrollView {private static final float Move_delay = 0.3f;//drag factor when pulling out screen privat
  e static final int anim_time = 300;//time consuming private static final int fling = 2;//fling coefficient private View childview;

  Private Boolean havamoved;

  Private Rect Originalrect = new Rect ();

  private float Starty;
    @Override protected void Onfinishinflate () {super.onfinishinflate ();
    if (Getchildcount () > 0) {childview = Getchildat (0);
  @Override public void Fling (int velocityy) {super.fling (VELOCITYY/2); @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {super.onlayout (changed, L, T,

    R, b);

    if (Childview = null) return; OriginaLrect.set (Childview.getleft (), Childview.gettop (), Childview.getright (), Childview.getbottom ()); Reboundscrollview (context, AttributeSet attrs, int defstyle) {Super (context, attr
  s, Defstyle);
  Public Reboundscrollview (context, AttributeSet attrs) {Super (context, attrs);
  Public Reboundscrollview {Super (context); /** * In the touch event, the logic for handling pull and drop-*/@Override public boolean dispatchtouchevent (motionevent ev) {if Childvi
    EW = null) {return super.dispatchtouchevent (EV);

    int action = Ev.getaction ();
        Switch (action) {Case MotionEvent.ACTION_DOWN:startY = ev.gety ();

      Break

        Case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:if (!havamoved) break;
        Translateanimation anim = new Translateanimation (0, 0, childview.gettop (), originalrect.top); Anim.setduration (Anim_time);

        Childview.startanimation (ANIM);
        Set the flag bit back to false havamoved = false;

        Resetviewlayout ();
      Break
        Case MotionEvent.ACTION_MOVE:float Nowy = Ev.gety ();
        int deltay = (int) (nowy-starty);
        int offset = (int) (DeltaY * move_delay); Childview.layout (originalrect.left, originalrect.top + offset, originalrect.right, Originalrect.bottom + OFFSE

        T);

        Havamoved = true;
      Break
    Default:break;
  return super.dispatchtouchevent (EV);  public void Resetviewlayout () {childview.layout (Originalrect.left, Originalrect.top, Originalrect.right,
  Originalrect.bottom);

 }

}

Post the code, and then analyze the specific implementation:
The first is to pull out the screen, and in the move process, I use layout to reset the position of the child view for more than part of the code.

The second thing to achieve is to rebound, dragged out always want to come back:
Here I define a rect, in the OnLayout (Boolean changed, int l, int t, int r, int b) methods, the initial position of ScrollView is recorded so that the rebound can be reset.

Say a lot, look at the key code in the code
The code for Move:

 float Nowy = ev.gety ();
        int deltay = (int) (nowy-starty);
        int offset = (int) (DeltaY * move_delay);
        Childview.layout (originalrect.left, originalrect.top + offset,
            originalrect.right, Originalrect.bottom + offset) ;

        Havamoved = true;

This is the time to do Motionevent.action_move, the Chlidview position reset is the Lauout method, which is based on the value of Originalrect, set the corresponding sliding coefficient, otherwise it is too sensitive.

Back-Bounce code:

 if (!havamoved) break
          ;

        Translateanimation anim = new Translateanimation (0, 0,
            childview.gettop (), originalrect.top);
        Anim.setduration (anim_time);

        Childview.startanimation (anim);
        Set the flag bit back to false
        havamoved = false;
        Resetviewlayout ();
...
...
 public void Resetviewlayout () {
    childview.layout (originalrect.left, originalrect.top,
        originalrect.right , originalrect.bottom);
  }

When the finger motionevent.action_up or motionevent.action_cancel, put Clildview in place, and then set the animation, this is the rebound.

Add:

It's a very simple rebound scrollview, and it's important to note that I don't differentiate between normal and on-screen movement on the screen, so it's important to determine whether the current move requires us to rewrite, that is, whether it's in the pull or pull down section. Without judgment, my current code will cause your gesture to move in a different distance from the control scrolling. I am feeling that if you use it normally you will not notice it. But this need is not ruled out.

The drop is not very good judgment, because I am operating here is childview,scrollview in the process of getscrolly () must be 0, so this point, I have not thought well. But it's easier to pull over. When Childview.height <= Scrollview.height + getscrolly is on the point of pulling out the screen, this should be able to figure it out.

Git address: Https://github.com/cjhandroid/ReboundScrollView

SOURCE Download: Http://xiazai.jb51.net/201611/yuanma/androidReboundScrollView (jb51.net). rar

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.