欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android提高之多方向抽屉实现方法

程序员文章站 2022-11-05 15:04:26
说起在android上要实现类似launch的抽屉效果,大家一定首先会想起slidingdrawer。slidingdrawer是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提高之多方向抽屉实现方法

这个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给删掉了!

希望本文所述实例对于大家进行android项目开发能有所帮助。