kankan.wheel.widget
Class WheelView

java.lang.Object
  extended by View
      extended by kankan.wheel.widget.WheelView

public class WheelView
extends View

Numeric wheel view.

Author:
Yuri Kanivets

Field Summary
private  GradientDrawable bottomShadow
           
private  Drawable centerDrawable
           
private  java.util.List<OnWheelChangedListener> changingListeners
           
private  java.util.List<OnWheelClickedListener> clickingListeners
           
private  int currentItem
           
private  DataSetObserver dataObserver
           
private static int DEF_VISIBLE_ITEMS
          Default count of visible items
private  int firstItem
           
(package private)  boolean isCyclic
           
private  boolean isScrollingPerformed
           
private static int ITEM_OFFSET_PERCENT
          Top and bottom items offset (to hide that)
private  int itemHeight
           
private  LinearLayout itemsLayout
           
private static int PADDING
          Left and right padding value
private  WheelRecycle recycle
           
private  WheelScroller scroller
           
(package private)  WheelScroller.ScrollingListener scrollingListener
           
private  java.util.List<OnWheelScrollListener> scrollingListeners
           
private  int scrollingOffset
           
private static int[] SHADOWS_COLORS
          Top and bottom shadows colors
private  GradientDrawable topShadow
           
private  WheelViewAdapter viewAdapter
           
private  int visibleItems
           
 
Constructor Summary
WheelView(Context context)
          Constructor
WheelView(Context context, AttributeSet attrs)
          Constructor
WheelView(Context context, AttributeSet attrs, int defStyle)
          Constructor
 
Method Summary
 void addChangingListener(OnWheelChangedListener listener)
          Adds wheel changing listener
 void addClickingListener(OnWheelClickedListener listener)
          Adds wheel clicking listener
 void addScrollingListener(OnWheelScrollListener listener)
          Adds wheel scrolling listener
private  boolean addViewItem(int index, boolean first)
          Adds view for item to items layout
private  void buildViewForMeasuring()
          Builds view for measuring
private  int calculateLayoutWidth(int widthSize, int mode)
          Calculates control width and creates text layouts
private  void createItemsLayout()
          Creates item layouts if necessary
private  void doScroll(int delta)
          Scrolls the wheel
private  void drawCenterRect(Canvas canvas)
          Draws rect for current value
private  void drawItems(Canvas canvas)
          Draws items
private  void drawShadows(Canvas canvas)
          Draws shadows on top and bottom of control
 int getCurrentItem()
          Gets current value
private  int getDesiredHeight(LinearLayout layout)
          Calculates desired height for layout
private  int getItemHeight()
          Returns height of wheel item
private  ItemsRange getItemsRange()
          Calculates range for wheel items
private  View getItemView(int index)
          Returns view for specified item
 WheelViewAdapter getViewAdapter()
          Gets view adapter
 int getVisibleItems()
          Gets count of visible items
private  void initData(Context context)
          Initializes class data
private  void initResourcesIfNecessary()
          Initializes resources
 void invalidateWheel(boolean clearCaches)
          Invalidates wheel
 boolean isCyclic()
          Tests if wheel is cyclic.
private  boolean isValidItemIndex(int index)
          Checks whether intem index is valid
private  void layout(int width, int height)
          Sets layouts width and height
protected  void notifyChangingListeners(int oldValue, int newValue)
          Notifies changing listeners
protected  void notifyClickListenersAboutClick(int item)
          Notifies listeners about clicking
protected  void notifyScrollingListenersAboutEnd()
          Notifies listeners about ending scrolling
protected  void notifyScrollingListenersAboutStart()
          Notifies listeners about starting scrolling
protected  void onDraw(Canvas canvas)
           
protected  void onLayout(boolean changed, int l, int t, int r, int b)
           
protected  void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
           
 boolean onTouchEvent(MotionEvent event)
           
