本文来自http://blog.csdn.net/hellogv/,引用必须注明出处!
在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的演示效果如下:
这个Panel控件可以轻易实现不同方向的抽屉效果,比SlidingDrawer有更强的扩展性!
在多次使用Panel的过程中,发现Panel有个bug,会间断性出现“闪烁”,也就是在onTouchListener里面的触发ACTION_DOWN后,抽屉瞬间弹出然后瞬间回收(版本日期为Feb 3, 2009)。把原Panel的OnTouchListener,即以下代码:
- OnTouchListenertouchListener=newOnTouchListener(){
- intinitX;
- intinitY;
- booleansetInitialPosition;
- publicbooleanonTouch(Viewv,MotionEventevent){
- if(mState==State.ANIMATING){
- //weareanimating
- returnfalse;
- }
- /Log.d(TAG,"state:"+mState+"x:"+event.getX()+"y:"+event.getY());
- intaction=event.getAction();
- if(action==MotionEvent.ACTION_DOWN){
- if(mBringToFront){
- bringToFront();
- }
- initX=0;
- initY=0;
- if(mContent.getVisibility()==GONE){
- //sincewemaynotknowcontentdimensionsweusefactorshere
- if(mOrientation==VERTICAL){
- initY=mPosition==TOP?-1:1;
- }else{
- initX=mPosition==LEFT?-1:1;
- }
- }
- setInitialPosition=true;
- }else{
- if(setInitialPosition){
- //nowweknowcontentdimensions,sowemultiplyfactors...
- initX*=mContentWidth;
- initY*=mContentHeight;
- //...andsetinitialpanel'sposition
- mGestureListener.setScroll(initX,initY);
- setInitialPosition=false;
- //foroffsetLocationwehavetoinvertvalues
- initX=-initX;
- initY=-initY;
- }
- //offseteveryACTION_MOVE&ACTION_UPevent
- event.offsetLocation(initX,initY);
- }
- if(!mGestureDetector.onTouchEvent(event)){
- if(action==MotionEvent.ACTION_UP){
- //tupupafterscrolling
- post(startAnimation);
- }
- }
- returnfalse;
- }
- };
替换为:
- OnTouchListenertouchListener=newOnTouchListener(){
- floattouchX,touchY;
- publicbooleanonTouch(Viewv,MotionEventevent){
- if(mState==State.ANIMATING){
- //weareanimating
- returnfalse;
- }
- intaction=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){
- //tupupafterscrolling
- intsize=(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);
- }
- }
- returnfalse;
- }
- };
即可修复这个bug,并且也同样实现了OnClickListener的功能,可以把原Panel的OnClickListener给删掉了!