抽屉效果

SlideringDrawer的使用(抽屉效果)http://www.itlanbao.com/code/20150825/10000/100476.html

一 SlidingDrawer 这个类,也就是所谓的"抽屉"类。它的用法很简单,要包括handle ,和content .
handle 就是当你点击它的时候,content 要么抽抽屉要么关抽屉。

这是上下拉抽屉的效果,将 SlidingDrawer属性设置为android:orientation="vertical"即可


这是左右拉抽屉的效果,将 SlidingDrawer属性设置为android:orientation="horizontal"即可。




二、重要属性

  android:allowSingleTap:指示是否可以通过handle打开或关闭

  android:animateOnClick:指示是否当使用者按下手柄打开/关闭时是否该有一个动画。

  android:content:隐藏的内容

  android:handle:handle(手柄)

  三、重要方法

  animateClose():关闭时实现动画。

close():即时关闭

  getContent():获取内容

  isMoving():指示SlidingDrawer是否在移动。

  isOpened():指示SlidingDrawer是否已全部打开

  lock():屏蔽触摸事件。

  setOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener):SlidingDrawer关闭时调用

  unlock():解除屏蔽触摸事件。

  toggle():切换打开和关闭的抽屉SlidingDrawer。



上面例子的布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:orientation="vertical" android:layout_width="fill_parent"
  7. android:layout_height="fill_parent">
  8. <TextView android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="效果显示:" />
  11. <SlidingDrawer android:id="@+id/drawer1"
  12. android:layout_width="fill_parent"
  13. android:layout_height="fill_parent"
  14. android:orientation="horizontal"
  15. android:handle="@+id/layout1"
  16. android:content="@+id/mycontent1">
  17. <LinearLayout android:id="@id/layout1"
  18. android:orientation="vertical"
  19. android:layout_width="35sp"
  20. android:layout_height="wrap_content"
  21. android:gravity="center">
  22. <ImageView android:id="@+id/myImage"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:src="@drawable/open" />
  26. </LinearLayout>
  27. <GridView android:id="@id/mycontent1" android:layout_width="fill_parent"
  28. android:layout_height="fill_parent" android:numColumns="2"
  29. android:gravity="center" />
  30. </SlidingDrawer>
  31. </LinearLayout>
  32. </FrameLayout>


java文件:

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.widget.GridView;
  4. import android.widget.ImageView;
  5. import android.widget.SlidingDrawer;
  6. import android.widget.SlidingDrawer.OnDrawerCloseListener;
  7. import android.widget.SlidingDrawer.OnDrawerOpenListener;
  8. import android.widget.SlidingDrawer.OnDrawerScrollListener;
  9. public class SlidingDrawerActivity extends Activity {
  10. /** Called when the activity is first created. */
  11. private SlidingDrawer sd;
  12. private GridView gv;
  13. private ImageView iv;
  14. private int [] itemIcons = new int []
  15. { R.drawable.alarm, R.drawable.calendar, R.drawable.camera, R.drawable.clock, R.drawable.music, R.drawable.tv };
  16. private String[] itemString = new String[]{"Alarm","Calendar","camera","clock","music","tv"};
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.slidrawer);
  21. init();
  22. GridAdapter adapter = new GridAdapter(SlidingDrawerActivity.this,itemString,itemIcons);
  23. gv.setAdapter(adapter);
  24. /* 设定SlidingDrawer被打开的事件处理 */
  25. sd.setOnDrawerOpenListener(new OnDrawerOpenListener() {
  26. @Override
  27. public void onDrawerOpened() {
  28. // TODO Auto-generated method stub
  29. iv.setImageResource(R.drawable.close);
  30. }
  31. });
  32. /* 设定SlidingDrawer被关闭的事件处理 */
  33. sd.setOnDrawerCloseListener(new OnDrawerCloseListener()
  34. {
  35. public void onDrawerClosed()
  36. {
  37. iv.setImageResource(R.drawable.open);
  38. }
  39. });
  40. sd.setOnDrawerScrollListener(new OnDrawerScrollListener() {
  41. @Override
  42. public void onScrollStarted() {
  43. // TODO Auto-generated method stub
  44. System.out.println("start");
  45. }
  46. @Override
  47. public void onScrollEnded() {
  48. // TODO Auto-generated method stub
  49. System.out.println("end");
  50. }
  51. });
  52. }
  53. private void init(){
  54. sd = (SlidingDrawer) findViewById(R.id.drawer1);
  55. gv = (GridView) findViewById(R.id.mycontent1);
  56. iv = (ImageView) findViewById(R.id.myImage);
  57. }
  58. }



  1. package com.shao.slider;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.BaseAdapter;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9. public class GridAdapter extends BaseAdapter {
  10. private Context context;
  11. private String[] itemString;
  12. private int[] itemIcons;
  13. public GridAdapter(Context con,String[] itemString,int[] itemIcons){
  14. context = con;
  15. this.itemString = itemString;
  16. this.itemIcons = itemIcons;
  17. }
  18. @Override
  19. public int getCount() {
  20. // TODO Auto-generated method stub
  21. return itemIcons.length;
  22. }
  23. @Override
  24. public Object getItem(int position) {
  25. // TODO Auto-generated method stub
  26. return itemString[position];
  27. }
  28. @Override
  29. public long getItemId(int position) {
  30. // TODO Auto-generated method stub
  31. return position;
  32. }
  33. @Override
  34. public View getView(int position, View convertView, ViewGroup parent) {
  35. // TODO Auto-generated method stub
  36. LayoutInflater inflater = LayoutInflater.from(context);
  37. /* 使用item.xml为每几个item的Layout */
  38. View v = inflater.inflate(R.layout.item, null);
  39. /* 取得View */
  40. ImageView iv = (ImageView) v.findViewById(R.id.item_grid);
  41. TextView tv = (TextView) v.findViewById(R.id.item_text);
  42. /* 设定显示的Image与文? */
  43. iv.setImageResource(itemIcons[position]);
  44. tv.setText(itemString[position]);
  45. return v;
  46. }
  47. }

