ScrollView with damping effect, imitation QQ space

Source: Internet
Author: User

Fixes a bug that was known in the last version and works better



Package Com.okdai;

Import Android.content.Context;
Import Android.graphics.Rect;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.widget.ImageView;
Import Android.widget.ScrollView;
Import Android.widget.Scroller;


/**
* ScrollView of damping effect
*/


public class Dampview extends ScrollView {
private static final int LEN = 0XC8;
private static final int DURATION = 500;
private static final int max_dy = 200;
Private Scroller Mscroller;
Touchtool tool;
int left, top;
Float StartX, Starty, CurrentX, currenty;
int IMAGEVIEWH;
int ROOTW, Rooth;
ImageView ImageView;
Boolean scrollertype;


Public Dampview (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);


}


Public Dampview (context context, AttributeSet Attrs) {
Super (context, attrs);
Mscroller = new Scroller (context);
}


Public Dampview (Context context) {
Super (context);


}


public void Setimageview (ImageView ImageView) {
This.imageview = ImageView;
}


@Override
protected int Computescrolldeltatogetchildrectonscreen (rect rect) {


return 0;
}


Private int[] Li = new int[2];
Private int[] Li2 = new int[2];
private float lastly;
Private Boolean startistop = true;


@Override
public boolean dispatchtouchevent (Motionevent event) {
Super.dispatchtouchevent (event);
int action = Event.getaction ();
if (!mscroller.isfinished ()) {
Return Super.ontouchevent (event);
}
CurrentX = Event.getx ();
CurrentY = Event.gety ();
Imageview.getlocationinwindow (LI);
Getlocationonscreen (LI2);
Imageview.gettop ();
Switch (action) {
Case Motionevent.action_down:
if (li[1]! = li2[1]) {//To determine when the touch is started, the ImageView is aligned with the top of the window
Startistop = false;
}
left = Imageview.getleft ();
top = Imageview.getbottom ();
ROOTW = GetWidth ();
Rooth = GetHeight ();
IMAGEVIEWH = Imageview.getheight ();
StartX = CurrentX;
Starty = CurrentY;
Tool = new Touchtool (Imageview.getleft (), Imageview.getbottom (), Imageview.getleft (),
Imageview.getbottom () + LEN);
Break
Case Motionevent.action_move:
if (!startistop && li[1] = = Li2[1]) {
Starty = CurrentY;
Startistop = true;
}
if (Imageview.isshown () && imageview.gettop () >= 0) {
if (tool! = NULL) {
int t = tool.getscrolly (currenty-starty);
if (!scrollertype && currenty < Lastly && imageview.getheight () > Imageviewh) {
ScrollTo (0, 0);
Imageview.getlocationinwindow (LI);
Getlocationonscreen (LI2);
Android.view.ViewGroup.LayoutParams params = Imageview.getlayoutparams ();
Params.height = t;
Imageview.setlayoutparams (params);
if (imageview.getheight () = = Imageviewh && li[1] = = Li2[1]) {
Scrollertype = true;
}
if (startistop && li[1]! = Li2[1]) {
Startistop = false;
}
}
if (t >= top && T <= imageview.getbottom () + LEN && li[1] = = li2[1] && currenty > Lastly) {
Android.view.ViewGroup.LayoutParams params = Imageview.getlayoutparams ();
Params.height = t;
Imageview.setlayoutparams (params);
}
}
Scrollertype = false;
}


lastly = CurrentY;
Break
Case MOTIONEVENT.ACTION_UP:
if (li[1] = = Li2[1]) {
Scrollertype = true;
Mscroller.startscroll (Imageview.getleft (), Imageview.getbottom (), 0-imageview.getleft (),
Imageviewh-imageview.getbottom (), DURATION);
Invalidate ();
}
Startistop = true;
Break
}


return true;
}


@Override
public void Computescroll () {
Super.computescroll ();
if (Mscroller.computescrolloffset ()) {
int x = Mscroller.getcurrx ();
int y = Mscroller.getcurry ();
Imageview.layout (0, 0, x + imageview.getwidth (), y);
Invalidate ();
if (!mscroller.isfinished () && scrollertype && y > Max_dy) {
Android.view.ViewGroup.LayoutParams params = Imageview.getlayoutparams ();
Params.height = y;
Imageview.setlayoutparams (params);
}
}
}


public class Touchtool {


private int StartX, starty;


Public Touchtool (int startX, int starty, int endx, int endY) {
Super ();
This.startx = StartX;
This.starty = Starty;
}


public int getscrollx (float dx) {
int xx = (int) (StartX + dx/2.5f);
return xx;
}


public int getscrolly (float dy) {
int yy = (int) (starty + dy/2.5f);
return yy;
}
}
}

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.