一直以来,没有写过技术博客。最近想整理一下自己学过的开源项目,通过动手加深对技术的理解
private void snapToDestination(){
final int screenWidth = getChildWdith();
final int whichScreen = (getScrollX() + (screenWidth / 2))
/ screenWidth;
snapToScreen(whichScreen);
}
@Override
public void computeScroll() {
if(mScroller.computeScrollOffset()){
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}else if(mNextScreen != INVALID_SCREEN){
mCurrentScreen = Math.max(0,
Math.min(mNextScreen, getChildCount() - 1));
mNextScreen = INVALID_SCREEN;
post(new Runnable() {
@Override
public void run() {
postViewSwitched(mLastScrollDirection);
}
});
}
}
android在Touch过程中,设置Scroller参数,获取滑动速率,用于ChildView切换计算
@Override
public boolean onTouchEvent(MotionEvent event) {
if(getChildCount() == 0)
return false;
if(mVelocityTracker == null){
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
final int action = event.getAction();
final float x = event.getX();
switch(action){
case MotionEvent.ACTION_DOWN:
/**
* If being flinged and user touches, stop the fling. isFinished
* will be false if being flinged.
*/
if(mScroller.isFinished()){
mScroller.abortAnimation();
}
// Remember where the motion event started
mLastMotionX = x;
mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST
: TOUCH_STATE_SCROLLING;
break;
case MotionEvent.ACTION_MOVE:
final int deltaX = (int)(mLastMotionX - x);
boolean xMoved = Math.abs(deltaX) > mTouchSlop;
if(xMoved){
mTouchState = TOUCH_STATE_SCROLLING;
if(mViewInitializeListener != null)
initializeView(deltaX);
}
if(mTouchState == TOUCH_STATE_SCROLLING){
// Scroll to follow the motion event
mLastMotionX = x;
int scrollX = getScrollX();
if(deltaX < 0){
if(scrollX > 0){
scrollBy(Math.max(-scrollX, deltaX), 0);
}
}else if(deltaX > 0){
final int availableToScroll = getChildAt(
getChildCount() - 1).getRight()
- getPaddingRight() - getHorizontalFadingEdgeLength()
- scrollX - getChildWdith();
scrollBy(Math.min(availableToScroll, deltaX), 0);
}
return true;
}
break;
case MotionEvent.ACTION_UP:
if(mTouchState == TOUCH_STATE_SCROLLING){
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int veloctiyX = (int) velocityTracker.getXVelocity();
if(veloctiyX > SNAP_VELOCITY && mCurrentScreen > 0){
snapToScreen(mCurrentScreen - 1);
}else if(veloctiyX < - SNAP_VELOCITY &&
mCurrentScreen < getChildCount() - 1){
snapToScreen(mCurrentScreen + 1);
}else{
snapToDestination();
}
if(mVelocityTracker != null){
mVelocityTracker.recycle();
mVelocityTracker = null;
}
}
mTouchState = TOUCH_STATE_REST;
break;
case MotionEvent.ACTION_CANCEL:
snapToDestination();
mTouchState = TOUCH_STATE_REST;
break;
}
return true;
}
private LinkedList<View> mLoadedViews;
private LinkedList<View> mRecycledViews;
private View obtainView(int position){
View convertView = getRecycledView();
View view = mAdapter.getView(position, convertView, this);
if(view != convertView && convertView != null){
mRecycledViews.add(convertView);
}
mLastObtainedViewWasRecycled = (view == convertView);
LayoutParams p = view.getLayoutParams();
if(p == null){
p = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
view.setLayoutParams(p);
}
return view;
}
private void postViewSwitched(int direction){
if(direction == 0)
return;
if(direction > 0){ // to the right
mCurrentAdapterIndex ++;
mCurrentBufferIndex ++;
mLazyInit.remove(LazyInit.LEFT);
mLazyInit.add(LazyInit.RIGHT);
// Recycle view outside buffer range
if(mCurrentAdapterIndex > mSideBuffer){
recycleView(mLoadedViews.removeFirst());
mCurrentBufferIndex --;
}
int newBufferIndex = mCurrentAdapterIndex + mSideBuffer;
if(newBufferIndex < mAdapter.getCount())
mLoadedViews.addLast(makeAndAddView(newBufferIndex, true));
}else { //to the left
mCurrentAdapterIndex --;
mCurrentBufferIndex --;
mLazyInit.add(LazyInit.LEFT);
mLazyInit.remove(LazyInit.RIGHT);
if((mAdapter.getCount() - 1 - mCurrentAdapterIndex) > mSideBuffer){
recycleView(mLoadedViews.removeLast());
}
int newBufferIndex = mCurrentAdapterIndex - mSideBuffer;
if(newBufferIndex > -1){
mLoadedViews.addFirst(makeAndAddView(newBufferIndex, false));
mCurrentBufferIndex ++;
}
}
requestLayout();
setVisibleView(mCurrentBufferIndex, true);
if(mIndicator != null){
mIndicator.onSwitched(mLoadedViews.get(mCurrentBufferIndex),
mCurrentAdapterIndex);
}
if(mViewSwitchListener != null){
mViewSwitchListener.onSwitched(mLoadedViews.get(mCurrentBufferIndex),
mCurrentAdapterIndex);
}
}
protected void recycleView(View v){
if(v == null)
return;
mRecycledViews.addFirst(v);
detachViewFromParent(v);
}