Workspace界面模拟
来源:互联网 发布:宁波每日成交数据 编辑:程序博客网 时间:2024/04/26 15:57
>模拟WorkSpace界面的实现代码
>>onMeasure onLayout初始化界面
>>onInterceptTouchEvent() onTouchEvent()实现左右滑动
/** * @author Lean @date:2014-10-30 */public class MyScrollLayout extends ViewGroup{private static final int TOUCH_STATE_SCROLLING = 0;private static final int TOUCH_STATE_REST = 1;private static final int SNAP_VELOCITY = 700;private Scroller mScroller;private int mTouchSlop;private int mTouchState;private VelocityTracker mVelocityTracker;private float mDownX;private int mCurrenScreen;public MyScrollLayout(Context context, AttributeSet attrs) {super(context, attrs);mScroller=new Scroller(context);mTouchSlop=ViewConfiguration.get(context).getScaledTouchSlop();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int widthMode=MeasureSpec.getMode(widthMeasureSpec);int heightMode=MeasureSpec.getMode(heightMeasureSpec);if (widthMode!=MeasureSpec.EXACTLY||heightMode!=MeasureSpec.EXACTLY) {throw new IllegalStateException("you must improve a exacty child view !");}int width=MeasureSpec.getSize(widthMeasureSpec);final int childCount=getChildCount();for (int i = 0; i <childCount; i++) {getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);}scrollTo(mCurrenScreen*width, 0);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {if (changed) {int childLeft=0;final int childCount=getChildCount();for (int i = 0; i < childCount; i++) {View childView=getChildAt(i);getChildAt(i).layout(childLeft, 0, childLeft+getWidth(),childView.getMeasuredHeight());childLeft+=getWidth();}}}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {int action=ev.getAction();if (action==MotionEvent.ACTION_MOVE&&(mTouchState!=TOUCH_STATE_REST)) {return true;}switch (action) {case MotionEvent.ACTION_DOWN:mDownX=ev.getRawX();mTouchState=mScroller.isFinished()?TOUCH_STATE_REST:TOUCH_STATE_SCROLLING;break;case MotionEvent.ACTION_MOVE:float deltaX=Math.abs(mDownX-ev.getRawX());if (mTouchSlop<deltaX) {mTouchState=TOUCH_STATE_SCROLLING;}break;case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:mTouchState=TOUCH_STATE_REST;break;default:break;}return mTouchState!=TOUCH_STATE_REST;}@Overridepublic boolean onTouchEvent(MotionEvent event) {createVelocity(event);switch (event.getAction()) {case MotionEvent.ACTION_DOWN:if (!mScroller.isFinished()) {mScroller.abortAnimation();}mDownX=event.getRawX();break;case MotionEvent.ACTION_MOVE:int deltaX=(int) (mDownX-event.getRawX());mDownX=event.getRawX();scrollBy(deltaX, 0);break;case MotionEvent.ACTION_UP:mVelocityTracker.computeCurrentVelocity(1000);int velocityX=(int) mVelocityTracker.getXVelocity();if (velocityX>SNAP_VELOCITY&&mCurrenScreen>0) {snapToScreen(mCurrenScreen-1);}else if (velocityX<-SNAP_VELOCITY&&mCurrenScreen<getChildCount()-1) {snapToScreen(mCurrenScreen+1);}else {snapToDestination();}recycleVelocity();mTouchState=TOUCH_STATE_REST;break;case MotionEvent.ACTION_CANCEL:mTouchState=TOUCH_STATE_REST;break;default:break;}return true;}private void snapToScreen(int whichScreen) {whichScreen=Math.max(0, Math.min(whichScreen,getChildCount()-1));if (getScrollX()!=(whichScreen*getWidth())) {int deltaX=whichScreen*getWidth()-getScrollX();mScroller.startScroll(getScrollX(),0, deltaX, 0,Math.abs(deltaX)*2);mCurrenScreen=whichScreen;invalidate();}}private void snapToDestination() {int whichScreen=(getScrollX()+getWidth()/2)/getWidth();snapToScreen(whichScreen);}@Overridepublic void computeScroll() {if (mScroller.computeScrollOffset()) {scrollTo(mScroller.getCurrX(),mScroller.getCurrY());invalidate();}}private void recycleVelocity() {if (mVelocityTracker!=null) {mVelocityTracker.recycle();mVelocityTracker=null;}}private void createVelocity(MotionEvent event) {if (mVelocityTracker==null) {mVelocityTracker=VelocityTracker.obtain();}mVelocityTracker.addMovement(event);}}
0 0
- Workspace界面模拟
- WorkSpace
- MSN 界面 模拟 (-)
- 模拟cmd界面
- QQ界面模拟----(1)
- 模拟QQ界面
- UITableView模拟聊天界面
- php模拟cmd界面
- qq登陆界面模拟
- 模拟一个点菜界面
- 模拟ATM柜员机界面
- 模拟QQ登录界面
- MiniTwitterSimulate 模拟登陆界面
- 模拟滑动解锁界面
- 模拟登陆注册界面
- 百度界面模拟
- 登陆界面模拟03-sign_up界面
- QQ界面的简单模拟
- java中Class.getResource用法
- FragmentPagerAdapter与FragmentStatePagerAdapter区别
- 设计模式之禅 读书总结
- rails tips
- php中$_REQUEST、$_POST、$_GET的区别和联系小结
- Workspace界面模拟
- Linux系统编程(第2版)笔记 (本书基本上就是Linux C API的简单使用说明,入门级别的)
- USB OTG中ID脚解释
- 硬盘内部硬件结构和工作原理详解
- RMAN备份与恢复—恢复无备份的数据文件
- 无废话WCF入门教程二[WCF应用的通信过程]
- MSM8974 TP 多点触控 B协议
- Dynamic historical stock data with d3.js and YQL
- Dos命令格式语法描述