当前位置: 移动技术网 > IT编程>移动开发>Android > Android提高之多方向抽屉实现方法

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

悠悠濮阳,柳州刘和平,尹施允上快乐大本营

说起在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加以替换,即以下代码:

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项目开发能有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网