private  boolean rebuildItems()
          Rebuilds wheel items if necessary.
 void removeChangingListener(OnWheelChangedListener listener)
          Removes wheel changing listener
 void removeClickingListener(OnWheelClickedListener listener)
          Removes wheel clicking listener
 void removeScrollingListener(OnWheelScrollListener listener)
          Removes wheel scrolling listener
 void scroll(int itemsToScroll, int time)
          Scroll the wheel
 void setCurrentItem(int index)
          Sets the current item w/o animation.
 void setCurrentItem(int index, boolean animated)
          Sets the current item.
 void setCyclic(boolean isCyclic)
          Set wheel cyclic flag
 void setInterpolator(Interpolator interpolator)
          Set the the specified scrolling interpolator
 void setViewAdapter(WheelViewAdapter viewAdapter)
          Sets view adapter.
 void setVisibleItems(int count)
          Sets the desired count of visible items.
 void stopScrolling()
          Stops scrolling
private  void updateView()
          Updates view.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SHADOWS_COLORS

private static final int[] SHADOWS_COLORS
Top and bottom shadows colors


ITEM_OFFSET_PERCENT

private static final int ITEM_OFFSET_PERCENT
Top and bottom items offset (to hide that)

See Also:
Constant Field Values

PADDING

private static final int PADDING
Left and right padding value

See Also:
Constant Field Values

DEF_VISIBLE_ITEMS

private static final int DEF_VISIBLE_ITEMS
Default count of visible items

See Also:
Constant Field Values

currentItem

private int currentItem

visibleItems

private int visibleItems

itemHeight

private int itemHeight

centerDrawable

private Drawable centerDrawable

topShadow

private GradientDrawable topShadow

bottomShadow

private GradientDrawable bottomShadow

scroller

private WheelScroller scroller

isScrollingPerformed

private boolean isScrollingPerformed

scrollingOffset

private int scrollingOffset

isCyclic

boolean isCyclic

itemsLayout

private LinearLayout itemsLayout

firstItem

private int firstItem

viewAdapter

private WheelViewAdapter viewAdapter

recycle

private WheelRecycle recycle

changingListeners

private java.util.List<OnWheelChangedListener> changingListeners

scrollingListeners

private java.util.List<OnWheelScrollListener> scrollingListeners

clickingListeners

private java.util.List<OnWheelClickedListener> clickingListeners

scrollingListener

WheelScroller.ScrollingListener scrollingListener

dataObserver

private DataSetObserver dataObserver
Constructor Detail

WheelView

public WheelView(Context context,
                 AttributeSet attrs,
                 int defStyle)
Constructor


WheelView

public WheelView(Context context,
                 AttributeSet attrs)
Constructor


WheelView

public WheelView(Context context)
Constructor

Method Detail

initData

private void initData(Context context)
Initializes class data

Parameters:
context - the context

setInterpolator

public void setInterpolator(Interpolator interpolator)
Set the the specified scrolling interpolator

Parameters:
interpolator - the interpolator

getVisibleItems

public int getVisibleItems()
Gets count of visible items

Returns:
the count of visible items

setVisibleItems

public void setVisibleItems(int count)
Sets the desired count of visible items. Actual amount of visible items depends on wheel layout parameters. To apply changes and rebuild view call measure().

Parameters:
count - the desired count for visible items

getViewAdapter

public WheelViewAdapter getViewAdapter()
Gets view adapter

Returns:
the view adapter

setViewAdapter

public void setViewAdapter(WheelViewAdapter viewAdapter)
Sets view adapter. Usually new adapters contain different views, so it needs to rebuild view by calling measure().

Parameters:
viewAdapter - the view adapter

addChangingListener

public void addChangingListener(OnWheelChangedListener listener)
Adds wheel changing listener

Parameters:
listener - the listener

removeChangingListener

public void removeChangingListener(OnWheelChangedListener listener)
Removes wheel changing listener

Parameters:
listener - the listener

notifyChangingListeners

protected void notifyChangingListeners(int oldValue,
                                       int newValue)
Notifies changing listeners

Parameters:
oldValue - the old wheel value
newValue - the new wheel value

addScrollingListener

public void addScrollingListener(OnWheelScrollListener listener)
Adds wheel scrolling listener

Parameters:
listener - the listener

removeScrollingListener

public void removeScrollingListener(OnWheelScrollListener listener)
Removes wheel scrolling listener

Parameters:
listener - the listener

notifyScrollingListenersAboutStart

protected void notifyScrollingListenersAboutStart()
Notifies listeners about starting scrolling


