天天看點

回彈效果HorizontalScrollview

實作方法一

import android.content.Context;  
import android.util.AttributeSet;  
import android.util.DisplayMetrics;  
import android.widget.HorizontalScrollView;  

public class BouncyHScrollView extends HorizontalScrollView {  

    private static final int MAX_X_OVERSCROLL_DISTANCE = ;     
    private Context mContext;     
    private int mMaxXOverscrollDistance;  

    public BouncyHScrollView(Context context) {  
        super(context);  
        // TODO Auto-generated constructor stub  
        mContext = context;     
        initBounceDistance();  
    }  
    public BouncyHScrollView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        // TODO Auto-generated constructor stub  
        mContext = context;     
        initBounceDistance();  
    }  
    public BouncyHScrollView(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
        // TODO Auto-generated constructor stub  
        mContext = context;     
        initBounceDistance();  
    }  

    private void initBounceDistance(){     
        final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();     
        mMaxXOverscrollDistance = (int) (metrics.density * MAX_X_OVERSCROLL_DISTANCE);     
    }     

    @Override    
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent){      
        //這塊是關鍵性代碼    
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, mMaxXOverscrollDistance, maxOverScrollY, isTouchEvent);       
    }    

}  
           

實作方法二

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;  

public class ElasticHorizontalScrollView extends HorizontalScrollView {  
    private View inner;  
    private Rect normal = new Rect();  
    private float x;  

    public ElasticHorizontalScrollView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }  

    public ElasticHorizontalScrollView(Context context) {  
        super(context);  
    }  

    @Override  
    protected void onFinishInflate() {  
        if (getChildCount() > ) {  
            inner = getChildAt();  
        }  
        super.onFinishInflate();  
    }  

    @Override  
    public boolean onTouchEvent(MotionEvent ev) {  
        if (ev == null) {  
            return super.onTouchEvent(ev);  
        } else {  
            commOnTouchEvent(ev);  
        }  
        return super.onTouchEvent(ev);  
    }  

    private void commOnTouchEvent(MotionEvent ev) {  
        int action = ev.getAction();  
        switch (action) {  
        case MotionEvent.ACTION_DOWN:  
            x = ev.getX();  
            break;  
        case MotionEvent.ACTION_UP:  
            if (isNeedAnimation()) {  
                animation();  
            }  
            break;  
        case MotionEvent.ACTION_MOVE:  
            final float preX = x;  
            float nowX = ev.getX();  
            int distanceX = (int) (preX - nowX);  
            scrollBy(distanceX, );  
            x = nowX;  
            if (isNeedMove()) {  
                if (normal.isEmpty()) {  
                    normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom());  
                }  
                inner.layout(inner.getLeft() - distanceX, inner.getTop(), inner.getRight() - distanceX, inner.getBottom());  
            }  

            break;  

        default:  
            break;  
        }  
    }  

    private void animation() {  
        TranslateAnimation mTranslateAnimation = new TranslateAnimation(inner.getLeft(), , normal.left, );  
        mTranslateAnimation.setDuration();  
        inner.setAnimation(mTranslateAnimation);  
        inner.layout(normal.left, normal.top, normal.right, normal.bottom);  
        normal.setEmpty();  

    }  

    private boolean isNeedAnimation() {  
        return !normal.isEmpty();  
    }  

    private boolean isNeedMove() {  
        int offset = inner.getMeasuredWidth() - getWidth();  
        int scrollX = getScrollX();  
        if (scrollX ==  || offset == scrollX)  
            return true;  
        return false;  
    }  
}  
           

繼續閱讀