Android的设置或者登陆或者其他的一些主窗体要展示的功能需要用到sliding的效果,下面就叫大家怎么做。直接上代码!!

MainActivity部分:

packagenet.ting.sliding;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.ImageView;publicclassMainActivityextendsActivityimplementsOnClickListener{privateSlideMenuslideMenu;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);slideMenu=(SlideMenu)findViewById(R.id.slide_menu);ImageViewmenuImg=(ImageView)findViewById(R.id.title_bar_menu_btn);menuImg.setOnClickListener(this);}@OverridepublicvoidonClick(Viewv){switch(v.getId()){caseR.id.title_bar_menu_btn:if(slideMenu.isMainScreenShowing()){slideMenu.openMenu();}else{slideMenu.closeMenu();}break;}}}

SlideMenu部分:

packagenet.ting.sliding;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.util.AttributeSet;importandroid.view.MotionEvent;importandroid.view.VelocityTracker;importandroid.view.View;importandroid.view.ViewConfiguration;importandroid.view.ViewGroup;importandroid.widget.Scroller;publicclassSlideMenuextendsViewGroup{publicstaticfinalintSCREEN_MENU=0;publicstaticfinalintSCREEN_MAIN=1;privatestaticfinalintSCREEN_INVALID=-1;privateintmCurrentScreen;privateintmNextScreen=SCREEN_INVALID;privateScrollermScroller;privateVelocityTrackermVelocityTracker;privateintmTouchSlop;privatefloatmLastMotionX;privatefloatmLastMotionY;privatefinalstaticintTOUCH_STATE_REST=0;privatefinalstaticintTOUCH_STATE_SCROLLING=1;privatestaticfinalintSNAP_VELOCITY=1000;publicintmTouchState=TOUCH_STATE_REST;privatebooleanmLocked;privatebooleanmAllowLongPress;publicSlideMenu(Contextcontext){this(context,null,0);}publicSlideMenu(Contextcontext,AttributeSetattrs){this(context,attrs,0);}publicSlideMenu(Contextcontext,AttributeSetattrs,intdefStyle){super(context,attrs,defStyle);mScroller=newScroller(getContext());mCurrentScreen=SCREEN_MAIN;mTouchSlop=ViewConfiguration.get(getContext()).getScaledTouchSlop();}@OverrideprotectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){super.onMeasure(widthMeasureSpec,heightMeasureSpec);measureViews(widthMeasureSpec,heightMeasureSpec);}publicvoidmeasureViews(intwidthMeasureSpec,intheightMeasureSpec){ViewmenuView=getChildAt(0);menuView.measure(menuView.getLayoutParams().width+menuView.getLeft()+menuView.getRight(),heightMeasureSpec);ViewcontentView=getChildAt(1);contentView.measure(widthMeasureSpec,heightMeasureSpec);}@OverrideprotectedvoidonLayout(booleanchanged,intl,intt,intr,intb){intchildCount=getChildCount();if(childCount!=2){thrownewIllegalStateException("ThechildCountofSlidingMenumustbe2");}ViewmenuView=getChildAt(0);finalintwidth=menuView.getMeasuredWidth();menuView.layout(-width,0,0,menuView.getMeasuredHeight());ViewcontentView=getChildAt(1);contentView.layout(0,0,contentView.getMeasuredWidth(),contentView.getMeasuredHeight());}@OverrideprotectedvoidonFinishInflate(){super.onFinishInflate();Viewchild;for(inti=0;i<getChildCount();i++){child=getChildAt(i);child.setFocusable(true);child.setClickable(true);}}@OverridepublicbooleanonInterceptTouchEvent(MotionEventev){if(mLocked){returntrue;}finalintaction=ev.getAction();if((action==MotionEvent.ACTION_MOVE)&&(mTouchState!=TOUCH_STATE_REST)){returntrue;}finalfloatx=ev.getX();finalfloaty=ev.getY();switch(action){caseMotionEvent.ACTION_MOVE:finalintxDiff=(int)Math.abs(x-mLastMotionX);finalintyDiff=(int)Math.abs(y-mLastMotionY);finalinttouchSlop=mTouchSlop;booleanxMoved=xDiff>touchSlop;booleanyMoved=yDiff>touchSlop;if(xMoved||yMoved){if(xMoved&&(yDiff<xDiff)){//ScrolliftheusermovedfarenoughalongtheXaxismTouchState=TOUCH_STATE_SCROLLING;enableChildrenCache();}//Eitherway,cancelanypendinglongpressif(mAllowLongPress){mAllowLongPress=false;//Trycancelingthelongpress.Itcouldalsohavebeen//scheduled//byadistantdescendant,sousethemAllowLongPressflag//toblock//everythingfinalViewcurrentScreen=getChildAt(mCurrentScreen);currentScreen.cancelLongPress();}}break;caseMotionEvent.ACTION_DOWN://RememberlocationofdowntouchmLastMotionX=x;mLastMotionY=y;mAllowLongPress=true;mTouchState=mScroller.isFinished()?TOUCH_STATE_REST:TOUCH_STATE_SCROLLING;break;caseMotionEvent.ACTION_CANCEL:caseMotionEvent.ACTION_UP://ReleasethedragclearChildrenCache();mTouchState=TOUCH_STATE_REST;mAllowLongPress=false;break;}/**Theonlytimewewanttointerceptmotioneventsisifweareinthe*dragmode.*/returnmTouchState!=TOUCH_STATE_REST;}voidenableChildrenCache(){finalintcount=getChildCount();for(inti=0;i<count;i++){finalViewlayout=(View)getChildAt(i);layout.setDrawingCacheEnabled(true);}}voidclearChildrenCache(){finalintcount=getChildCount();for(inti=0;i<count;i++){finalViewlayout=(View)getChildAt(i);layout.setDrawingCacheEnabled(false);}}@OverridepublicbooleanonTouchEvent(MotionEventev){if(mLocked){returntrue;}if(mVelocityTracker==null){mVelocityTracker=VelocityTracker.obtain();}mVelocityTracker.addMovement(ev);finalintaction=ev.getAction();finalfloatx=ev.getX();switch(action){caseMotionEvent.ACTION_DOWN:/**Ifbeingflingedandusertouches,stopthefling.isFinished*willbefalseifbeingflinged.*/if(!mScroller.isFinished()){mScroller.abortAnimation();}//RememberwherethemotioneventstartedmLastMotionX=x;break;caseMotionEvent.ACTION_MOVE:if(mTouchState==TOUCH_STATE_SCROLLING){//ScrolltofollowthemotioneventfinalintdeltaX=(int)(mLastMotionX-x);mLastMotionX=x;if(deltaX<0){if(deltaX+getScrollX()>=-getChildAt(0).getWidth()){scrollBy(deltaX,0);}}elseif(deltaX>0){finalintavailableToScroll=getChildAt(getChildCount()-1).getRight()-getScrollX()-getWidth();if(availableToScroll>0){scrollBy(Math.min(availableToScroll,deltaX),0);}}}break;caseMotionEvent.ACTION_UP:if(mTouchState==TOUCH_STATE_SCROLLING){finalVelocityTrackervelocityTracker=mVelocityTracker;velocityTracker.computeCurrentVelocity(1000);intvelocityX=(int)velocityTracker.getXVelocity();if(velocityX>SNAP_VELOCITY&&mCurrentScreen==SCREEN_MAIN){//FlinghardenoughtomoveleftsnapToScreen(SCREEN_MENU);}elseif(velocityX<-SNAP_VELOCITY&&mCurrentScreen==SCREEN_MENU){//FlinghardenoughtomoverightsnapToScreen(SCREEN_MAIN);}else{snapToDestination();}if(mVelocityTracker!=null){mVelocityTracker.recycle();mVelocityTracker=null;}}mTouchState=TOUCH_STATE_REST;break;caseMotionEvent.ACTION_CANCEL:mTouchState=TOUCH_STATE_REST;}returntrue;}@OverridepublicvoidcomputeScroll(){if(mScroller.computeScrollOffset()){scrollTo(mScroller.getCurrX(),mScroller.getCurrY());}elseif(mNextScreen!=SCREEN_INVALID){mCurrentScreen=Math.max(0,Math.min(mNextScreen,getChildCount()-1));mNextScreen=SCREEN_INVALID;clearChildrenCache();}}@OverridepublicvoidscrollTo(intx,inty){super.scrollTo(x,y);postInvalidate();}@OverrideprotectedvoiddispatchDraw(Canvascanvas){finalintscrollX=getScrollX();super.dispatchDraw(canvas);canvas.translate(scrollX,0);}@OverridepublicbooleandispatchUnhandledMove(Viewfocused,intdirection){if(direction==View.FOCUS_LEFT){if(getCurrentScreen()>0){snapToScreen(getCurrentScreen()-1);returntrue;}}elseif(direction==View.FOCUS_RIGHT){if(getCurrentScreen()<getChildCount()-1){snapToScreen(getCurrentScreen()+1);returntrue;}}returnsuper.dispatchUnhandledMove(focused,direction);}protectedvoidsnapToScreen(intwhichScreen){enableChildrenCache();whichScreen=Math.max(0,Math.min(whichScreen,getChildCount()-1));booleanchangingScreens=whichScreen!=mCurrentScreen;mNextScreen=whichScreen;ViewfocusedChild=getFocusedChild();if(focusedChild!=null&&changingScreens&&focusedChild==getChildAt(mCurrentScreen)){focusedChild.clearFocus();}finalintnewX=(whichScreen-1)*getChildAt(0).getWidth();finalintdelta=newX-getScrollX();mScroller.startScroll(getScrollX(),0,delta,0,Math.abs(delta)*2);invalidate();}protectedvoidsnapToDestination(){if(getScrollX()==0){return;}finalintscreenWidth=getChildAt(0).getWidth();finalintwhichScreen=(screenWidth+getScrollX()+(screenWidth/2))/screenWidth;snapToScreen(whichScreen);}publicintgetCurrentScreen(){returnmCurrentScreen;}publicbooleanisMainScreenShowing(){returnmCurrentScreen==SCREEN_MAIN;}publicvoidopenMenu(){mCurrentScreen=SCREEN_MENU;snapToScreen(mCurrentScreen);}publicvoidcloseMenu(){mCurrentScreen=SCREEN_MAIN;snapToScreen(mCurrentScreen);}publicvoidunlock(){mLocked=false;}publicvoidlock(){mLocked=true;}}

activity_main.xml部分:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><net.ting.sliding.SlideMenuandroid:id="@+id/slide_menu"android:layout_width="fill_parent"android:layout_height="fill_parent"><includelayout="@layout/layout_menu"/><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/color_2"android:orientation="vertical"><includelayout="@layout/layout_title_bar"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="HelloWorld"android:textSize="22sp"/></RelativeLayout></net.ting.sliding.SlideMenu></RelativeLayout>

layout_menu.xml部分:

<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="240dip"android:layout_height="fill_parent"android:background="@drawable/bitmap_book_read_chapterlist_repeat"android:orientation="vertical"><TextViewandroid:layout_width="240dip"android:layout_height="28.0dip"android:background="#ff313131"android:gravity="center_vertical"android:paddingLeft="15.0dip"android:text="分类"android:textColor="#ff959595"android:textSize="14.0sp"/><TextViewandroid:layout_width="240dip"android:layout_height="50.0dip"android:background="@drawable/selector_category_item"android:clickable="true"android:drawableLeft="@drawable/ic_category_mark"android:drawablePadding="1.0dip"android:gravity="center_vertical"android:paddingLeft="15.0dip"android:paddingRight="10.0dip"android:text="侧边栏选项-1"android:textColor="#ff959595"android:textSize="16.0sp"/><Viewandroid:layout_width="240dip"android:layout_height="2.0dip"android:background="@drawable/ic_shelf_category_divider"/><TextViewandroid:layout_width="240dip"android:layout_height="50.0dip"android:background="@drawable/selector_category_item"android:clickable="true"android:drawableLeft="@drawable/ic_category_mark"android:drawablePadding="1.0dip"android:gravity="center_vertical"android:paddingLeft="15.0dip"android:paddingRight="10.0dip"android:text="侧边栏选项-2"android:textColor="#ff959595"android:textSize="16.0sp"/><Viewandroid:layout_width="240dip"android:layout_height="2.0dip"android:background="@drawable/ic_shelf_category_divider"/><TextViewandroid:layout_width="240dip"android:layout_height="50.0dip"android:background="@drawable/selector_category_item"android:clickable="true"android:drawableLeft="@drawable/ic_category_mark"android:drawablePadding="1.0dip"android:gravity="center_vertical"android:paddingLeft="15.0dip"android:paddingRight="10.0dip"android:text="侧边栏选项-3"android:textColor="#ff959595"android:textSize="16.0sp"/><Viewandroid:layout_width="240dip"android:layout_height="2.0dip"android:background="@drawable/ic_shelf_category_divider"/></LinearLayout>

layout_title_bar.xml部分:

<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="45.0dip"android:background="@drawable/bg_title_bar"android:gravity="center_vertical"><ImageViewandroid:id="@+id/title_bar_menu_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="3.0dip"android:layout_marginRight="3.0dip"android:layout_marginTop="3.0dip"android:gravity="center"android:src="@drawable/ic_top_bar_category"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_toRightOf="@id/title_bar_menu_btn"android:background="@drawable/ic_top_divider"/><TextViewandroid:id="@+id/title_bar_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:ellipsize="end"android:gravity="center"android:paddingLeft="75.0dip"android:paddingRight="75.0dip"android:singleLine="true"android:text="Title"android:textColor="#ffffff"android:textSize="22sp"/></RelativeLayout>

wKiom1OlT1Hx0ol3AAA31EuiTmo000.jpg按钮效果布局部分:

bitmap_page_category_bg.xml:<?xmlversion="1.0"encoding="utf-8"?><bitmapandroid:src="@drawable/ic_item_normal_bg"android:tileMode="repeat"xmlns:android="http://schemas.android.com/apk/res/android"/>selector_category_item.xml<?xmlversion="1.0"encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="true"android:drawable="@drawable/ic_item_selected_bg"/><itemandroid:state_focused="true"android:drawable="@drawable/ic_item_selected_bg"/><itemandroid:drawable="@drawable/ic_item_normal_bg"/></selector>

附效果图:

wKioL1OlT9qBMY8jAABpb3fnd5w155.jpg

wKiom1OlUAiBOj0eAAHJRLKNcIQ554.jpg


更多相关文章

  1. android三个选项的对话框
  2. 改变android 选项卡的背景色
  3. 调用android自带的截图工具进行截图
  4. 【Android系统源码修改】在系统设置中添加设置选项
  5. Android快速设置中添加隐藏状态栏和导航栏选项
  6. Android设置Settings:预读取设置的选项和更新设置结果【2】
  7. Android原生(Native)C开发:编译选项小结
  8. Android 使用WebView加载含有echarts的页面,截图不显示的解决方式
  9. android菜单详解二:选项菜单

随机推荐

  1. 2021北京高新技术企业申报难度及相关要求
  2. Java基础编程练习8:猜数字游戏
  3. Java基础编程练习7:百钱百鸡问题。
  4. Linux宝塔负载均衡使用教程
  5. javascript创建对象几种方式
  6. IE6 png 透明 (三种解决方法)
  7. 流行的javascript库介绍
  8. 作用域和闭包; 类与类的继承
  9. 技术问答-23 javabean创建一个二叉树,左
  10. Java 线程池 ThreadPoolExecutor -01