android 弹性效果

阅读更多
重写自己的view,覆盖父类的overScrollBy方法。
详细内容可以查看我的网站: http://1.playandroid.duapp.com/index.jsp
代码:
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.ScrollView;

/**
 * ClassName:MtScrollView
 * Function: TODO ADD FUNCTION .
 *
 * VERSION       DATE            AUTHOR       CHANGE/COMMENT
 * ──────────────────────────────────————————————————————————
 * (产品版本号)             2012-4-23          duweibin         create
 */
public class MtScrollView extends ScrollView {
private static final int MAX_Y_OVERSCROLL_DISTANCE = 200; 
    
    private Context mContext; 
    private int mMaxYOverscrollDistance; 
      
    public MtScrollView(Context context){ 
        super(context); 
        mContext = context; 
        initBounceListView(); 
    } 
      
    public MtScrollView(Context context, AttributeSet attrs){ 
        super(context, attrs); 
        mContext = context; 
        initBounceListView();
    } 
      
    public MtScrollView(Context context, AttributeSet attrs, int defStyle){ 
        super(context, attrs, defStyle); 
        mContext = context; 
        initBounceListView(); 
    } 
      
    private void initBounceListView(){ 
        final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); 
            final float density = metrics.density; 
          
        mMaxYOverscrollDistance = (int) (density * MAX_Y_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, maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent);   
    }
}


针对2.3以上版本,包括2.3

你可能感兴趣的:(android,overscrollby)