notifyScrollingListenersAboutEnd

protected void notifyScrollingListenersAboutEnd()
Notifies listeners about ending scrolling


addClickingListener

public void addClickingListener(OnWheelClickedListener listener)
Adds wheel clicking listener

Parameters:
listener - the listener

removeClickingListener

public void removeClickingListener(OnWheelClickedListener listener)
Removes wheel clicking listener

Parameters:
listener - the listener

notifyClickListenersAboutClick

protected void notifyClickListenersAboutClick(int item)
Notifies listeners about clicking


getCurrentItem

public int getCurrentItem()
Gets current value

Returns:
the current value

setCurrentItem

public void setCurrentItem(int index,
                           boolean animated)
Sets the current item. Does nothing when index is wrong.

Parameters:
index - the item index
animated - the animation flag

setCurrentItem

public void setCurrentItem(int index)
Sets the current item w/o animation. Does nothing when index is wrong.

Parameters:
index - the item index

isCyclic

public boolean isCyclic()
Tests if wheel is cyclic. That means before the 1st item there is shown the last one

Returns:
true if wheel is cyclic

setCyclic

public void setCyclic(boolean isCyclic)
Set wheel cyclic flag

Parameters:
isCyclic - the flag to set

invalidateWheel

public void invalidateWheel(boolean clearCaches)
Invalidates wheel

Parameters:
clearCaches - if true then cached views will be clear

initResourcesIfNecessary

private void initResourcesIfNecessary()
Initializes resources


getDesiredHeight

private int getDesiredHeight(LinearLayout layout)
Calculates desired height for layout

Parameters:
layout - the source layout
Returns:
the desired layout height

getItemHeight

private int getItemHeight()
Returns height of wheel item

Returns:
the item height

calculateLayoutWidth

private int calculateLayoutWidth(int widthSize,
                                 int mode)
Calculates control width and creates text layouts

Parameters:
widthSize - the input layout width
mode - the layout mode
Returns:
the calculated control width

onMeasure

protected void onMeasure(int widthMeasureSpec,
                         int heightMeasureSpec)

onLayout

protected void onLayout(boolean changed,
                        int l,
                        int t,
                        int r,
                        int b)

layout

private void layout(int width,
                    int height)
Sets layouts width and height

Parameters:
width - the layout width
height - the layout height

onDraw

protected void onDraw(Canvas canvas)

drawShadows

private void drawShadows(Canvas canvas)
Draws shadows on top and bottom of control

Parameters:
canvas - the canvas for drawing

drawItems

private void drawItems(Canvas canvas)
Draws items

Parameters:
canvas - the canvas for drawing

drawCenterRect

private void drawCenterRect(Canvas canvas)
Draws rect for current value

Parameters:
canvas - the canvas for drawing

onTouchEvent

public boolean onTouchEvent(MotionEvent event)

doScroll

private void doScroll(int delta)
Scrolls the wheel

Parameters:
delta - the scrolling value

scroll

public void scroll(int itemsToScroll,
                   int time)
Scroll the wheel

Parameters:
itemsToSkip - items to scroll
time - scrolling duration

getItemsRange

private ItemsRange getItemsRange()
Calculates range for wheel items

Returns:
the items range

rebuildItems

private boolean rebuildItems()
Rebuilds wheel items if necessary. Caches all unused items.

Returns:
true if items are rebuilt

updateView

private void updateView()
Updates view. Rebuilds items and label if necessary, recalculate items sizes.


createItemsLayout

private void createItemsLayout()
Creates item layouts if necessary


buildViewForMeasuring

private void buildViewForMeasuring()
Builds view for measuring


addViewItem

private boolean addViewItem(int index,
                            boolean first)
Adds view for item to items layout

Parameters:
index - the item index
first - the flag indicates if view should be first
Returns:
true if corresponding item exists and is added

isValidItemIndex

private boolean isValidItemIndex(int index)
Checks whether intem index is valid

Parameters:
index - the item index
Returns:
true if item index is not out of bounds or the wheel is cyclic

getItemView

private View getItemView(int index)
Returns view for specified item

Parameters:
index - the item index
Returns:
item view or empty view if index is out of bounds

stopScrolling

public void stopScrolling()
Stops scrolling