在android上要实现类似Launch的抽屉效果,大家一定首先会想起SlidingDrawer。SlidingDrawer是android官方控件之一,本文的主角不是它,而是民间的控件工具集合~~~android-misc-widgets。android-misc-widgets里面包含几个widget:Panel、SmoothButton、Switcher、VirtualKeyboard,还有一些动画特效,本文主要介绍抽屉容器Panel的用法。android-misc-widgets的google工程地址:-widgets/http://code.google.com/p/android-misc,工程代码中Panel的演示效果如下:

Android提高21篇之十九:“多方向”抽屉

  这个Panel控件可以轻易实现不同方向的抽屉效果,比SlidingDrawer有更强的扩展性!

  在多次使用Panel的过程中,发现Panel有个bug,会间断性出现“闪烁”,也就是在onTouchListener里面的触发ACTION_DOWN后,抽屉瞬间弹出然后瞬间回收(版本日期为Feb 3, 2009)。把原Panel的OnTouchListener,即以下代码:

Java代码
  1. OnTouchListener touchListener = new OnTouchListener() {  
  2.                int initX;  
  3.                int initY;  
  4.                boolean setInitialPosition;  
  5.                public boolean onTouch(View v, MotionEvent event) {  
  6.                        if (mState == State.ANIMATING) {  
  7.                                // we are animating  
  8.                                return false;  
  9.                        }  
  10. /                        Log.d(TAG, "state: " + mState + " x: " + event.getX() + " y: " + event.getY());  
  11.                        int action = event.getAction();  
  12.                        if (action == MotionEvent.ACTION_DOWN) {  
  13.                                if (mBringToFront) {  
  14.                                        bringToFront();  
  15.                                }  
  16.                                initX = 0;  
  17.                                initY = 0;  
  18.                                if (mContent.getVisibility() == GONE) {  
  19.                                        // since we may not know content dimensions we use factors here  
  20.                                        if (mOrientation == VERTICAL) {  
  21.                                                initY = mPosition == TOP? -1 : 1;  
  22.                                        } else {  
  23.                                                initX = mPosition == LEFT? -1 : 1;  
  24.                                        }  
  25.                                }  
  26.                                setInitialPosition = true;  
  27.                        } else {  
  28.                                if (setInitialPosition) {  
  29.                                        // now we know content dimensions, so we multiply factors...  
  30.                                        initX *= mContentWidth;  
  31.                                        initY *= mContentHeight;  
  32.                                        // ... and set initial panel's position  
  33.                                        mGestureListener.setScroll(initX, initY);  
  34.                                        setInitialPosition = false;  
  35.                                        // for offsetLocation we have to invert values  
  36.                                        initX = -initX;  
  37.                                        initY = -initY;  
  38.                                }  
  39.                                // offset every ACTION_MOVE & ACTION_UP event   
  40.                                event.offsetLocation(initX, initY);  
  41.                        }  
  42.                        if (!mGestureDetector.onTouchEvent(event)) {  
  43.                                if (action == MotionEvent.ACTION_UP) {  
  44.                                        // tup up after scrolling  
  45.                                        post(startAnimation);  
  46.                                }  
  47.                        }  
  48.                        return false;  
  49.                }  
  50.        };  

  替换为:

Java代码
  1. OnTouchListener touchListener = new OnTouchListener() {  
  2.                float touchX, touchY;  
  3.   
  4.                public boolean onTouch(View v, MotionEvent event) {  
  5.                        if (mState == State.ANIMATING) {  
  6.                                // we are animating  
  7.                                return false;  
  8.                        }  
  9.   
  10.                        int action = event.getAction();  
  11.                        if (action == MotionEvent.ACTION_DOWN) {  
  12.                                if (mBringToFront) {  
  13.                                        bringToFront();  
  14.                                }  
  15.                                touchX = event.getX();  
  16.                                touchY = event.getY();  
  17.                        }  
  18.   
  19.                        if (!mGestureDetector.onTouchEvent(event)) {  
  20.                                if (action == MotionEvent.ACTION_UP) {  
  21.                                        // tup up after scrolling  
  22.                                        int size = (int) (Math.abs(touchX - event.getX()) + Math  
  23.                                                        .abs(touchY - event.getY()));  
  24.   
  25.                                        if (size == mContentWidth || size == mContentHeight) {  
  26.                                                mState = State.ABOUT_TO_ANIMATE;  
  27.                                                //Log.e("size", String.valueOf(size));  
  28.                                                //Log.e(String.valueOf(mContentWidth),String.valueOf(mContentHeight));  
  29.                                        }  
  30.   
  31.                                        post(startAnimation);  
  32.                                }  
  33.                        }  
  34.                        return false;  
  35.                }  
  36.        };  

  即可修复这个bug,并且也同样实现了OnClickListener的功能,可以把原Panel的OnClickListener给删掉了!

本文发布:Android开发网
本文地址:http://www.jizhuomi.com/android/course/514.html
2015年12月17日
发布:鸡啄米 分类:Android开发教程 浏览: 评论:0