抽屉效果

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

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

这是上下拉抽屉的效果,将 SlidingDrawer属性设置为android:orientation="vertical"即可
抽屉效果_第1张图片 抽屉效果_第2张图片


这是左右拉抽屉的效果,将 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。



上面例子的布局文件:

[html] view plain copy print ?
  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>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="效果显示:" /> <SlidingDrawer android:id="@+id/drawer1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:handle="@+id/layout1" android:content="@+id/mycontent1"> <LinearLayout android:id="@id/layout1" android:orientation="vertical" android:layout_width="35sp" android:layout_height="wrap_content" android:gravity="center"> <ImageView android:id="@+id/myImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/open" /> </LinearLayout> <GridView android:id="@id/mycontent1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="2" android:gravity="center" /> </SlidingDrawer> </LinearLayout> </FrameLayout>


java文件:

[java] view plain copy print ?
  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. }
import android.app.Activity; import android.os.Bundle; import android.widget.GridView; import android.widget.ImageView; import android.widget.SlidingDrawer; import android.widget.SlidingDrawer.OnDrawerCloseListener; import android.widget.SlidingDrawer.OnDrawerOpenListener; import android.widget.SlidingDrawer.OnDrawerScrollListener; public class SlidingDrawerActivity extends Activity { /** Called when the activity is first created. */ private SlidingDrawer sd; private GridView gv; private ImageView iv; private int [] itemIcons = new int [] { R.drawable.alarm, R.drawable.calendar, R.drawable.camera, R.drawable.clock, R.drawable.music, R.drawable.tv }; private String[] itemString = new String[]{"Alarm","Calendar","camera","clock","music","tv"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.slidrawer); init(); GridAdapter adapter = new GridAdapter(SlidingDrawerActivity.this,itemString,itemIcons); gv.setAdapter(adapter); /* 设定SlidingDrawer被打开的事件处理 */ sd.setOnDrawerOpenListener(new OnDrawerOpenListener() { @Override public void onDrawerOpened() { // TODO Auto-generated method stub iv.setImageResource(R.drawable.close); } }); /* 设定SlidingDrawer被关闭的事件处理 */ sd.setOnDrawerCloseListener(new OnDrawerCloseListener() { public void onDrawerClosed() { iv.setImageResource(R.drawable.open); } }); sd.setOnDrawerScrollListener(new OnDrawerScrollListener() { @Override public void onScrollStarted() { // TODO Auto-generated method stub System.out.println("start"); } @Override public void onScrollEnded() { // TODO Auto-generated method stub System.out.println("end"); } }); } private void init(){ sd = (SlidingDrawer) findViewById(R.id.drawer1); gv = (GridView) findViewById(R.id.mycontent1); iv = (ImageView) findViewById(R.id.myImage); } }



[java] view plain copy print ?
  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. }
package com.shao.slider; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class GridAdapter extends BaseAdapter { private Context context; private String[] itemString; private int[] itemIcons; public GridAdapter(Context con,String[] itemString,int[] itemIcons){ context = con; this.itemString = itemString; this.itemIcons = itemIcons; } @Override public int getCount() { // TODO Auto-generated method stub return itemIcons.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return itemString[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LayoutInflater inflater = LayoutInflater.from(context); /* 使用item.xml为每几个item的Layout */ View v = inflater.inflate(R.layout.item, null); /* 取得View */ ImageView iv = (ImageView) v.findViewById(R.id.item_grid); TextView tv = (TextView) v.findViewById(R.id.item_text); /* 设定显示的Image与文? */ iv.setImageResource(itemIcons[position]); tv.setText(itemString[position]); return v; } }

还有注意的是:

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

抽屉效果_第3张图片  

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

下面是自定义组建的实现代码  
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的源码:  
  • CustomSlidingDrawer.rar (140.3 KB)
  • 下载次数: 356
  • 查看图片附件

 

 

你可能感兴趣的:(抽屉效果)