还有注意的是:

SlidingDrawer should be used as an overlay inside layouts. This means SlidingDrawer should only be used inside of a FrameLayout or a RelativeLayout for instance,如果显示的时候不正常,考虑上面的原因。

 

 

 

android仿腾讯安全管家首页抽屉效果

转载请说明出处  
最近在做公司新产品的设计,看到腾讯安全管家首页的抽屉效果设计的挺不错,一方面可以讲经常使用的功能模块直接显示给用户,另一方面将用户不常用的功能模块隐藏起来,而这些功能模块的显示和隐藏可以通过一个抽屉组建实现。所以我们想将这个设计理念加入到我们的产品中。腾讯安全管家效果图如下:  

 

虽然android 文档中向我们提供了一个叫SlidingDrawer的抽屉组建,但是这个组建的使用限制比较多,也实现不了我们想要的效果。故到网上搜了一会,也没看到有开发者写这样的组建。所以只能靠自己了,但是在网上还是看到了一下有价值的参考案例。  
不费话了,直接上实现后的效果图:  
 

下面是自定义组建的实现代码  
Java代码    收藏代码
  1. import android.content.Context;  
  2. import android.os.AsyncTask;  
  3. import android.util.Log;  
  4. import android.view.GestureDetector;  
  5. import android.view.MotionEvent;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.LinearLayout;  
  9.   
  10. public class CustomSlidingDrawer extends LinearLayout implements GestureDetector.OnGestureListener, View.OnTouchListener{  
  11.     private final static String TAG = "CustomSlidingDrawer";  
  12.       
  13.     private Button btnHandler;  
  14.     private LinearLayout container;  
  15.     private int mBottonMargin=0;  
  16.     private GestureDetector mGestureDetector;  
  17.     private boolean mIsScrolling=false;  
  18.     private float mScrollY;  
  19.     private int moveDistance;  
  20.       
  21.     public CustomSlidingDrawer(Context context,View otherView,int width,int height, int hideDistance) {  
  22.         super(context);  
  23.         moveDistance = hideDistance;  
  24.         //定义手势识别  
  25.         mGestureDetector = new GestureDetector(context,this);  
  26.         mGestureDetector.setIsLongpressEnabled(false);  
  27.           
  28.         //改变CustomSlidingDrawer附近组件的属性  
  29.         LayoutParams otherLP=(LayoutParams) otherView.getLayoutParams();  
  30.         //这一步很重要,要不组建不会显示  
  31.         otherLP.weight=1;  
  32.         otherView.setLayoutParams(otherLP);  
  33.           
  34.         //设置CustomSlidingDrawer本身的属性  
  35.         LayoutParams lp=new LayoutParams(width, height);  
  36.         lp.bottomMargin = -moveDistance;  
  37.         mBottonMargin=Math.abs(lp.bottomMargin);  
  38.         this.setLayoutParams(lp);  
  39.         this.setOrientation(LinearLayout.VERTICAL);  
  40.         //this.setBackgroundColor(Color.BLUE);  
  41.           
  42.         //设置Handler的属性  
  43.         btnHandler=new Button(context);  
  44.         btnHandler.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 35));  
  45.         btnHandler.setOnTouchListener(this);  
  46.         this.addView(btnHandler);  
  47.           
  48.         //设置Container的属性  
  49.         container=new LinearLayout(context);  
  50.         //container.setBackgroundColor(Color.GREEN);  
  51.         container.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,  
  52.                 LayoutParams.MATCH_PARENT));  
  53.         this.addView(container);  
  54.     }  
  55.       
  56.     /** 
  57.      * 把View放在CustomSlidingDrawer的Container 
  58.      * @param v 
  59.      */  
  60.     public void fillPanelContainer(View v)  
  61.     {  
  62.         container.addView(v);  
  63.     }  
  64.       
  65.     /** 
  66.      * 异步移动CustomSlidingDrawer 
  67.      */  
  68.     class AsynMove extends AsyncTask<Integer, Integer, Void> {  
  69.         @Override  
  70.         protected Void doInBackground(Integer... params) {  
  71.             Log.e(TAG, "AsynMove doInBackground");  
  72.             int times;  
  73.             if (mBottonMargin % Math.abs(params[0]) == 0)// 整除  
  74.                 times = mBottonMargin / Math.abs(params[0]);  
  75.             else  
  76.                 // 有余数  
  77.                 times = mBottonMargin / Math.abs(params[0]) + 1;  
  78.             for (int i = 0; i < times; i++) {  
  79.                 publishProgress(params);  
  80.             }  
  81.             return null;  
  82.         }  
  83.         @Override  
  84.         protected void onProgressUpdate(Integer... params) {  
  85.             Log.e(TAG, "AsynMove onProgressUpdate");  
  86.             LayoutParams lp = (LayoutParams) CustomSlidingDrawer.this.getLayoutParams();  
  87.             if (params[0] < 0)  
  88.                 lp.bottomMargin = Math.max(lp.bottomMargin + params[0],  
  89.                         (-mBottonMargin));  
  90.             else  
  91.                 lp.bottomMargin = Math.min(lp.bottomMargin + params[0], 0);  
  92.             CustomSlidingDrawer.this.setLayoutParams(lp);  
  93.         }  
  94.     }  
  95.       
  96.     @Override  
  97.     public boolean onDown(MotionEvent e) {  
  98.         mScrollY=0;  
  99.         mIsScrolling=false;  
  100.         return false;  
  101.     }  
  102.       
  103.     @Override  
  104.     public boolean onSingleTapUp(MotionEvent e) {  
  105.         LayoutParams lp = (LayoutParams) CustomSlidingDrawer.this.getLayoutParams();  
  106.         if (lp.bottomMargin < 0)  
  107.             new AsynMove().execute(new Integer[] { moveDistance });// 正数展开  
  108.         else if (lp.bottomMargin >= 0)  
  109.             new AsynMove().execute(new Integer[] { -moveDistance });// 负数收缩  
  110.         return false;  
  111.     }  
  112.       
  113.     @Override  
  114.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  115.             float distanceY) {  
  116.         mIsScrolling=true;  
  117.         mScrollY+=distanceY;  
  118.         LayoutParams lp=(LayoutParams) CustomSlidingDrawer.this.getLayoutParams();  
  119.         if (lp.bottomMargin < -1 && mScrollY > 0) {//往上拖拉  
  120.             lp.bottomMargin = Math.min((lp.bottomMargin + (int) mScrollY),0);  
  121.             CustomSlidingDrawer.this.setLayoutParams(lp);  
  122.         } else if (lp.bottomMargin > -(mBottonMargin) && mScrollY < 0) {//往下拖拉  
  123.             lp.bottomMargin = Math.max((lp.bottomMargin + (int) mScrollY),-mBottonMargin);  
  124.             CustomSlidingDrawer.this.setLayoutParams(lp);  
  125.         }  
  126.         return false;  
  127.     }  
  128.       
  129.     @Override  
  130.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  131.             float velocityY) {return false;}  
  132.     @Override  
  133.     public void onLongPress(MotionEvent e) {}  
  134.     @Override  
  135.     public void onShowPress(MotionEvent e) {}  
  136.       
  137.     @Override  
  138.     public boolean onTouch(View v, MotionEvent event) {  
  139.         if(event.getAction()==MotionEvent.ACTION_UP && //onScroll时的ACTION_UP  
  140.                 mIsScrolling==true)  
  141.         {  
  142.             LayoutParams lp=(LayoutParams) CustomSlidingDrawer.this.getLayoutParams();  
  143.             if (lp.bottomMargin >= (-mBottonMargin/2)) {//往上超过一半  
  144.                 new AsynMove().execute(new Integer[] { moveDistance });// 正数展开  
  145.             }   
  146.             else if (lp.bottomMargin < (-mBottonMargin/2)) {//往下拖拉  
  147.                 new AsynMove().execute(new Integer[] { -moveDistance });// 负数收缩  
  148.             }  
  149.         }  
  150.         return mGestureDetector.onTouchEvent(event);   
  151.     }  
  152. }  


下面是demo的源码:  

style="font: 12px/18px Helvetica, Tahoma, Arial, sans-serif; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; widows: 1; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px;" height="90" src="http://ff20081528.iteye.com/iframe_ggbd/187" frameborder="0" width="680" scrolling="no">

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值