在这里使用的是典型的回调方法模式。
然后现在,我们进入WheelView类,看一下他是如何构建,首先,WheelView继承了View类。代码的22行到45行是导入的所需要的类。从54行到135行是声明一些变量和类:
- /** Scrolling duration */
- private static final int SCROLLING_DURATION = 400;
- /** Minimum delta for scrolling */
- private static final int MIN_DELTA_FOR_SCROLLING = 1;
- /** Current value & label text color */
- private static final int VALUE_TEXT_COLOR = 0xF0000000;
- /** Items text color */
- private static final int ITEMS_TEXT_COLOR = 0xFF000000;
- /** Top and bottom shadows colors */
- private static final int[] SHADOWS_COLORS = new int[] { 0xFF111111,
- 0x00AAAAAA, 0x00AAAAAA };
- /** Additional items height (is added to standard text item height) */
- private static final int ADDITIONAL_ITEM_HEIGHT = 15;
- /** Text size */
- private static final int TEXT_SIZE = 24;
- /** Top and bottom items offset (to hide that) */
- private static final int ITEM_OFFSET = TEXT_SIZE / 5;
- /** Additional width for items layout */
- private static final int ADDITIONAL_ITEMS_SPACE = 10;
- /** Label offset */
- private static final int LABEL_OFFSET = 8;
- /** Left and right padding value */
- private static final int PADDING = 10;
- /** Default count of visible items */
- private static final int DEF_VISIBLE_ITEMS = 5;
- // Wheel Values
- private WheelAdapter adapter = null;
- private int currentItem = 0;
- // Widths
- private int itemsWidth = 0;
- private int labelWidth = 0;
- // Count of visible items
- private int visibleItems = DEF_VISIBLE_ITEMS;
- // Item height
- private int itemHeight = 0;
- // Text paints
- private TextPaint itemsPaint;
- private TextPaint valuePaint;
- // Layouts
- private StaticLayout itemsLayout;
- private StaticLayout labelLayout;
- private StaticLayout valueLayout;
- // Label & background
- private String label;
- private Drawable centerDrawable;
- // Shadows drawables
- private GradientDrawable topShadow;
- private GradientDrawable bottomShadow;
- // Scrolling
- private boolean isScrollingPerformed;
- private int scrollingOffset;
- // Scrolling animation
- private GestureDetector gestureDetector;
- private Scroller scroller;
- private int lastScrollY;
- // Cyclic
- boolean isCyclic = false;
- // Listeners
- private List<OnWheelChangedListener> changingListeners = new LinkedList<OnWheelChangedListener>();
- private List<OnWheelScrollListener> scrollingListeners = new LinkedList<OnWheelScrollListener>();
在这里面,使用到了StaticLayout,在开发文档中找一下这个类:
- StaticLayout is a Layout for text that will not be edited after it is laid out. Use DynamicLayout for text that may change.
- 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的概述:
- A Drawable with a color gradient for buttons, backgrounds, etc.
- It can be defined in an XML file with the <shape> element. For more information, see the guide to Drawable Resources.
