package com.leadien.www.widget; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.widget.Button; import android.widget.Scroller; /** * 循环滚动 * * @author hammer */ public class RollingCycle extends ViewGroup { private static final int INVALID_SCREEN = -999; private static final int SNAP_VELOCITY = 600; private int mDefaultScreen; private int mCurrentScreen; private int mNextScreen = INVALID_SCREEN; private boolean mFirstLayout = true; private Scroller mScroller; private VelocityTracker mVelocityTracker; private float mLastMotionX; private float mLastMotionY; private static final int INVALID_POINTER = -1; private int mActivePointerId = INVALID_POINTER; private final static int TOUCH_STATE_REST = 0; private final static int TOUCH_STATE_SCROLLING = 1; private int mTouchState = TOUCH_STATE_REST; public static int mDefaultSlop; public static int mTouchSlop; private int mMaximumVelocity; private static final float BASELINE_FLING_VELOCITY = 2500.f; private static final float FLING_VELOCITY_INFLUENCE = 0.4f; private static final float NANOTIME_DIV = 1000000000.0f; private static final float SMOOTHING_SPEED = 0.75f; private static final float SMOOTHING_CONSTANT = (float) (0.016 / Math .log(SMOOTHING_SPEED)); private float mSmoothingTime; private float mTouchX; /** * Used to inflate the Workspace from XML. */ public RollingCycle(Context context, AttributeSet attrs) { this(context, attrs, 0); } /** * Used to inflate the Workspace from XML. */ public RollingCycle(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mDefaultScreen = 0; final ViewConfiguration configuration = ViewConfiguration .get(getContext()); mTouchSlop = configuration.getScaledTouchSlop(); mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); initView(); } private void initView() { mScroller = new Scroller(getContext()); mCurrentScreen = mDefaultScreen; final ViewConfiguration configuration = ViewConfiguration .get(getContext()); mDefaultSlop = configuration.getScaledTouchSlop(); mTouchSlop = mDefaultSlop; /* * LayoutInflater inflater = LayoutInflater.from(getContext()); for (int * i = 0; i < 3; i++) { View view = * inflater.inflate(R.layout.sub_page_webview_girlcoding, null, false); * addView(view); } */ Button b1 = new Button(getContext()); b1.setText("b1"); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); b1.setLayoutParams(params); addView(b1); Button b2 = new Button(getContext()); b2.setText("b2"); LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); b2.setLayoutParams(params2); addView(b2); Button b3 = new Button(getContext()); b3.setText("b3"); LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); b3.setLayoutParams(params3); addView(b3); } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { mTouchX = mScroller.getCurrX(); scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } else if (mNextScreen != INVALID_SCREEN) { if (mNextScreen == -1 && true) { mCurrentScreen = getChildCount() - 1; scrollTo(mCurrentScreen * getWidth(), getScrollY()); } else if (mNextScreen == getChildCount() && true) { mCurrentScreen = 0; scrollTo(0, getScrollY()); } else { mCurrentScreen = Math.max(0, Math.min(mNextScreen, getChildCount() - 1)); } mNextScreen = INVALID_SCREEN; } else if (mTouchState == TOUCH_STATE_SCROLLING) { final float now = System.nanoTime() / NANOTIME_DIV; final float e = (float) Math.exp((now - mSmoothingTime) / SMOOTHING_CONSTANT); final float dx = mTouchX - getScrollX(); // mScrollX += dx * e; final int scrolltoX = getScrollX() + (int) (dx * e); super.scrollTo(scrolltoX, getScrollY()); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); final int width = MeasureSpec.getSize(widthMeasureSpec); final int widthMode = MeasureSpec.getMode(widthMeasureSpec); if (widthMode != MeasureSpec.EXACTLY) { throw new IllegalStateException( "Workspace can only be used in EXACTLY mode."); } final int heightMode = MeasureSpec.getMode(heightMeasureSpec); if (heightMode != MeasureSpec.EXACTLY) { throw new IllegalStateException( "Workspace can only be used in EXACTLY mode."); } // The children are given the same width and height as the workspace final int count = getChildCount(); for (int i = 0; i < count; i++) { getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec); } if (mFirstLayout) { scrollTo(mCurrentScreen * width, 0); mFirstLayout = false; } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childLeft = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() != View.GONE) { final int childWidth = child.getMeasuredWidth(); child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight()); childLeft += childWidth; } } } @Override protected void dispatchDraw(Canvas canvas) { boolean fastDraw = mTouchState != TOUCH_STATE_SCROLLING && mNextScreen == INVALID_SCREEN; if (fastDraw) { drawChild(canvas, getChildAt(mCurrentScreen), getDrawingTime()); } else { long drawingTime = getDrawingTime(); int width = getWidth(); float scrollPos = (float) getScrollX() / width; boolean endlessScrolling = true; int leftScreen; int rightScreen; boolean isScrollToRight = false; int childCount = getChildCount(); if (scrollPos < 0 && endlessScrolling) { leftScreen = childCount - 1; rightScreen = 0; } else { leftScreen = Math.min((int) scrollPos, childCount - 1); rightScreen = leftScreen + 1; if (endlessScrolling) { rightScreen = rightScreen % childCount; isScrollToRight = true; } } if (isScreenNoValid(leftScreen)) { if (rightScreen == 0 && !isScrollToRight) { int offset = childCount * width; canvas.translate(-offset, 0); drawChild(canvas, getChildAt(leftScreen), drawingTime); canvas.translate(+offset, 0); } else { drawChild(canvas, getChildAt(leftScreen), drawingTime); } } if (scrollPos != leftScreen && isScreenNoValid(rightScreen)) { if (endlessScrolling && rightScreen == 0 && isScrollToRight) { int offset = childCount * width; canvas.translate(+offset, 0); drawChild(canvas, getChildAt(rightScreen), drawingTime); canvas.translate(-offset, 0); } else { drawChild(canvas, getChildAt(rightScreen), drawingTime); } } } } private boolean isScreenNoValid(int screen) { return screen >= 0 && screen < getChildCount(); } int getCurrentScreen() { return mCurrentScreen; } @Override public void setOnClickListener(OnClickListener l) { final int count = getChildCount(); for (int i = 0; i < count; i++) { getChildAt(i).setOnClickListener(l); } } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { acquireVelocityTrackerAndAddMovement(ev); final int action = ev.getAction(); if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) { return true; } switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_MOVE: { final int pointerIndex = ev.findPointerIndex(mActivePointerId); final float x = ev.getX(pointerIndex); final float y = ev.getY(pointerIndex); final int xDiff = (int) Math.abs(x - mLastMotionX); final int yDiff = (int) Math.abs(y - mLastMotionY); final int touchSlop = mTouchSlop; boolean xMoved = xDiff > touchSlop; boolean yMoved = yDiff > touchSlop; if (xMoved || yMoved) { if (xMoved) { // Scroll if the user moved far enough along the X axis mTouchState = TOUCH_STATE_SCROLLING; mLastMotionX = x; mTouchX = getScrollX(); mSmoothingTime = System.nanoTime() / NANOTIME_DIV; } } break; } case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); // Remember location of down touch mLastMotionX = x; mLastMotionY = y; mActivePointerId = ev.getPointerId(0); /* * If being flinged and user touches the screen, initiate drag; * otherwise don't. mScroller.isFinished should be false when being * flinged. */ mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; break; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: // Release the drag mTouchState = TOUCH_STATE_REST; mActivePointerId = INVALID_POINTER; releaseVelocityTracker(); break; case MotionEvent.ACTION_POINTER_UP: break; } /* * The only time we want to intercept motion events is if we are in the * drag mode. */ return mTouchState != TOUCH_STATE_REST; } @Override public boolean onTouchEvent(MotionEvent ev) { final int action = ev.getAction(); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: if (!mScroller.isFinished()) { mScroller.abortAnimation(); } // Remember where the motion event started mLastMotionX = ev.getX(); mActivePointerId = ev.getPointerId(0); break; case MotionEvent.ACTION_MOVE: if (mTouchState == TOUCH_STATE_SCROLLING) { // Scroll to follow the motion event final int pointerIndex = ev.findPointerIndex(mActivePointerId); final float x = ev.getX(pointerIndex); final float deltaX = mLastMotionX - x; mLastMotionX = x; if (deltaX < 0) { // if (mTouchX > 0) { mTouchX += Math.max(-mTouchX, deltaX); mSmoothingTime = System.nanoTime() / NANOTIME_DIV; invalidate(); } else if (true && mTouchX > -getWidth()) { mTouchX += deltaX; mSmoothingTime = System.nanoTime() / NANOTIME_DIV; invalidate(); } } else if (deltaX > 0) { final float availableToScroll = getChildAt( getChildCount() - 1).getRight() - mTouchX - (true ? 0 : getWidth()); if (availableToScroll > 0) { mTouchX += Math.min(availableToScroll, deltaX); mSmoothingTime = System.nanoTime() / NANOTIME_DIV; invalidate(); } } else { awakenScrollBars(); } } break; case MotionEvent.ACTION_UP: if (mTouchState == TOUCH_STATE_SCROLLING) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); final int velocityX = (int) velocityTracker .getXVelocity(mActivePointerId); final int screenWidth = getWidth(); // final int whichScreen = (mScrollX + (screenWidth / 2)) / // screenWidth; final int whichScreen = (int) Math .floor((getScrollX() + (screenWidth / 2.0)) / screenWidth); final float scrolledPos = (float) getScrollX() / screenWidth; if (velocityX > SNAP_VELOCITY && mCurrentScreen > (true ? -1 : 0)) { // Fling hard enough to move left. // Don't fling across more than one screen at a time. final int bound = scrolledPos < whichScreen ? mCurrentScreen - 1 : mCurrentScreen; snapToScreen(Math.min(whichScreen, bound), velocityX, true); } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - (true ? 0 : 1)) { // Fling hard enough to move right // Don't fling across more than one screen at a time. final int bound = scrolledPos > whichScreen ? mCurrentScreen + 1 : mCurrentScreen; snapToScreen(Math.max(whichScreen, bound), velocityX, true); } else { snapToScreen(whichScreen, 0, true); } } mTouchState = TOUCH_STATE_REST; mActivePointerId = INVALID_POINTER; releaseVelocityTracker(); break; case MotionEvent.ACTION_CANCEL: if (mTouchState == TOUCH_STATE_SCROLLING) { final int screenWidth = getWidth(); final int whichScreen = (getScrollX() + (screenWidth / 2)) / screenWidth; snapToScreen(whichScreen, 0, true); } mTouchState = TOUCH_STATE_REST; mActivePointerId = INVALID_POINTER; releaseVelocityTracker(); break; case MotionEvent.ACTION_POINTER_UP: break; } return true; } void snapToScreen(int whichScreen) { snapToScreen(whichScreen, 0, false); } private void snapToScreen(int whichScreen, int velocity, boolean settle) { whichScreen = Math.max((true ? -1 : 0), Math.min(whichScreen, getChildCount() - (true ? 0 : 1))); mNextScreen = whichScreen; View focusedChild = getFocusedChild(); if (focusedChild != null && whichScreen != mCurrentScreen && focusedChild == getChildAt(mCurrentScreen)) { focusedChild.clearFocus(); } final int screenDelta = Math.max(1, Math.abs(whichScreen - mCurrentScreen)); final int newX = whichScreen * getWidth(); final int delta = newX - getScrollX(); int duration = (screenDelta + 1) * 100; if (!mScroller.isFinished()) { mScroller.abortAnimation(); } velocity = Math.abs(velocity); if (velocity > 0) { duration += (duration / (velocity / BASELINE_FLING_VELOCITY)) * FLING_VELOCITY_INFLUENCE; } else { duration += 100; } awakenScrollBars(duration); mScroller.startScroll(getScrollX(), 0, delta, 0, duration); invalidate(); } private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); } private void releaseVelocityTracker() { if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } } }