ListView 拉出回彈效果 overscroll,阻尼效果



一  Android 9 之后才新增的API. 原理是 

     1. 在View中增加了overSrollBy方法,用於記錄x, y 軸上滾動的之

     2. 在AbsListView的onTouchEvent中判斷是否到達邊界(頂部 或 底部) ,然后調用view.overScrollBy ,傳入 mScrollY等參數

     3. overScrollBy 最終賦值給View的mScrollX, mScrollY 兩個變量

      4. 在AbsListView中調用完overScrollBy之后,調用invalidate重繪


二  下面是一個直接使用API的例子

1. 自定義ListView

public class BounceListView extends ListView{
    private static final int MAX_Y_OVERSCROLL_DISTANCE = 200;
     
    private Context mContext;
    private int mMaxYOverscrollDistance;
     
    public BounceListView(Context context){
        super(context);
        mContext = context;
        initBounceListView();
    }
     
    public BounceListView(Context context, AttributeSet attrs){
        super(context, attrs);
        mContext = context;
        initBounceListView();
    }
     
    public BounceListView(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        mContext = context;
        initBounceListView();
    }
     
    private void initBounceListView(){
        //get the density of the screen and do some maths with it on the max overscroll distance
        //variable so that you get similar behaviors no matter what the screen size
         
        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){ 
        //This is where the magic happens, we have replaced the incoming maxOverScrollY with our own custom variable mMaxYOverscrollDistance; 
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent);  
    }
     
}



2. Activity

public class MainActivity extends Activity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        
        setContentView(linearLayout);
        
        
        BounceListView bounceListView = new BounceListView(this);
        
        String[] data = new String[30];
        for (int i = 0; i < data.length; i++) {
			data[i] = "天天記錄 " + i;
 		}
        
        ArrayAdapter<String> arrayAdapter = 
        		new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, data);
        
        bounceListView.setAdapter(arrayAdapter);
        
        linearLayout.addView(bounceListView);
    }
	
	
    
}


資料:

Android Overscroll: Revisited



轉載請注明出處:http://blog.csdn.net/love_world_/article/details/8155350



2013-04-08   更新,這種效果也叫“阻尼效果”




注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com