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

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

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

在这里使用的是典型的回调方法模式。

然后现在,我们进入WheelView类,看一下他是如何构建,首先,WheelView继承了View类。代码的22行到45行是导入的所需要的类。从54行到135行是声明一些变量和类:

  1. /** Scrolling duration */  
  2.     private static final int SCROLLING_DURATION = 400;  
  3.   
  4.     /** Minimum delta for scrolling */  
  5.     private static final int MIN_DELTA_FOR_SCROLLING = 1;  
  6.   
  7.     /** Current value & label text color */  
  8.     private static final int VALUE_TEXT_COLOR = 0xF0000000;  
  9.   
  10.     /** Items text color */  
  11.     private static final int ITEMS_TEXT_COLOR = 0xFF000000;  
  12.   
  13.     /** Top and bottom shadows colors */  
  14.     private static final int[] SHADOWS_COLORS = new int[] { 0xFF111111,  
  15.             0x00AAAAAA, 0x00AAAAAA };  
  16.   
  17.     /** Additional items height (is added to standard text item height) */  
  18.     private static final int ADDITIONAL_ITEM_HEIGHT = 15;  
  19.   
  20.     /** Text size */  
  21.     private static final int TEXT_SIZE = 24;  
  22.   
  23.     /** Top and bottom items offset (to hide that) */  
  24.     private static final int ITEM_OFFSET = TEXT_SIZE / 5;  
  25.   
  26.     /** Additional width for items layout */  
  27.     private static final int ADDITIONAL_ITEMS_SPACE = 10;  
  28.   
  29.     /** Label offset */  
  30.     private static final int LABEL_OFFSET = 8;  
  31.   
  32.     /** Left and right padding value */  
  33.     private static final int PADDING = 10;  
  34.   
  35.     /** Default count of visible items */  
  36.     private static final int DEF_VISIBLE_ITEMS = 5;  
  37.   
  38.     // Wheel Values   
  39.     private WheelAdapter adapter = null;  
  40.     private int currentItem = 0;  
  41.       
  42.     // Widths   
  43.     private int itemsWidth = 0;  
  44.     private int labelWidth = 0;  
  45.   
  46.     // Count of visible items   
  47.     private int visibleItems = DEF_VISIBLE_ITEMS;  
  48.       
  49.     // Item height   
  50.     private int itemHeight = 0;  
  51.   
  52.     // Text paints   
  53.     private TextPaint itemsPaint;  
  54.     private TextPaint valuePaint;  
  55.   
  56.     // Layouts   
  57.     private StaticLayout itemsLayout;  
  58.     private StaticLayout labelLayout;  
  59.     private StaticLayout valueLayout;  
  60.   
  61.     // Label & background   
  62.     private String label;  
  63.     private Drawable centerDrawable;  
  64.   
  65.     // Shadows drawables   
  66.     private GradientDrawable topShadow;  
  67.     private GradientDrawable bottomShadow;  
  68.   
  69.     // Scrolling   
  70.     private boolean isScrollingPerformed;   
  71.     private int scrollingOffset;  
  72.   
  73.     // Scrolling animation   
  74.     private GestureDetector gestureDetector;  
  75.     private Scroller scroller;  
  76.     private int lastScrollY;  
  77.   
  78.     // Cyclic   
  79.     boolean isCyclic = false;  
  80.       
  81.     // Listeners   
  82.     private List<OnWheelChangedListener> changingListeners = new LinkedList<OnWheelChangedListener>();  
  83.     private List<OnWheelScrollListener> scrollingListeners = new LinkedList<OnWheelScrollListener>();  

在这里面,使用到了StaticLayout,在开发文档中找一下这个类:

  1. StaticLayout is a Layout for text that will not be edited after it is laid out. Use DynamicLayout for text that may change.  
  2.   
  3. This is used by widgets to control text layout. You should not need to use this class directly unless you are implementing your own widget or custom display object, or would be tempted to call Canvas.drawText

staticLayout被创建以后就不能被修改了,通常被用于控制文本组件布局。

还使用到了Drawable、Text'Paint、GradientDrawable、GestureDetector、Scroller类,在开发文档中,GradientDrawable的概述:

  1. A Drawable with a color gradient for buttons, backgrounds, etc.  
  2.   
  3. It can be defined in an XML file with the <shape> element. For more information, see the guide to Drawable Resources. 
linux
相关资讯       Android源码 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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