当前位置: 移动技术网 > 移动技术>移动开发>Android > android仿iphone滚轮控件显示效果

android仿iphone滚轮控件显示效果

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

android仿iphone滚轮控件显示效果,供大家参考,具体内容如下

在论坛里看到的,自己弄个效果:

这个滚动的wheelview

 /* 
 * android wheel control. 
 * https://code.google.com/p/android-wheel/ 
 *  
 * copyright 2010 yuri kanivets 
 * 
 * licensed under the apache license, version 2.0 (the "license"); 
 * you may not use this file except in compliance with the license. 
 * you may obtain a copy of the license at 
 * 
 * http://www.apache.org/licenses/license-2.0 
 * 
 * unless required by applicable law or agreed to in writing, software 
 * distributed under the license is distributed on an "as is" basis, 
 * without warranties or conditions of any kind, either express or implied. 
 * see the license for the specific language governing permissions and 
 * limitations under the license. 
 */ 
 
package kankan.wheel.widget; 
 
import java.util.linkedlist; 
import java.util.list; 
 
import android.content.context; 
import android.graphics.canvas; 
import android.graphics.paint; 
import android.graphics.rect; 
import android.graphics.drawable.drawable; 
import android.graphics.drawable.gradientdrawable; 
import android.graphics.drawable.gradientdrawable.orientation; 
import android.os.handler; 
import android.os.message; 
import android.text.layout; 
import android.text.staticlayout; 
import android.text.textpaint; 
import android.util.attributeset; 
import android.util.floatmath; 
import android.view.gesturedetector; 
import android.view.gesturedetector.simpleongesturelistener; 
import android.view.motionevent; 
import android.view.view; 
import android.view.animation.interpolator; 
import android.widget.scroller; 
 
import com.shao.pwd.r; 
 
/** 
 * numeric wheel view. 
 * 
 * @author yuri kanivets 
 */ 
public class wheelview extends view { 
  /** 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>(); 
 
  /** 
   * constructor 
   */ 
  public wheelview(context context, attributeset attrs, int defstyle) { 
    super(context, attrs, defstyle); 
    initdata(context); 
  } 
 
  /** 
   * constructor 
   */ 
  public wheelview(context context, attributeset attrs) { 
    super(context, attrs); 
    initdata(context); 
  } 
 
  /** 
   * constructor 
   */ 
  public wheelview(context context) { 
    super(context); 
    initdata(context); 
  } 
   
  /** 
   * initializes class data 
   * @param context the context 
   */ 
  private void initdata(context context) { 
    gesturedetector = new gesturedetector(context, gesturelistener); 
    gesturedetector.setislongpressenabled(false); 
     
    scroller = new scroller(context); 
  } 
   
  /** 
   * gets wheel adapter 
   * @return the adapter 
   */ 
  public wheeladapter getadapter() { 
    return adapter; 
  } 
   
  /** 
   * sets wheel adapter 
   * @param adapter the new wheel adapter 
   */ 
  public void setadapter(wheeladapter adapter) { 
    this.adapter = adapter; 
    invalidatelayouts(); 
    invalidate(); 
  } 
   
  /** 
   * set the the specified scrolling interpolator 
   * @param interpolator the interpolator 
   */ 
  public void setinterpolator(interpolator interpolator) { 
    scroller.forcefinished(true); 
    scroller = new scroller(getcontext(), interpolator); 
  } 
   
  /** 
   * gets count of visible items 
   * 
   * @return the count of visible items 
   */ 
  public int getvisibleitems() { 
    return visibleitems; 
  } 
 
  /** 
   * sets count of visible items 
   * 
   * @param count 
   *      the new count 
   */ 
  public void setvisibleitems(int count) { 
    visibleitems = count; 
    invalidate(); 
  } 
 
  /** 
   * gets label 
   * 
   * @return the label 
   */ 
  public string getlabel() { 
    return label; 
  } 
 
