滑动翻页实现, 类似Gallery效果

public class GlidePage extends ViewGroup {
	
	private float mTouchX;
	private float mLastMotionX;
	private int mActivePointerId = -1;
	private Scroller mScroller;

	public GlidePage(Context context, AttributeSet attrs) {
		super(context, attrs);
		mScroller = new Scroller(context);
		//initScreen();
		// TODO Auto-generated constructor stub
	}
	
	public GlidePage(Context context, AttributeSet attrs, int defStyle) {
		super(context,attrs,defStyle);
		mScroller = new Scroller(context);
		//initScreen();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		int childLeft = 0;
		final int count = getChildCount();
		for(int i = 0; i < count; 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) {
		// TODO Auto-generated method stub
		final long drawingTime = getDrawingTime();
		final int count = getChildCount();
		for(int i = 0; i< count; i++) {
			drawChild(canvas, getChildAt(i), drawingTime);
		}
	}

	@Override
	public void computeScroll() {
		// TODO Auto-generated method stub
		if(mScroller.computeScrollOffset()) {
			mTouchX = mScroller.getCurrX();
			int scrollY = mScroller.getCurrY();
			scrollTo((int)mTouchX, scrollY);
			postInvalidate();
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		if(widthMode != MeasureSpec.EXACTLY) {
			throw new IllegalStateException("GlidePage can only be used in EXACTLY mode.");
		}
		
		final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		if(heightMode != MeasureSpec.EXACTLY) {
			throw new IllegalStateException("GlidePage can only be used in EXACTLY mode.");
		}
		
		final int count = getChildCount();
		for(int i = 0; i < count; i++) {
			getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e) {
		final int action = e.getAction();
		switch(action & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			mLastMotionX = e.getX();
			mActivePointerId = e.getPointerId(0);
			break;
		case MotionEvent.ACTION_MOVE:
			final int pointerIndex = e.findPointerIndex(mActivePointerId);
			try{
				final float x = e.getX(pointerIndex);
				final float deltaX = mLastMotionX - x;
				mLastMotionX = x;
				if(deltaX > 0) {
					scrollBy((int)deltaX, 0);
				} else if(deltaX < 0) {
					scrollBy((int)deltaX,0);
				} else {
					awakenScrollBars();
				}
			}catch(ArrayIndexOutOfBoundsException ex) {
				
			}
			break;
		case MotionEvent.ACTION_UP:
			int screenWidth = getWidth();
			int whichView = (getScrollX() + (screenWidth / 2)) / screenWidth;
			whichView = Math.max(0, Math.min(whichView, getChildCount() - 1));
			int newX = whichView * getWidth();
			int delta = newX - getScrollX();
			mScroller.startScroll(getScrollX(), 0, delta, 0);
			invalidate();
			break;
		case MotionEvent.ACTION_CANCEL:
			break;
		case MotionEvent.ACTION_POINTER_UP:
			break;
		}
		
		return true;
	}

}

 

转载于:https://www.cnblogs.com/dunning/archive/2012/07/12/2588409.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值