android 之slidingmenu控件 (直接封装成了jar )




       相信大家都使用过这些功能,也相信都见过这样的布局Slidingmenu,开始在看到这样的布局的时候,相信大多数人的做法都是通过动画实现的。这样的设计是比较合情合理的。
        相信大家只要是开发android的肯定也了解https://github.com这个平台,而我要说的这个Slidingmenu就是出自该平台的Jfeinstein10所创。而我只是将他的杰作封装了一下,打了一个jar包,将该布局作为一个控件的形式体现出来。在google上搜索Slidingmenu就可以发现该控件的信息,但使用的方法却较为繁琐,需要:
  • 先导入ABS项目工程
  • 再导入library工程,而且library工程需要添加ABS的依赖。
  • 随后才可以新建自己工程,而且自己的工程还需要依赖library工程。
     这样的做法实在是很麻烦,很费劲。然后我在开发项目时需要该控件的使用,当时我就从网上搜了一下,其实这类的封装还是很多的,但是或多或少都有些缺陷,有的是通过动画实现的,有的是通过线程休眠实现的动画效果的。当然都可以实现这样的效果,但是感觉总体来说体验效果还不够好。而且灵活性不够,就那左右滑动来说吧,有的demo的例子就是左滑,但是你若想用右滑的话还要自己修改代码。这也感觉很不灵活。但是Jfeinstein10封装的却很到位,像这样的配置都可以通过配置设置。相关该控件的详细资料可参考https://github.com/jfeinstein10/SlidingMenu。
     我就只把他的属性配置给拷贝过来。
    
  • viewAbove - a reference to the layout that you want to use as the above view of the SlidingMenu
  • viewBehind - a reference to the layout that you want to use as the behind view of the SlidingMenu
  • touchModeAbove - an enum that designates what part of the screen is touchable when the above view is showing. Margin means only the left margin. Fullscreen means the entire screen. Default is margin.
  • behindOffset - a dimension representing the number of pixels that you want the above view to show when thebehind view is showing. Default is 0.
  • behindWidth - a dimension representing the width of the behind view. Default is the width of the screen(equivalent to behindOffset = 0).
  • behindScrollScale - a float representing the relationship between the above view scrolling and the behindbehind view scrolling. If set to 0.5f, the behind view will scroll 1px for every 2px that the above view scrolls.If set to 1.0f, the behind view will scroll 1px for every 1px that the above view scrolls. And if set to 0.0f, thebehind view will never scroll; it will be static. This one is fun to play around with. Default is 0.25f.
  • shadowDrawable - a reference to a drawable to be used as a drop shadow from the above view onto the below view.Default is no shadow for now.
  • shadowWidth - a dimension representing the width of the shadow drawable. Default is 0.
  • fadeEnabled - a boolean representing whether or not the behind view should fade when the SlidingMenu is closingand "un-fade" when opening
  • fadeDegree - a float representing the "amount" of fade.1.0f would mean fade all the way to black when theSlidingMenu is closed.0.0f would mean do not fade at all.
  • selectorEnabled - a boolean representing whether or not a selector should be drawn on the left side of the aboveview showing a selected view on the behind view.
  • selectorDrawable - a reference to a drawable to be used as the selectorNOTE : in order to have the selector drawn, you must call SlidingMenu.setSelectedView(View v) with the selected view.Note that this will most likely not work with items in a ListView because of the way that Android recycles item views.

   
接下来谈一下我封装的library。
        需要做的只需要将包导入项目即可。

     看一下如何用代码:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          final SlidingMenu menu = new SlidingMenu(this);//初始化组件
            Button button = new Button(this);//添加一个不同用于控制menu
            button.setText("mybutton");
            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                     menu.toggle();//滑动的方法
                }
            });
            RelativeLayout view = new RelativeLayout(this);
            view.addView(button);
            setContentView(view);//主视图
            
            menu.setMode(SlidingMenu.LEFT);//设置菜单的位置
            menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//设置菜单滑动的样式
            menu.setShadowWidthRes(R.dimen.shadow_width);
            menu.setShadowDrawable(R.drawable.shadow);//菜单滑动时阴影部分
            menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
            menu.setBehindWidth(200);//菜单宽带
            menu.setFadeDegree(0.35f);
            menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
            menu.setMenu(R.layout.activity_main);//添加菜单
    }

就这些代码即可实现,而且效果可以通知自己的配置随便设置。需要注意的是下面的参数是通过配置文件控制的                    menu.setShadowWidthRes(R.dimen.shadow_width);
            menu.setShadowDrawable(R.drawable.shadow);//菜单滑动时阴影部分
            menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
      使用时请注意:menu菜单的视图通过menu.setMenu控制。而左侧的视图就是直接通过setContentView控制的。

好了分享一下library:
    http://download.csdn.net/detail/woaixinxin123/5710131  [无需积分]
分享一下demo:
    http://download.csdn.net/detail/woaixinxin123/5710155   [积分3]

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 最近发现只单纯的左右滑动已经满足不了人们的欲望了。我研究了下源码发现在我封装的这个jar里也可以使用左右一块换到的效果。

左右滑动代码展示:

@Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

          final SlidingMenu menu = new SlidingMenu(this);

            Button button = new Button(this);

            button.setText("left");

            button.setOnClickListener(new OnClickListener() {

                @Override

                public void onClick(View v) {

                     menu.showMenu();

                }

            });

            

            Button button2 = new Button(this);

            button2.setText("right");

            button2.setOnClickListener(new OnClickListener() {

                @Override

                public void onClick(View v) {

                     menu.showSecondaryMenu();

                }

            });

            RelativeLayout view = new RelativeLayout(this);

            LayoutParams llp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

            llp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

            view.addView(button,llp);

            LayoutParams rlp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

            rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);

            view.addView(button2,rlp);

            setContentView(view);

            

            menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

            menu.setShadowWidthRes(R.dimen.shadow_width);

            menu.setShadowDrawable(R.drawable.shadow);

            menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);

            menu.setBehindWidth(200);

            menu.setFadeDegree(0.35f);

            menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

            menu.setMenu(R.layout.activity_main);

            

            menu.setMode(SlidingMenu.LEFT_RIGHT);

            menu.setSecondaryMenu(R.layout.activity_main);

            menu.setSecondaryShadowDrawable(R.drawable.shadow);

            menu.setShadowDrawable(R.drawable.shadow);

            

            

    }

为了更方便的使用该控件我再次写了一下demo,分为两个activity一个是左右一块滑动的,一个是单方向滑动的。看不懂的可以QQ联系我。

http://download.csdn.net/detail/woaixinxin123/5883503 【3积分】



你没有理由不喜欢,除非你不需要。本人android,web开发                                                   QQ群255825960
                                                                                                                                                                null
                                                                                                                                                             2013/7/6


评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值