Java代码:
  • importandroid.content.Context;
  • importandroid.util.Log;
  • importandroid.content.res.TypedArray;
  • importandroid.util.AttributeSet;
  • importandroid.view.MotionEvent;
  • importandroid.view.VelocityTracker;
  • importandroid.view.View;
  • importandroid.view.ViewGroup;
  • importandroid.view.ViewConfiguration;
  • importandroid.widget.Scroller;
  • publicclassDragableSpaceextendsViewGroup{
  • privateScrollermScroller;
  • privateVelocityTrackermVelocityTracker;
  • privateintmScrollX=0;
  • privateintmCurrentScreen=0;
  • privatefloatmLastMotionX;
  • privatestaticfinalStringLOG_TAG="DragableSpace";
  • privatestaticfinalintSNAP_VELOCITY=1000;
  • privatefinalstaticintTOUCH_STATE_REST=0;
  • privatefinalstaticintTOUCH_STATE_SCROLLING=1;
  • privateintmTouchState=TOUCH_STATE_REST;
  • privateintmTouchSlop=0;
  • publicDragableSpace(Contextcontext){
  • super(context);
  • mScroller=newScroller(context);
  • mTouchSlop=ViewConfiguration.get(getContext()).getScaledTouchSlop();
  • this.setLayoutParams(newViewGroup.LayoutParams(
  • ViewGroup.LayoutParams.WRAP_CONTENT,
  • ViewGroup.LayoutParams.FILL_PARENT));
  • }
  • publicDragableSpace(Contextcontext,AttributeSetattrs){
  • super(context,attrs);
  • mScroller=newScroller(context);
  • mTouchSlop=ViewConfiguration.get(getContext()).getScaledTouchSlop();
  • this.setLayoutParams(newViewGroup.LayoutParams(
  • ViewGroup.LayoutParams.WRAP_CONTENT,
  • ViewGroup.LayoutParams.FILL_PARENT));
  • TypedArraya=getContext().obtainStyledAttributes(attrs,R.styleable.DragableSpace);
  • mCurrentScreen=a.getInteger(R.styleable.DragableSpace_default_screen,0);
  • }
  • @Override
  • publicbooleanonInterceptTouchEvent(MotionEventev){
  • finalintaction=ev.getAction();
  • if((action==MotionEvent.ACTION_MOVE)
  • &&(mTouchState!=TOUCH_STATE_REST)){
  • returntrue;
  • }
  • finalfloatx=ev.getX();
  • switch(action){
  • caseMotionEvent.ACTION_MOVE:
  • finalintxDiff=(int)Math.abs(x-mLastMotionX);
  • booleanxMoved=xDiff>mTouchSlop;
  • if(xMoved){
  • //ScrolliftheusermovedfarenoughalongtheXaxis
  • mTouchState=TOUCH_STATE_SCROLLING;
  • }
  • break;
  • caseMotionEvent.ACTION_DOWN:
  • //Rememberlocationofdowntouch
  • mLastMotionX=x;
  • mTouchState=mScroller.isFinished()?TOUCH_STATE_REST:TOUCH_STATE_SCROLLING;
  • break;
  • caseMotionEvent.ACTION_CANCEL:
  • caseMotionEvent.ACTION_UP:
  • //Releasethedrag
  • mTouchState=TOUCH_STATE_REST;
  • break;
  • }
  • returnmTouchState!=TOUCH_STATE_REST;
  • }
  • @Override
  • publicbooleanonTouchEvent(MotionEventevent){
  • if(mVelocityTracker==null){
  • mVelocityTracker=VelocityTracker.obtain();
  • }
  • mVelocityTracker.addMovement(event);
  • finalintaction=event.getAction();
  • finalfloatx=event.getX();
  • switch(action){
  • caseMotionEvent.ACTION_DOWN:
  • Log.i(LOG_TAG,"event:down");
  • if(!mScroller.isFinished()){
  • mScroller.abortAnimation();
  • }
  • //Rememberwherethemotioneventstarted
  • mLastMotionX=x;
  • break;
  • caseMotionEvent.ACTION_MOVE:
  • //Log.i(LOG_TAG,"event:move");
  • //if(mTouchState==TOUCH_STATE_SCROLLING){
  • //Scrolltofollowthemotionevent
  • finalintdeltaX=(int)(mLastMotionX-x);
  • mLastMotionX=x;
  • //Log.i(LOG_TAG,"event:move,deltaX"+deltaX+",mScrollX"+mScrollX);
  • if(deltaX<0){
  • if(mScrollX>0){
  • scrollBy(Math.max(-mScrollX,deltaX),0);
  • }
  • }elseif(deltaX>0){
  • finalintavailableToScroll=getChildAt(getChildCount()-1)
  • .getRight()
  • -mScrollX-getWidth();
  • if(availableToScroll>0){
  • scrollBy(Math.min(availableToScroll,deltaX),0);
  • }
  • }
  • //}
  • break;
  • caseMotionEvent.ACTION_UP:
  • Log.i(LOG_TAG,"event:up");
  • //if(mTouchState==TOUCH_STATE_SCROLLING){
  • finalVelocityTrackervelocityTracker=mVelocityTracker;
  • velocityTracker.computeCurrentVelocity(1000);
  • intvelocityX=(int)velocityTracker.getXVelocity();
  • if(velocityX>SNAP_VELOCITY&&mCurrentScreen>0){
  • //Flinghardenoughtomoveleft
  • snapToScreen(mCurrentScreen-1);
  • }elseif(velocityX<-SNAP_VELOCITY
  • &&mCurrentScreen<getChildCount()-1){
  • //Flinghardenoughtomoveright
  • snapToScreen(mCurrentScreen+1);
  • }else{
  • snapToDestination();
  • }
  • if(mVelocityTracker!=null){
  • mVelocityTracker.recycle();
  • mVelocityTracker=null;
  • }
  • //}
  • mTouchState=TOUCH_STATE_REST;
  • break;
  • caseMotionEvent.ACTION_CANCEL:
  • Log.i(LOG_TAG,"event:cancel");
  • mTouchState=TOUCH_STATE_REST;
  • }
  • mScrollX=this.getScrollX();
  • returntrue;
  • }
  • privatevoidsnapToDestination(){
  • finalintscreenWidth=getWidth();
  • finalintwhichScreen=(mScrollX+(screenWidth/2))/screenWidth;
  • Log.i(LOG_TAG,"fromdes");
  • snapToScreen(whichScreen);
  • }
  • publicvoidsnapToScreen(intwhichScreen){
  • Log.i(LOG_TAG,"snapToScreen"+whichScreen);
  • mCurrentScreen=whichScreen;
  • finalintnewX=whichScreen*getWidth();
  • finalintdelta=newX-mScrollX;
  • mScroller.startScroll(mScrollX,0,delta,0,Math.abs(delta)*2);
  • invalidate();
  • }
  • publicvoidsetToScreen(intwhichScreen){
  • Log.i(LOG_TAG,"setToScreen"+whichScreen);
  • mCurrentScreen=whichScreen;
  • finalintnewX=whichScreen*getWidth();
  • mScroller.startScroll(newX,0,0,0,10);
  • invalidate();
  • }
  • @Override
  • protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){
  • intchildLeft=0;
  • finalintcount=getChildCount();
  • for(inti=0;i<count;i++){
  • finalViewchild=getChildAt(i);
  • if(child.getVisibility()!=View.GONE){
  • finalintchildWidth=child.getMeasuredWidth();
  • child.layout(childLeft,0,childLeft+childWidth,child
  • .getMeasuredHeight());
  • childLeft+=childWidth;
  • }
  • }
  • }
  • @Override
  • protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
  • super.onMeasure(widthMeasureSpec,heightMeasureSpec);
  • finalintwidth=MeasureSpec.getSize(widthMeasureSpec);
  • finalintwidthMode=MeasureSpec.getMode(widthMeasureSpec);
  • if(widthMode!=MeasureSpec.EXACTLY){
  • thrownewIllegalStateException("errormode.");
  • }
  • finalintheightMode=MeasureSpec.getMode(heightMeasureSpec);
  • if(heightMode!=MeasureSpec.EXACTLY){
  • thrownewIllegalStateException("errormode.");
  • }
  • //Thechildrenaregiventhesamewidthandheightastheworkspace
  • finalintcount=getChildCount();
  • for(inti=0;i<count;i++){
  • getChildAt(i).measure(widthMeasureSpec,heightMeasureSpec);
  • }
  • Log.i(LOG_TAG,"movingtoscreen"+mCurrentScreen);
  • scrollTo(mCurrentScreen*width,0);
  • }
  • @Override
  • publicvoidcomputeScroll(){
  • if(mScroller.computeScrollOffset()){
  • mScrollX=mScroller.getCurrX();
  • scrollTo(mScrollX,0);
  • postInvalidate();
  • }
  • }
  • }


  • Java代码:
<?xmlversion= "1.0" encoding= "utf-8" ?>
<com.matthieu.launcher.DragableSpacexmlns:app= "http://schemas.android.com/apk/res/com.matthieu.launcher"
xmlns:android= "http://schemas.android.com/apk/res/android"
android:id= "@+id/space"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
app:default_screen= "1"
>
<includeandroid:id= "@+id/left" layout= "@layout/left_screen" />
<includeandroid:id= "@+id/center" layout= "@layout/initial_screen" />
<includeandroid:id= "@+id/right" layout= "@layout/right_screen" />
</com.matthieu.launcher.DragableSpace>

values/attrs.xml

<?xml version="1.0" encoding="utf-8"?> <resources>   <declare-styleable name="DragableSpace">     <attr name="default_screen" format="integer"/>   </declare-styleable> </resources> 

更多相关文章

  1. android常用的代码片段
  2. Android Camera代码位置
  3. Android Studio设置类代码模板
  4. Android PopupWindow动画效果代码
  5. Android获取高清app图标代码分享
  6. 收藏代码-Android状态栏工具代码
  7. Android 2.1 GPS定位和拍照功能代码
  8. android 震动和提示音的实现代码

随机推荐

  1. Android(安卓)SQLite使用详解和多线程并
  2. android基础入门(二)――创建android工程
  3. 2010-11-4
  4. Android读写XML(上)——package说明
  5. 如何退出Android应用程序
  6. android系统更新应用的添加
  7. 17 位谷歌 Android(安卓)开发专家是如何
  8. Android(安卓)View事件分发、拦截、消费
  9. Android中的消息机制:Handler消息传递机制
  10. Android(安卓)Studio系列教程六--Gradle