本文是在http://blog.csdn.net/zhongkejingwang/article/details/38728119的基础上改的
仿多看滑动
看了原作者的思路,觉得很好,就按照自己的理解写了一遍,这里做个记录,后期再补充完善
public class MyScanView extends ViewGroup {
private static final int VELOCITY_SLIDE_THRESHOLD = 20;
private View mPreView;
private View mCurView;
private View mNextView;
private PageAdapter mAdapter;
private int mPageIndex;
private int mPrePageLeft;
private int mCurPageLeft;
private int mNextPageLeft;
private boolean mIsInit;
private int mWidth;
private int mHeight;
private int mLastX;
private int mLastY;
private int mTouchSlop;
private VelocityTracker mVelocityTracker;
private Scroller mScroller;
private boolean mIsActionUp = false;
private enum SlideState{
NONE, PRE, CURRENT
}
private SlideState mState = SlideState.NONE;
public MyScanView(Context context) {
this(context, null);
}
public MyScanView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyScanView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs){
mPageIndex = 1;
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mScroller = new Scroller(context);
}
public void setAdapter(PageAdapter adapter){
if(adapter == null){
return;
}
removeAllViews();
mAdapter = adapter;
mPreView = adapter.getView();
adapter.addContent(mPreView, mPageIndex - 1);
addView(mPreView, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
mCurView = adapter.getView();
adapter.addContent(mCurView, mPageIndex);
addView(mCurView, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
mNextView = adapter.getView();
adapter.addContent(mNextView, mPageIndex + 1);
addView(mNextView, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
DebugTools.d("view2 dispatchTouchEvent action: " + ev.getAction());
switch(ev.getAction()){
case MotionEvent.ACTION_DOWN:
mIsActionUp = false;
addVelocityTracker(ev);
mLastX = (int)ev.getX();
mLastY = (int)ev.getY();
if(mIsLayouting){
if(mAnim != null){
mAnim.cancel();
}
mIsLayouting = false;
}
break;
case MotionEvent.ACTION_MOVE:
addVelocityTracker(ev);
mIsActionUp = false;
int deltaX = (int)ev.getX() - mLastX;
int deltaY = (int)ev.getY() - mLastY;
if(Math.abs(deltaX) > Math.abs(deltaY)){
DebugTools.d("page2 mPageIndex: " + mPageIndex + " count: " + mAdapter.getCount());
if((deltaX < 0 && mState == SlideState.NONE && mPageIndex < mAdapter.getCount()) || (mState == SlideState.CURRENT && !mIsLayouting)){
mCurPageLeft += deltaX;
DebugTools.d("view2 dispatchTouchEvent mCurPageLeft: " + mCurPageLeft);
mState = SlideState.CURRENT;
requestLayout();
}else if((deltaX > 0 && mState == SlideState.NONE && mPageIndex > 1) || (mState == SlideState.PRE && !mIsLayouting)){
mPrePageLeft += deltaX;
mState = SlideState.PRE;
requestLayout();
}
}
mLastX = (int)ev.getX();
mLastY = (int)ev.getY();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
if(mState == SlideState.CURRENT && !mIsLayouting){
float speed = getVelocity();
DebugTools.d("view2 speed: " + speed);
if(mCurPageLeft < -mWidth / 2 || speed < -VELOCITY_SLIDE_THRESHOLD){
startScroll(mCurPageLeft, -mWidth);
}else{
startScroll(mCurPageLeft, 0);
}
invalidate();
}else if(mState == SlideState.PRE && !mIsLayouting){
float speed = getVelocity();
if(mPrePageLeft > -mWidth / 2 || speed > VELOCITY_SLIDE_THRESHOLD){
startScroll(mPrePageLeft, 0);
}else{
startScroll(mPrePageLeft, -mWidth);
}
invalidate();
}
recycleVelocityTracker();
mIsActionUp = true;
break;
}
super.dispatchTouchEvent(ev);
return true;
}
private ValueAnimator mAnim = null;
private void startScroll(final int start, final int end){
mAnim = ValueAnimator.ofInt(start, end);
mAnim.setDuration(400);
mAnim.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (Integer) animation.getAnimatedValue();
boolean isScrolling = value != end;
handleScroll(isScrolling, value);
}
});
mAnim.start();
}
private float getVelocity(){
if(mVelocityTracker == null){
return 0;
}
mVelocityTracker.computeCurrentVelocity(1000);
return mVelocityTracker.getXVelocity();
}
private boolean mIsLayouting = false;
private void handleScroll(boolean isScrolling, int value){
if(isScrolling){
if(mState == SlideState.CURRENT){
mCurPageLeft = value;
requestLayout();
mIsLayouting = true;
}else if(mState == SlideState.PRE){
mPrePageLeft = value;
requestLayout();
mIsLayouting = true;
}
}else{
if(mIsActionUp){
DebugTools.d("view2 computeScroll mState: " + mState + " mCurPageLeft: " + mCurPageLeft);
if(mState == SlideState.CURRENT){
mPageIndex++;
addNextPage();
}else if(mState == SlideState.PRE){
mPageIndex--;
addPrePage();
}
mState = SlideState.NONE;
}
mIsLayouting = false;
}
}
private void addNextPage(){
removeView(mPreView);
addView(mPreView, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
mAdapter.addContent(mPreView, mPageIndex + 1);
View temp = mCurView;
mCurView = mNextView;
mNextView = mPreView;
mPreView = temp;
mCurPageLeft = 0;
}
private void addPrePage(){
removeView(mNextView);
addView(mNextView, -1, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
mAdapter.addContent(mNextView, mPageIndex - 1);
View temp = mCurView;
mCurView = mPreView;
mPreView = mNextView;
mNextView = temp;
mPrePageLeft = -mWidth;
}
private void addVelocityTracker(MotionEvent event){
if(mVelocityTracker == null){
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
}
private void recycleVelocityTracker(){
if(mVelocityTracker != null){
mVelocityTracker.clear();
mVelocityTracker = null;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
if(!mIsInit){
mPrePageLeft = -mWidth;
mCurPageLeft = 0;
mNextPageLeft = 0;
mIsInit = true;
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if(mAdapter == null){
return;
}
mNextView.layout(mNextPageLeft, 0, mNextPageLeft + mNextView.getMeasuredWidth(), mNextView.getMeasuredHeight());
mCurView.layout(mCurPageLeft, 0, mCurPageLeft + mCurView.getMeasuredWidth(), mCurView.getMeasuredHeight());
mPreView.layout(mPrePageLeft, 0, mPrePageLeft + mPreView.getMeasuredWidth(), mPreView.getMeasuredHeight());
invalidate();
}
}