在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
這個Panel控件能夠輕易實現不一樣方向的抽屜效果,比SlidingDrawer有更強的擴展性! 工具
在屢次使用Panel的過程當中,發現Panel有個bug,會間斷性出現「閃爍」,也就是在onTouchListener裏面的觸發ACTION_DOWN後,抽屜瞬間彈出而後瞬間回收(版本日期爲Feb 3, 2009)。把原Panel的OnTouchListener,即如下代碼: post
01 |
OnTouchListener touchListener = new OnTouchListener() { |
04 |
boolean setInitialPosition; |
05 |
public boolean onTouch(View v, MotionEvent event) { |
06 |
if (mState == State.ANIMATING) { |
10 |
// Log.d(TAG, "state: " + mState + " x: " + event.getX() + " y: " + event.getY()); |
11 |
int action = event.getAction(); |
12 |
if (action == MotionEvent.ACTION_DOWN) { |
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; |
23 |
initX = mPosition == LEFT? -1 : 1; |
26 |
setInitialPosition = true; |
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 |
39 |
// offset every ACTION_MOVE & ACTION_UP event |
40 |
event.offsetLocation(initX, initY); |
42 |
if (!mGestureDetector.onTouchEvent(event)) { |
43 |
if (action == MotionEvent.ACTION_UP) { |
44 |
// tup up after scrolling |
替換爲: 動畫
01 |
OnTouchListener touchListener = new OnTouchListener() { |
04 |
public boolean onTouch(View v, MotionEvent event) { |
05 |
if (mState == State.ANIMATING) { |
10 |
int action = event.getAction(); |
11 |
if (action == MotionEvent.ACTION_DOWN) { |
15 |
touchX = event.getX(); |
16 |
touchY = event.getY(); |
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())); |
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)); |
便可修復這個bug,而且也一樣實現了OnClickListener的功能,能夠把原Panel的OnClickListener給刪掉了! google
原文出處:http://blog.csdn.net/hellogv/article/details/6264706 spa