  /** 
   * sets label 
   * 
   * @param newlabel 
   *      the label to set 
   */ 
  public void setlabel(string newlabel) { 
    if (label == null || !label.equals(newlabel)) { 
      label = newlabel; 
      labellayout = null; 
      invalidate(); 
    } 
  } 
   
  /** 
   * adds wheel changing listener 
   * @param listener the listener 
   */ 
  public void addchanginglistener(onwheelchangedlistener listener) { 
    changinglisteners.add(listener); 
  } 
 
  /** 
   * removes wheel changing listener 
   * @param listener the listener 
   */ 
  public void removechanginglistener(onwheelchangedlistener listener) { 
    changinglisteners.remove(listener); 
  } 
   
  /** 
   * notifies changing listeners 
   * @param oldvalue the old wheel value 
   * @param newvalue the new wheel value 
   */ 
  protected void notifychanginglisteners(int oldvalue, int newvalue) { 
    for (onwheelchangedlistener listener : changinglisteners) { 
      listener.onchanged(this, oldvalue, newvalue); 
    } 
  } 
 
  /** 
   * adds wheel scrolling listener 
   * @param listener the listener 
   */ 
  public void addscrollinglistener(onwheelscrolllistener listener) { 
    scrollinglisteners.add(listener); 
  } 
 
  /** 
   * removes wheel scrolling listener 
   * @param listener the listener 
   */ 
  public void removescrollinglistener(onwheelscrolllistener listener) { 
    scrollinglisteners.remove(listener); 
  } 
   
  /** 
   * notifies listeners about starting scrolling 
   */ 
  protected void notifyscrollinglistenersaboutstart() { 
    for (onwheelscrolllistener listener : scrollinglisteners) { 
      listener.onscrollingstarted(this); 
    } 
  } 
 
  /** 
   * notifies listeners about ending scrolling 
   */ 
  protected void notifyscrollinglistenersaboutend() { 
    for (onwheelscrolllistener listener : scrollinglisteners) { 
      listener.onscrollingfinished(this); 
    } 
  } 
 
  /** 
   * gets current value 
   * 
   * @return the current value 
   */ 
  public int getcurrentitem() { 
    return currentitem; 
  } 
 
  /** 
   * sets the current item. does nothing when index is wrong. 
   * 
   * @param index the item index 
   * @param animated the animation flag 
   */ 
  public void setcurrentitem(int index, boolean animated) { 
    if (adapter == null || adapter.getitemscount() == 0) { 
      return; // throw? 
    } 
    if (index < 0 || index >= adapter.getitemscount()) { 
      if (iscyclic) { 
        while (index < 0) { 
          index += adapter.getitemscount(); 
        } 
        index %= adapter.getitemscount(); 
      } else{ 
        return; // throw? 
      } 
    } 
    if (index != currentitem) { 
      if (animated) { 
        scroll(index - currentitem, scrolling_duration); 
      } else { 
        invalidatelayouts(); 
       
        int old = currentitem; 
        currentitem = index; 
       
        notifychanginglisteners(old, currentitem); 
       
        invalidate(); 
      } 
    } 
  } 
 
  /** 
   * sets the current item w/o animation. does nothing when index is wrong. 
   * 
   * @param index the item index 
   */ 
  public void setcurrentitem(int index) { 
    setcurrentitem(index, false); 
  }   
   
  /** 
   * tests if wheel is cyclic. that means before the 1st item there is shown the last one 
   * @return true if wheel is cyclic 
   */ 
  public boolean iscyclic() { 
    return iscyclic; 
  } 
 
  /** 
   * set wheel cyclic flag 
   * @param iscyclic the flag to set 
   */ 
  public void setcyclic(boolean iscyclic) { 
    this.iscyclic = iscyclic; 
     
    invalidate(); 
    invalidatelayouts(); 
  } 
 
  /** 
   * invalidates layouts 
   */ 
  private void invalidatelayouts() { 
    itemslayout = null; 
    valuelayout = null; 
    scrollingoffset = 0; 
  } 
 
