
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.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,wIDthLabel > 0 ? Layout.Alignment.AliGN_OPPOSITE : Layout.Alignment.AliGN_CENTER,false); } else if (isScrollingperformed) { valueLayout = null; } else { valueLayout.increaseWIDthTo(wIDthItems); } if (wIDthLabel > 0) { if (labelLayout == null || labelLayout.getWIDth() > wIDthLabel) { labelLayout = new StaticLayout(label,wIDthLabel,Layout.Alignment.AliGN_norMAL,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,getWIDth(),getHeight() / visibleItems); topShadow.draw(canvas); bottomShadow.setBounds(0,getHeight() - getHeight() / visibleItems,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,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,float veLocityX,float veLocityY) { lastScrollY = currentItem * getItemHeight() + scrollingOffset; int maxY = isCyclic ? 0x7FFFFFFF : adapter.getItemsCount() * getItemHeight(); int minY = isCyclic ? -maxY : 0; scroller.fling(0,lastScrollY,(int) -veLocityY / 2,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,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,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,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,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,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,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); }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的android仿iphone滚轮控件显示效果全部内容,希望文章能够帮你解决android仿iphone滚轮控件显示效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)