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 MD5加密(二)
  2. Android(安卓)Studio设置类代码模板
  3. Android(安卓)Camera代码位置
  4. Android(安卓)开发常用代码片段
  5. android
  6. 收藏代码-Android状态栏工具代码
  7. android 根据Bitmap bitmap 保存图片到手机上
  8. Android(安卓)EditText中添加图标的简单方法
  9. Android获取高清app图标代码分享

随机推荐

  1. ubuntu android 环境变量的配置
  2. PUSH机制
  3. 动态修改ViewPagerIndicator CustomTabPa
  4. Android设计开发要必用的Color.xml文件整
  5. Android(安卓)ScrollView嵌套ScrollView
  6. Android常用的多渠道打包方式整理(不断更
  7. Android(安卓)浅谈MatrixCursor
  8. Android实现获取短信验证码并自动填写功
  9. Android(安卓)多线程通信 Handler
  10. android半透叠加对照表