  /** 
   * initializes resources 
   */ 
  private void initresourcesifnecessary() { 
    if (itemspaint == null) { 
      itemspaint = new textpaint(paint.anti_alias_flag 
          | paint.fake_bold_text_flag); 
      //itemspaint.density = getresources().getdisplaymetrics().density; 
      itemspaint.settextsize(text_size); 
    } 
 
    if (valuepaint == null) { 
      valuepaint = new textpaint(paint.anti_alias_flag 
          | paint.fake_bold_text_flag | paint.dither_flag); 
      //valuepaint.density = getresources().getdisplaymetrics().density; 
      valuepaint.settextsize(text_size); 
      valuepaint.setshadowlayer(0.1f, 0, 0.1f, 0xffc0c0c0); 
    } 
 
    if (centerdrawable == null) { 
      centerdrawable = getcontext().getresources().getdrawable(r.drawable.wheel_val); 
    } 
 
    if (topshadow == null) { 
      topshadow = new gradientdrawable(orientation.top_bottom, shadows_colors); 
    } 
 
    if (bottomshadow == null) { 
      bottomshadow = new gradientdrawable(orientation.bottom_top, shadows_colors); 
    } 
 
    setbackgroundresource(r.drawable.wheel_bg); 
  } 
 
  /** 
   * calculates desired height for layout 
   * 
   * @param layout 
   *      the source layout 
   * @return the desired layout height 
   */ 
  private int getdesiredheight(layout layout) { 
    if (layout == null) { 
      return 0; 
    } 
 
    int desired = getitemheight() * visibleitems - item_offset * 2 
        - additional_item_height; 
 
    // check against our minimum height 
    desired = math.max(desired, getsuggestedminimumheight()); 
 
    return desired; 
  } 
 
  /** 
   * returns text item by index 
   * @param index the item index 
   * @return the item or null 
   */ 
  private string gettextitem(int index) { 
    if (adapter == null || adapter.getitemscount() == 0) { 
      return null; 
    } 
    int count = adapter.getitemscount(); 
    if ((index < 0 || index >= count) && !iscyclic) { 
      return null; 
    } else { 
      while (index < 0) { 
        index = count + index; 
      } 
    } 
     
    index %= count; 
    return adapter.getitem(index); 
  } 
   
  /** 
   * builds text depending on current value 
   * 
   * @param usecurrentvalue 
   * @return the text 
   */ 
  private string buildtext(boolean usecurrentvalue) { 
    stringbuilder itemstext = new stringbuilder(); 
    int additems = visibleitems / 2 + 1; 
 
    for (int i = currentitem - additems; i <= currentitem + additems; i++) { 
      if (usecurrentvalue || i != currentitem) { 
        string text = gettextitem(i); 
        if (text != null) { 
          itemstext.append(text); 
        } 
      } 
      if (i < currentitem + additems) { 
        itemstext.append("\n"); 
      } 
    } 
     
    return itemstext.tostring(); 
  } 
 
  /** 
   * returns the max item length that can be present 
   * @return the max length 
   */ 
  private int getmaxtextlength() { 
    wheeladapter adapter = getadapter(); 
    if (adapter == null) { 
      return 0; 
    } 
     
    int adapterlength = adapter.getmaximumlength(); 
    if (adapterlength > 0) { 
      return adapterlength; 
    } 
 
    string maxtext = null; 
    int additems = visibleitems / 2; 
    for (int i = math.max(currentitem - additems, 0); 
        i < math.min(currentitem + visibleitems, adapter.getitemscount()); i++) { 
      string text = adapter.getitem(i); 
      if (text != null && (maxtext == null || maxtext.length() < text.length())) { 
        maxtext = text; 
      } 
    } 
 
    return maxtext != null ? maxtext.length() : 0; 
  } 
 
  /** 
   * returns height of wheel item 
   * @return the item height 
   */ 
  private int getitemheight() { 
    if (itemheight != 0) { 
      return itemheight; 
    } else if (itemslayout != null && itemslayout.getlinecount() > 2) { 
      itemheight = itemslayout.getlinetop(2) - itemslayout.getlinetop(1); 
      return itemheight; 
    } 
     
    return getheight() / visibleitems; 
  } 
 
