Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android提高21篇之十九:“多方向”抽屜

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

編輯:關於android開發

       在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給刪掉了!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved