RSS
 
以后地位 : 主页 > 刷机教程 >

Android开辟之多偏向抽屉

工夫:2015-08-24 16:44 阅读:

  在android上要完成相似Launch的抽屉结果,各人肯定起首会想起SlidingDrawer。SlidingDrawer是android官方控件之一,本文的配角不是它,而是官方的控件东西聚集android-misc-widgets。android-misc-widgets外面包括几个widget:Panel、SmoothButton、Switcher、VirtualKeyboard,另有一些动画殊效,本文次要引见抽屉容器Panel的用法。

  工程代码中Panel的演示结果如下:

  这个Panel控件可以随便完成差别偏向的抽屉结果,比SlidingDrawer有更强的扩展性!

  在屡次运用Panel的进程中,发明Panel有个bug,会连续性呈现“闪耀”,也便是在onTouchListener外面的触发ACTION_DOWN后,抽屉霎时弹出然后霎时接纳(版即日期为Feb 3, 2009)。把原Panel 的OnTouchListener,即以下代码:

  OnTouchListener touchListener = new OnTouchListener() { 
      int initX; 
      int initY; 
      boolean setInitialPosition; 
      public boolean onTouch(View v, MotionEvent event) { 
          if (mState == State.ANIMATING) { 
              // we are animating 
              return false; 
          } 
  /           Log.d(TAG, "state: " + mState + " x: " + event.getX() + " y: " + event.getY());

 
          int action = event.getAction(); 
          if (action == MotionEvent.ACTION_DOWN) { 
              if (mBringToFront) { 
                  bringToFront(); 
              } 
              initX = 0; 
              initY = 0; 
              if (mContent.getVisibility() == GONE) { 
                  // since we may not know content dimensions we use factors here 
                  if (mOrientation == VERTICAL) { 
                      initY = mPosition == TOP? -1 : 1; 
                  } else { 
                      initX = mPosition == LEFT? -1 : 1; 
                  } 
              } 
              setInitialPosition = true; 
          } else { 
              if (setInitialPosition) { 
                  // now we know content dimensions, so we multiply factors... 
                  initX *= mContentWidth; 
                  initY *= mContentHeight; 
                  // ... and set initial panel's position 
                  mGestureListener.setScroll(initX, initY); 
                  setInitialPosition = false; 
                  // for offsetLocation we have to invert values 
                  initX = -initX; 
                  initY = -initY; 
              } 
              // offset every ACTION_MOVE & ACTION_UP event  
              event.offsetLocation(initX, initY); 
          } 
          if (!mGestureDetector.onTouchEvent(event)) { 
              if (action == MotionEvent.ACTION_UP) { 
                  // tup up after scrolling 
                  post(startAnimation); 
              } 
          } 
          return false; 
      } 
  }; 

  交换为:

  OnTouchListener touchListener = new OnTouchListener() { 
      float touchX, touchY; 
    
      public boolean onTouch(View v, MotionEvent event) { 
          if (mState == State.ANIMATING) { 
              // we are animating 
              return false; 
          } 
 
          int action = event.getAction(); 
          if (action == MotionEvent.ACTION_DOWN) { 
              if (mBringToFront) { 
                  bringToFront(); 
              } 
              touchX = event.getX(); 
              touchY = event.getY(); 
          } 
 
          if (!mGestureDetector.onTouchEvent(event)) { 
              if (action == MotionEvent.ACTION_UP) { 
                  // tup up after scrolling 
                  int size = (int) (Math.abs(touchX - event.getX()) + Math 
                          .abs(touchY - event.getY())); 
 
                  if (size == mContentWidth || size == mContentHeight) { 
                      mState = State.ABOUT_TO_ANIMATE; 
                      //Log.e("size", String.valueOf(size)); 
                      //Log.e(String.valueOf(mContentWidth),String.valueOf(mContentHeight)); 
                  } 
 
                  post(startAnimation); 
              } 
          } 
          return false; 
      } 
  }; 

  即可修复这个bug,而且也异样完成了OnClickListener的功用,可以把原Panel的OnClickListener给删失了!