  /** 
   * calculates control width and creates text layouts 
   * @param widthsize the input layout width 
   * @param mode the layout mode 
   * @return the calculated control width 
   */ 
  private int calculatelayoutwidth(int widthsize, int mode) { 
    initresourcesifnecessary(); 
 
    int width = widthsize; 
 
    int maxlength = getmaxtextlength(); 
    if (maxlength > 0) { 
      float textwidth = floatmath.ceil(layout.getdesiredwidth("0", itemspaint)); 
      itemswidth = (int) (maxlength * textwidth); 
    } else { 
      itemswidth = 0; 
    } 
    itemswidth += additional_items_space; // make it some more 
 
    labelwidth = 0; 
    if (label != null && label.length() > 0) { 
      labelwidth = (int) floatmath.ceil(layout.getdesiredwidth(label, valuepaint)); 
    } 
 
    boolean recalculate = false; 
    if (mode == measurespec.exactly) { 
      width = widthsize; 
      recalculate = true; 
    } else { 
      width = itemswidth + labelwidth + 2 * padding; 
      if (labelwidth > 0) { 
        width += label_offset; 
      } 
 
      // check against our minimum width 
      width = math.max(width, getsuggestedminimumwidth()); 
 
      if (mode == measurespec.at_most && widthsize < width) { 
        width = widthsize; 
        recalculate = true; 
      } 
    } 
 
    if (recalculate) { 
      // recalculate width 
      int purewidth = width - label_offset - 2 * padding; 
      if (purewidth <= 0) { 
        itemswidth = labelwidth = 0; 
      } 
      if (labelwidth > 0) { 
        double newwidthitems = (double) itemswidth * purewidth 
            / (itemswidth + labelwidth); 
        itemswidth = (int) newwidthitems; 
        labelwidth = purewidth - itemswidth; 
      } else { 
        itemswidth = purewidth + label_offset; // no label 
      } 
    } 
 
    if (itemswidth > 0) { 
      createlayouts(itemswidth, labelwidth); 
    } 
 
    return width; 
  } 
 
  /** 
   * creates layouts 
   * @param widthitems width of items layout 
   * @param widthlabel width of label layout 
   */ 
  private void createlayouts(int widthitems, int widthlabel) { 
    if (itemslayout == null || itemslayout.getwidth() > widthitems) { 
      itemslayout = new staticlayout(buildtext(isscrollingperformed), itemspaint, widthitems, 
          widthlabel > 0 ? layout.alignment.align_opposite : layout.alignment.align_center, 
          1, additional_item_height, false); 
    } else { 
      itemslayout.increasewidthto(widthitems); 
    } 
 
    if (!isscrollingperformed && (valuelayout == null || valuelayout.getwidth() > widthitems)) { 
      string text = getadapter() != null ? getadapter().getitem(currentitem) : null; 
      valuelayout = new staticlayout(text != null ? text : "", 
          valuepaint, widthitems, widthlabel > 0 ? 
              layout.alignment.align_opposite : layout.alignment.align_center, 
              1, additional_item_height, false); 
    } else if (isscrollingperformed) { 
      valuelayout = null; 
    } else { 
      valuelayout.increasewidthto(widthitems); 
    } 
 
    if (widthlabel > 0) { 
      if (labellayout == null || labellayout.getwidth() > widthlabel) { 
        labellayout = new staticlayout(label, valuepaint, 
            widthlabel, layout.alignment.align_normal, 1, 
            additional_item_height, false); 
      } else { 
        labellayout.increasewidthto(widthlabel); 
      } 
    } 
  } 
 
  @override 
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { 
    int widthmode = measurespec.getmode(widthmeasurespec); 
    int heightmode = measurespec.getmode(heightmeasurespec); 
    int widthsize = measurespec.getsize(widthmeasurespec); 
    int heightsize = measurespec.getsize(heightmeasurespec); 
 
    int width = calculatelayoutwidth(widthsize, widthmode); 
 
    int height; 
    if (heightmode == measurespec.exactly) { 
      height = heightsize; 
    } else { 
      height = getdesiredheight(itemslayout); 
 
      if (heightmode == measurespec.at_most) { 
        height = math.min(height, heightsize); 
      } 
    } 
 
    setmeasureddimension(width, height); 
  } 
 
