你好,游客 登录 注册 搜索
背景:
阅读新闻

Android仿iPhone滚轮控件实现及源码分析

[日期:2012-06-18] 来源:Linux社区  作者:chengxibeauty [字体:大 中 小]

主要就是通过canvas类进行图形的绘制。

最后,我们看下840行定义的手势监听:

  1. // gesture listener   
  2.     private SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() {  
  3.         public boolean onDown(MotionEvent e) {  
  4.             if (isScrollingPerformed) {  
  5.                 scroller.forceFinished(true);  
  6.                 clearMessages();  
  7.                 return true;  
  8.             }  
  9.             return false;  
  10.         }  
  11.           
  12.         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
  13.             startScrolling();  
  14.             doScroll((int)-distanceY);  
  15.             return true;  
  16.         }  
  17.           
  18.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
  19.             lastScrollY = currentItem * getItemHeight() + scrollingOffset;  
  20.             int maxY = isCyclic ? 0x7FFFFFFF : adapter.getItemsCount() * getItemHeight();  
  21.             int minY = isCyclic ? -maxY : 0;  
  22.             scroller.fling(0, lastScrollY, 0, (int) -velocityY / 2, 0, 0, minY, maxY);  
  23.             setNextMessage(MESSAGE_SCROLL);  
  24.             return true;  
  25.         }  
  26.     };  

里面主要调用的方法:clearMessages()、startScrolling()、doScroll()、setNextMessage(),先看下中间的两个方法开始滑动和滑动

  1. /** 
  2.      * Scrolls the wheel 
  3.      * @param delta the scrolling value 
  4.      */  
  5.     private void doScroll(int delta) {  
  6.         scrollingOffset += delta;         
  7.         int count = scrollingOffset / getItemHeight();  
  8.         int pos = currentItem - count;  
  9.         if (isCyclic && adapter.getItemsCount() > 0) {  
  10.             // fix position by rotating   
  11.             while (pos < 0) {  
  12.                 pos += adapter.getItemsCount();  
  13.             }  
  14.             pos %= adapter.getItemsCount();  
  15.         } else if (isScrollingPerformed) {  
  16.             //    
  17.             if (pos < 0) {  
  18.                 count = currentItem;  
  19.                 pos = 0;  
  20.             } else if (pos >= adapter.getItemsCount()) {  
  21.                 count = currentItem - adapter.getItemsCount() + 1;  
  22.                 pos = adapter.getItemsCount() - 1;  
  23.             }  
  24.         } else {  
  25.             // fix position   
  26.             pos = Math.max(pos, 0);  
  27.             pos = Math.min(pos, adapter.getItemsCount() - 1);  
  28.         }  
  29.           
  30.         int offset = scrollingOffset;  
  31.         if (pos != currentItem) {  
  32.             setCurrentItem(pos, false);  
  33.         } else {  
  34.             invalidate();  
  35.         }  
  36.           
  37.         // update offset   
  38.         scrollingOffset = offset - count * getItemHeight();  
  39.         if (scrollingOffset > getHeight()) {  
  40.             scrollingOffset = scrollingOffset % getHeight() + getHeight();  
  41.         }  
  42.     }  

 

  1. /** 
  2.      * Starts scrolling 
  3.      */  
  4.     private void startScrolling() {  
  5.         if (!isScrollingPerformed) {  
  6.             isScrollingPerformed = true;  
  7.             notifyScrollingListenersAboutStart();  
  8.         }  
  9.     }  
linux
相关资讯       Android源码 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款