  @override 
  protected void ondraw(canvas canvas) { 
    super.ondraw(canvas); 
     
    if (itemslayout == null) { 
      if (itemswidth == 0) { 
        calculatelayoutwidth(getwidth(), measurespec.exactly); 
      } else { 
        createlayouts(itemswidth, labelwidth); 
      } 
    } 
 
    if (itemswidth > 0) { 
      canvas.save(); 
      // skip padding space and hide a part of top and bottom items 
      canvas.translate(padding, -item_offset); 
      drawitems(canvas); 
      drawvalue(canvas); 
      canvas.restore(); 
    } 
 
    drawcenterrect(canvas); 
    drawshadows(canvas); 
  } 
 
  /** 
   * draws shadows on top and bottom of control 
   * @param canvas the canvas for drawing 
   */ 
  private void drawshadows(canvas canvas) { 
    topshadow.setbounds(0, 0, getwidth(), getheight() / visibleitems); 
    topshadow.draw(canvas); 
 
    bottomshadow.setbounds(0, getheight() - getheight() / visibleitems, 
        getwidth(), getheight()); 
    bottomshadow.draw(canvas); 
  } 
 
  /** 
   * draws value and label layout 
   * @param canvas the canvas for drawing 
   */ 
  private void drawvalue(canvas canvas) { 
    valuepaint.setcolor(value_text_color); 
    valuepaint.drawablestate = getdrawablestate(); 
 
    rect bounds = new rect(); 
    itemslayout.getlinebounds(visibleitems / 2, bounds); 
 
    // draw label 
    if (labellayout != null) { 
      canvas.save(); 
      canvas.translate(itemslayout.getwidth() + label_offset, bounds.top); 
      labellayout.draw(canvas); 
      canvas.restore(); 
    } 
 
    // draw current value 
    if (valuelayout != null) { 
      canvas.save(); 
      canvas.translate(0, bounds.top + scrollingoffset); 
      valuelayout.draw(canvas); 
      canvas.restore(); 
    } 
  } 
 
  /** 
   * draws items 
   * @param canvas the canvas for drawing 
   */ 
  private void drawitems(canvas canvas) { 
    canvas.save(); 
     
    int top = itemslayout.getlinetop(1); 
    canvas.translate(0, - top + scrollingoffset); 
     
    itemspaint.setcolor(items_text_color); 
    itemspaint.drawablestate = getdrawablestate(); 
    itemslayout.draw(canvas); 
     
    canvas.restore(); 
  } 
 
  /** 
   * draws rect for current value 
   * @param canvas the canvas for drawing 
   */ 
  private void drawcenterrect(canvas canvas) { 
    int center = getheight() / 2; 
    int offset = getitemheight() / 2; 
    centerdrawable.setbounds(0, center - offset, getwidth(), center + offset); 
    centerdrawable.draw(canvas); 
  } 
 
  @override 
  public boolean ontouchevent(motionevent event) { 
    wheeladapter adapter = getadapter(); 
    if (adapter == null) { 
      return true; 
    } 
     
      if (!gesturedetector.ontouchevent(event) && event.getaction() == motionevent.action_up) { 
      justify(); 
    } 
    return true; 
  } 
   
  /** 
   * scrolls the wheel 
   * @param delta the scrolling value 
   */ 
  private void doscroll(int delta) { 
    scrollingoffset += delta; 
     
    int count = scrollingoffset / getitemheight(); 
    int pos = currentitem - count; 
    if (iscyclic && adapter.getitemscount() > 0) { 
      // fix position by rotating 
      while (pos < 0) { 
        pos += adapter.getitemscount(); 
      } 
      pos %= adapter.getitemscount(); 
    } else if (isscrollingperformed) { 
      //  
      if (pos < 0) { 
        count = currentitem; 
        pos = 0; 
      } else if (pos >= adapter.getitemscount()) { 
        count = currentitem - adapter.getitemscount() + 1; 
        pos = adapter.getitemscount() - 1; 
      } 
    } else { 
      // fix position 
      pos = math.max(pos, 0); 
      pos = math.min(pos, adapter.getitemscount() - 1); 
    } 
     
    int offset = scrollingoffset; 
    if (pos != currentitem) { 
      setcurrentitem(pos, false); 
    } else { 
      invalidate(); 
    } 
     
    // update offset 
    scrollingoffset = offset - count * getitemheight(); 
    if (scrollingoffset > getheight()) { 
      scrollingoffset = scrollingoffset % getheight() + getheight(); 
    } 
  } 
   
  // gesture listener 
  private simpleongesturelistener gesturelistener = new simpleongesturelistener() { 
    public boolean ondown(motionevent e) { 
      if (isscrollingperformed) { 
        scroller.forcefinished(true); 
        clearmessages(); 
        return true; 
      } 
      return false; 
    } 
     
    public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { 
      startscrolling(); 
      doscroll((int)-distancey); 
      return true; 
    } 
     
    public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { 
      lastscrolly = currentitem * getitemheight() + scrollingoffset; 
      int maxy = iscyclic ? 0x7fffffff : adapter.getitemscount() * getitemheight(); 
      int miny = iscyclic ? -maxy : 0; 
      scroller.fling(0, lastscrolly, 0, (int) -velocityy / 2, 0, 0, miny, maxy); 
      setnextmessage(message_scroll); 
      return true; 
    } 
  }; 
 
  // messages 
  private final int message_scroll = 0; 
  private final int message_justify = 1; 
   
  /** 
   * set next message to queue. clears queue before. 
   * 
   * @param message the message to set 
   */ 
  private void setnextmessage(int message) { 
    clearmessages(); 
    animationhandler.sendemptymessage(message); 
  } 
 
  /** 
   * clears messages from queue 
   */ 
  private void clearmessages() { 
    animationhandler.removemessages(message_scroll); 
    animationhandler.removemessages(message_justify); 
  } 
   
  // animation handler 
  private handler animationhandler = new handler() { 
    public void handlemessage(message msg) { 
      scroller.computescrolloffset(); 
      int curry = scroller.getcurry(); 
      int delta = lastscrolly - curry; 
      lastscrolly = curry; 
      if (delta != 0) { 
        doscroll(delta); 
      } 
       
      // scrolling is not finished when it comes to final y 
      // so, finish it manually  
      if (math.abs(curry - scroller.getfinaly()) < min_delta_for_scrolling) { 
        curry = scroller.getfinaly(); 
        scroller.forcefinished(true); 
      } 
      if (!scroller.isfinished()) { 
        animationhandler.sendemptymessage(msg.what); 
      } else if (msg.what == message_scroll) { 
        justify(); 
      } else { 
        finishscrolling(); 
      } 
    } 
  }; 
   
  /** 
   * justifies wheel 
   */ 
  private void justify() { 
    if (adapter == null) { 
      return; 
    } 
     
    lastscrolly = 0; 
    int offset = scrollingoffset; 
    int itemheight = getitemheight(); 
    boolean needtoincrease = offset > 0 ? currentitem < adapter.getitemscount() : currentitem > 0;  
    if ((iscyclic || needtoincrease) && math.abs((float) offset) > (float) itemheight / 2) { 
      if (offset < 0) 
        offset += itemheight + min_delta_for_scrolling; 
      else 
        offset -= itemheight + min_delta_for_scrolling; 
    } 
    if (math.abs(offset) > min_delta_for_scrolling) { 
      scroller.startscroll(0, 0, 0, offset, scrolling_duration); 
      setnextmessage(message_justify); 
    } else { 
      finishscrolling(); 
    } 
  } 
   
  /** 
   * starts scrolling 
   */ 
  private void startscrolling() { 
    if (!isscrollingperformed) { 
      isscrollingperformed = true; 
      notifyscrollinglistenersaboutstart(); 
    } 
  } 
 
  /** 
   * finishes scrolling 
   */ 
  void finishscrolling() { 
    if (isscrollingperformed) { 
      notifyscrollinglistenersaboutend(); 
      isscrollingperformed = false; 
    } 
    invalidatelayouts(); 
    invalidate(); 
  } 
   
   
  /** 
   * scroll the wheel 
   * @param itemstoskip items to scroll 
   * @param time scrolling duration 
   */ 
  public void scroll(int itemstoscroll, int time) { 
    scroller.forcefinished(true); 
 
    lastscrolly = scrollingoffset; 
    int offset = itemstoscroll * getitemheight(); 
     
    scroller.startscroll(0, lastscrolly, 0, offset - lastscrolly, time); 
    setnextmessage(message_scroll); 
     
    startscrolling(); 
  } 
 
} 

主布局文件

 <?xml version="1.0" encoding="utf-8"?> 
 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_height="wrap_content" 
  android:orientation="vertical" 
  android:background="@drawable/layout_bg" 
  android:layout_width="fill_parent"> 
   
  <textview 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margintop="24dp" 
    android:layout_gravity="center_horizontal" 
    android:textsize="20sp" 
    android:textstyle="bold" 
    android:text="please enter pin"/> 
     
  <linearlayout 
    android:layout_margintop="24dp" 
    android:layout_gravity="center_horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
     
    <kankan.wheel.widget.wheelview android:id="@+id/passw_1" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content"/> 
    <kankan.wheel.widget.wheelview android:id="@+id/passw_2" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content"/> 
    <kankan.wheel.widget.wheelview android:id="@+id/passw_3" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content"/> 
    <kankan.wheel.widget.wheelview android:id="@+id/passw_4" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content"/> 
    <kankan.wheel.widget.wheelview android:id="@+id/passw_5" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"/> 
    <kankan.wheel.widget.wheelview android:id="@+id/passw_6" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"/> 
  </linearlayout> 
   
  <textview android:id="@+id/pwd_status" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margintop="24dp" 
    android:layout_gravity="center_horizontal" 
    android:textsize="18sp" 
    android:textcolor="#fff" 
    android:text="wrong pin"/> 
   
  <button android:id="@+id/btn_mix" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_margintop="12dp" 
    android:textsize="18sp" 
    android:text="  mix "/> 
 
</linearlayout> 

适配器adapter 

/* 
 * copyright 2010 yuri kanivets 
 * 
 * licensed under the apache license, version 2.0 (the "license"); 
 * you may not use this file except in compliance with the license. 
 * you may obtain a copy of the license at 
 * 
 * http://www.apache.org/licenses/license-2.0 
 * 
 * unless required by applicable law or agreed to in writing, software 
 * distributed under the license is distributed on an "as is" basis, 
 * without warranties or conditions of any kind, either express or implied. 
 * see the license for the specific language governing permissions and 
 * limitations under the license. 
 */ 
 
package kankan.wheel.widget; 
 
public interface wheeladapter { 
  /** 
   * gets items count 
   * @return the count of wheel items 
   */ 
  public int getitemscount(); 
   
  /** 
   * gets a wheel item by index. 
   * 
   * @param index the item index 
   * @return the wheel item text or null 
   */ 
  public string getitem(int index); 
   
  /** 
   * gets maximum item length. it is used to determine the wheel width. 
   * if -1 is returned there will be used the default wheel width. 
   * 
   * @return the maximum item length or -1 
   */ 
  public int getmaximumlength(); 
} 

/* 
 * copyright 2010 yuri kanivets 
 * 
 * licensed under the apache license, version 2.0 (the "license"); 
 * you may not use this file except in compliance with the license. 
 * you may obtain a copy of the license at 
 * 
 * http://www.apache.org/licenses/license-2.0 
 * 
 * unless required by applicable law or agreed to in writing, software 
 * distributed under the license is distributed on an "as is" basis, 
 * without warranties or conditions of any kind, either express or implied. 
 * see the license for the specific language governing permissions and 
 * limitations under the license. 
 */ 
 
package kankan.wheel.widget; 
 
 
/** 
 * numeric wheel adapter. 
 */ 
public class numericwheeladapter implements wheeladapter { 
   
  /** the default min value */ 
  public static final int default_max_value = 9; 
 
  /** the default max value */ 
  private static final int default_min_value = 0; 
   
  // values 
  private int minvalue; 
  private int maxvalue; 
   
  // format 
  private string format; 
   
  /** 
   * default constructor 
   */ 
  public numericwheeladapter() { 
    this(default_min_value, default_max_value); 
  } 
 
  /** 
   * constructor 
   * @param minvalue the wheel min value 
   * @param maxvalue the wheel max value 
   */ 
  public numericwheeladapter(int minvalue, int maxvalue) { 
    this(minvalue, maxvalue, null); 
  } 
 
  /** 
   * constructor 
   * @param minvalue the wheel min value 
   * @param maxvalue the wheel max value 
   * @param format the format string 
   */ 
  public numericwheeladapter(int minvalue, int maxvalue, string format) { 
    this.minvalue = minvalue; 
    this.maxvalue = maxvalue; 
    this.format = format; 
  } 
 
  @override 
  public string getitem(int index) { 
    if (index >= 0 && index < getitemscount()) { 
      int value = minvalue + index; 
      return format != null ? string.format(format, value) : integer.tostring(value); 
    } 
    return null; 
  } 
 
  @override 
  public int getitemscount() { 
    return maxvalue - minvalue + 1; 
  } 
   
  @override 
  public int getmaximumlength() { 
    int max = math.max(math.abs(maxvalue), math.abs(minvalue)); 
    int maxlen = integer.tostring(max).length(); 
    if (minvalue < 0) { 
      maxlen++; 
    } 
    return maxlen; 
  } 
} 

监听器listener文件:

/* 
 * copyright 2010 yuri kanivets 
 * 
 * licensed under the apache license, version 2.0 (the "license"); 
 * you may not use this file except in compliance with the license. 
 * you may obtain a copy of the license at 
 * 
 * http://www.apache.org/licenses/license-2.0 
 * 
 * unless required by applicable law or agreed to in writing, software 
 * distributed under the license is distributed on an "as is" basis, 
 * without warranties or conditions of any kind, either express or implied. 
 * see the license for the specific language governing permissions and 
 * limitations under the license. 
 */ 
 
package kankan.wheel.widget; 
 
/** 
 * wheel scrolled listener interface. 
 */ 
public interface onwheelscrolllistener { 
  /** 
   * callback method to be invoked when scrolling started. 
   * @param wheel the wheel view whose state has changed. 
   */ 
  void onscrollingstarted(wheelview wheel); 
   
  /** 
   * callback method to be invoked when scrolling ended. 
   * @param wheel the wheel view whose state has changed. 
   */ 
  void onscrollingfinished(wheelview wheel); 
} 
/* 
 * copyright 2010 yuri kanivets 
 * 
 * licensed under the apache license, version 2.0 (the "license"); 
 * you may not use this file except in compliance with the license. 
 * you may obtain a copy of the license at 
 * 
 * http://www.apache.org/licenses/license-2.0 
 * 
 * unless required by applicable law or agreed to in writing, software 
 * distributed under the license is distributed on an "as is" basis, 
 * without warranties or conditions of any kind, either express or implied. 
 * see the license for the specific language governing permissions and 
 * limitations under the license. 
 */ 
 
package kankan.wheel.widget; 
 
/** 
 * wheel changed listener interface. 
 * <p>the currentitemchanged() method is called whenever current wheel positions is changed: 
 * <li> new wheel position is set 
 * <li> wheel view is scrolled 
 */ 
public interface onwheelchangedlistener { 
 /** 
  * callback method to be invoked when current item changed 
  * @param wheel the wheel view whose state has changed 
  * @param oldvalue the old value of current item 
  * @param newvalue the new value of current item 
  */ 
 void onchanged(wheelview wheel, int oldvalue, int newvalue); 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网