android WheelView时间选择器

来源:互联网 发布:ubuntu系统更新命令 编辑:程序博客网 时间:2024/05/13 08:18
private void showtimepicker(){AlertDialog.Builder builder = new AlertDialog.Builder(VideoPlayBackActivity.this);View view = LayoutInflater.from(VideoPlayBackActivity.this).inflate(R.layout.time_layout, null);builder.setView(view);builder.setTitle(R.string.select_time);final WheelView hours = (WheelView) view.findViewById(R.id.hour);hours.setAdapter(new NumericWheelAdapter(0, 23));hours.setLabel(getString(R.string.string_hour));final WheelView mins = (WheelView) view.findViewById(R.id.mins);mins.setAdapter(new NumericWheelAdapter(0, 59, "%02d"));mins.setLabel(getString(R.string.string_minute));mins.setCyclic(true);final WheelView scds = (WheelView) view.findViewById(R.id.scd);scds.setAdapter(new NumericWheelAdapter(0, 59, "%02d"));scds.setLabel(getString(R.string.string_second));scds.setCyclic(true);int curHours = c.get(Calendar.HOUR_OF_DAY);int curMinutes = c.get(Calendar.MINUTE);int curSeconds=c.get(Calendar.SECOND);hours.setCurrentItem(curHours);mins.setCurrentItem(curMinutes);scds.setCurrentItem(curSeconds);builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubint h=hours.getCurrentItem();int m=mins.getCurrentItem();int s=scds.getCurrentItem();time.setText(new StringBuilder().append((h < 10) ? "0" + h : h).append(":").append((m < 10) ? "0" + m : m).append(":").append((s < 10) ? "0" + s : s));}  });builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubdialog.dismiss();}});builder.show();}


布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="wrap_content"android:layout_width="fill_parent"android:layout_marginTop="12dp"android:orientation="vertical"android:background="@drawable/layout_bg"><LinearLayout android:layout_height="wrap_content"android:layout_width="fill_parent"android:layout_gravity="center_horizontal"android:paddingLeft="12dp"android:paddingRight="12dp"android:paddingTop="10dp">    <cn.gsss.iot.widgets.WheelView       android:id="@+id/hour"    android:layout_height="wrap_content"    android:layout_width="fill_parent"    android:layout_weight="1"/><cn.gsss.iot.widgets.WheelView     android:id="@+id/mins"    android:layout_height="wrap_content"    android:layout_width="fill_parent"    android:layout_weight="1"/><cn.gsss.iot.widgets.WheelView     android:id="@+id/scd"    android:layout_height="wrap_content"    android:layout_width="fill_parent"    android:layout_weight="1"/></LinearLayout></LinearLayout>


WheelView.java

import java.util.LinkedList;import java.util.List;import cn.gsss.iot.R;import cn.gsss.iot.adapter.WheelAdapter;import android.app.Activity;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.DisplayMetrics;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 android.widget.TextView;/** * Numeric wheel view. *  * @author Yuri Kanivets */public class WheelView extends View {/** 滚动的间隔 */ private static final int SCROLLING_DURATION = 400;    /** 最小滚动的距离*/private static final int MIN_DELTA_FOR_SCROLLING = 1;    /** 当前颜色*/   private static final int VALUE_TEXT_COLOR = 0xF0000000;    /** 文本的颜色*/   private static final int ITEMS_TEXT_COLOR = 0xFF000000;/** 顶部和底部的颜色 */ 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;/** 文字的大小*/ private static final int TEXT_SIZE = 40;/** Top and bottom items offset (to hide that) */ /** 顶部和底部的距离*/   private final int ITEM_OFFSET = TEXT_SIZE / 5;//private 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;// 相应的宽度   private int itemsWidth = 0;private int labelWidth = 0; //显示的项目数private int visibleItems = DEF_VISIBLE_ITEMS; // 项目的高度  private int itemHeight = 0;// Text paintsprivate TextPaint itemsPaint;private TextPaint valuePaint;// 相应的布局控件  private StaticLayout itemsLayout;private StaticLayout labelLayout;private StaticLayout valueLayout;// 标签和文本  private String label;private Drawable centerDrawable;// Shadows drawablesprivate GradientDrawable topShadow;private GradientDrawable bottomShadow;// Scrollingprivate boolean isScrollingPerformed; private int scrollingOffset;// Scrolling animationprivate GestureDetector gestureDetector;private Scroller scroller;private int lastScrollY;// 是不是循环boolean isCyclic = true;// Listenersprivate 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 heightdesired = 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 morelabelWidth = 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 widthwidth = Math.max(width, getSuggestedMinimumWidth());if (mode == MeasureSpec.AT_MOST && widthSize < width) {width = widthSize;recalculate = true;}}if (recalculate) {// recalculate widthint 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);}}}@Overrideprotected 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);}@Overrideprotected 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 itemscanvas.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 labelif (labelLayout != null) {canvas.save();canvas.translate(itemsLayout.getWidth() + LABEL_OFFSET, bounds.top);labelLayout.draw(canvas);canvas.restore();}// draw current valueif (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);}@Overridepublic 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 rotatingwhile (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 positionpos = Math.max(pos, 0);pos = Math.min(pos, adapter.getItemsCount() - 1);}int offset = scrollingOffset;if (pos != currentItem) {setCurrentItem(pos, false);} else {invalidate();}// update offsetscrollingOffset = offset - count * getItemHeight();if (scrollingOffset > getHeight()) {scrollingOffset = scrollingOffset % getHeight() + getHeight();}}// gesture listenerprivate 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;}};// Messagesprivate 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 handlerprivate 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;elseoffset -= 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();}}

WheelAdapter.java

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();}

NumericWheelAdapter.java
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;// Valuesprivate int minValue;private int maxValue;// formatprivate 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;}@Overridepublic 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;}@Overridepublic int getItemsCount() {return maxValue - minValue + 1;}@Overridepublic int getMaximumLength() {int max = Math.max(Math.abs(maxValue), Math.abs(minValue));int maxLen = Integer.toString(max).length();if (minValue < 0) {maxLen++;}return maxLen;}}

自定义dialog的方法(根据屏幕设置字体大小):

日期选择器:DatePickWheelDialog.java

import java.util.Arrays;import java.util.Calendar;import java.util.List;import cn.gsss.iot.R;import android.app.Activity;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.View;import android.widget.Button;public class DatePickWheelDialog extends Dialog {private static int START_YEAR = 1900, END_YEAR = 2999;private WheelView wv_year;private WheelView wv_month;private WheelView wv_day;String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };String[] months_little = { "4", "6", "9", "11" };final List<String> list_big = Arrays.asList(months_big);final List<String> list_little = Arrays.asList(months_little);private final Context mContext;private Button btn_sure;private CharSequence positiveText;private Button btn_cancel;private CharSequence negativeText;private Calendar calendar;private View.OnClickListener positiveClickListener;private View.OnClickListener negativeClickListener;private DatePickWheelDialog(Context context) {super(context);// TODO Auto-generated constructor stubthis.mContext = context;}private DatePickWheelDialog(Context context, Calendar instance) {// TODO Auto-generated constructor stubthis(context);calendar = instance;}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.date_layout);findView();adjustView();setListener();setDate(calendar);}private void adjustView() {// TODO Auto-generated method stub// 根据屏幕密度来指定选择器字体的大小int textSize = 0;textSize = pixelsToDip(mContext, 15);wv_day.TEXT_SIZE = textSize;wv_month.TEXT_SIZE = textSize;wv_year.TEXT_SIZE = textSize;}public static int pixelsToDip(Context context, int pixels) {DisplayMetrics mDisplayMetrics = new DisplayMetrics();((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);        int screenw = mDisplayMetrics.widthPixels;        float rate=(float)screenw/320;float paintsize=13*rate;return (int)paintsize;}private void setListener() {// TODO Auto-generated method stubwv_year.addChangingListener(wheelListener_year);wv_month.addChangingListener(wheelListener_month);// 取消if (negativeClickListener != null) {btn_cancel.setOnClickListener(negativeClickListener);} else {btn_cancel.setOnClickListener(dismissListener);}if (positiveClickListener != null) {btn_sure.setOnClickListener(positiveClickListener);} else {btn_sure.setOnClickListener(dismissListener);}}private final View.OnClickListener dismissListener = new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubdismiss();}};private void findView() {// TODO Auto-generated method stub// 年wv_year = (WheelView) findViewById(R.id.year);wv_year.setAdapter(new NumericWheelAdapter(START_YEAR,END_YEAR));// 设置"年"的显示数据wv_year.setLabel("年");// 添加文字// 月wv_month = (WheelView) findViewById(R.id.month);wv_month.setAdapter(new NumericWheelAdapter(1,12));wv_month.setLabel("月");// 日wv_day = (WheelView) findViewById(R.id.day);// 判断大小月及是否闰年,用来确定"日"的数据wv_day.setLabel("日");btn_sure = (Button) findViewById(R.id.btn_datetime_sure);if (positiveText != null) {btn_sure.setVisibility(View.VISIBLE);btn_sure.setText(positiveText);}btn_cancel = (Button) findViewById(R.id.btn_datetime_cancel);if (negativeText != null) {btn_cancel.setVisibility(View.VISIBLE);btn_cancel.setText(negativeText);}}// 添加"年"监听private final OnWheelChangedListener wheelListener_year = new OnWheelChangedListener() {@Overridepublic void onChanged(WheelView wheel, int oldValue, int newValue) {int year_num = newValue + START_YEAR;// 判断大小月及是否闰年,用来确定"日"的数据if (list_big.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {wv_day.setAdapter(new NumericWheelAdapter(1,31));} else if (list_little.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {wv_day.setAdapter(new NumericWheelAdapter(1,30));} else {if ((year_num % 4 == 0 && year_num % 100 != 0)|| year_num % 400 == 0)wv_day.setAdapter(new NumericWheelAdapter(1,29));elsewv_day.setAdapter(new NumericWheelAdapter(1,28));}}};// 添加"月"监听private final OnWheelChangedListener wheelListener_month = new OnWheelChangedListener() {@Overridepublic void onChanged(WheelView wheel, int oldValue, int newValue) {int month_num = newValue + 1;// 判断大小月及是否闰年,用来确定"日"的数据if (list_big.contains(String.valueOf(month_num))) {wv_day.setAdapter(new NumericWheelAdapter(1, 31));} else if (list_little.contains(String.valueOf(month_num))) {wv_day.setAdapter(new NumericWheelAdapter(1, 30));} else {if (((wv_year.getCurrentItem() + START_YEAR) % 4 == 0 && (wv_year.getCurrentItem() + START_YEAR) % 100 != 0)|| (wv_year.getCurrentItem() + START_YEAR) % 400 == 0)wv_day.setAdapter(new NumericWheelAdapter(1, 29));elsewv_day.setAdapter(new NumericWheelAdapter(1, 28));}}};private void setPositiveButton(CharSequence mPositiveButtonText,View.OnClickListener onClickListener) {positiveText = mPositiveButtonText;positiveClickListener = onClickListener;// can't use btn_sure here// because it's on defined yet}private void setNegativeButton(CharSequence mNegativeButtonText,View.OnClickListener onClickListener) {negativeText = mNegativeButtonText;negativeClickListener = onClickListener;// can't use btn_sure here// because it's on defined yet}private void setCalendar(Calendar calendar) {// TODO Auto-generated method stubthis.calendar = calendar;}public DatePickWheelDialog setDate(Calendar calendar) {if (calendar == null)return this;int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH);int day = calendar.get(Calendar.DAY_OF_MONTH);wv_year.setCurrentItem(year - START_YEAR);// 初始化时显示的数据wv_month.setCurrentItem(month);if (list_big.contains(String.valueOf(month + 1))) {wv_day.setAdapter(new NumericWheelAdapter(1, 31));} else if (list_little.contains(String.valueOf(month + 1))) {wv_day.setAdapter(new NumericWheelAdapter(1, 30));} else {// 闰年if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)wv_day.setAdapter(new NumericWheelAdapter(1, 29));elsewv_day.setAdapter(new NumericWheelAdapter(1, 28));}wv_day.setCurrentItem(day-1);return this;}public Calendar getSetCalendar() {// TODO Auto-generated method stubCalendar c = Calendar.getInstance();c.set(wv_year.getCurrentItem() + START_YEAR, wv_month.getCurrentItem(),wv_day.getCurrentItem() + 1);return c;}public static class Builder {private final DatePickParams P;public Builder(Context context) {P = new DatePickParams(context);}public Builder setTitle(CharSequence title) {P.mTitle = title;return this;}public Builder setIcon(int iconId) {P.mIconId = iconId;return this;}public Builder setPositiveButton(CharSequence text,final View.OnClickListener listener) {P.mPositiveButtonText = text;P.mPositiveButtonListener = listener;return this;}public Builder setNegativeButton(CharSequence text,final View.OnClickListener listener) {// TODO Auto-generated method stubP.mNegativeButtonText = text;P.mNegativeButtonListener = listener;return this;}public DatePickWheelDialog create() {final DatePickWheelDialog dialog = new DatePickWheelDialog(P.mContext);P.apply(dialog);return dialog;}}public static class DatePickParams {public int mIconId;public View.OnClickListener mPositiveButtonListener;public CharSequence mPositiveButtonText;public CharSequence mTitle;public final Context mContext;public Calendar calendar;private CharSequence mNegativeButtonText;private View.OnClickListener mNegativeButtonListener;public DatePickParams(Context context) {mContext = context;calendar = Calendar.getInstance();};public DatePickParams(Context context, Calendar calendar) {mContext = context;this.calendar = calendar;}public void apply(DatePickWheelDialog dialog) {if (mTitle != null) {dialog.setTitle(mTitle);}if (mPositiveButtonText != null) {dialog.setPositiveButton(mPositiveButtonText,mPositiveButtonListener);}if (mNegativeButtonText != null) {dialog.setNegativeButton(mNegativeButtonText,mNegativeButtonListener);}if (calendar != null)dialog.setCalendar(calendar);}}}

布局:date_layout.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="wrap_content" android:layout_width="fill_parent"><LinearLayout android:layout_height="wrap_content" android:layout_width="400dip"android:layout_gravity="center_horizontal" android:paddingLeft="12dp"android:paddingRight="12dp" android:paddingTop="10dp" android:id="@+id/timePicker1"><cn.gsss.iot.widgets.WheelViewandroid:id="@+id/year" android:layout_height="wrap_content"android:layout_width="0dp" android:layout_weight="2"/><cn.gsss.iot.widgets.WheelViewandroid:id="@+id/month" android:layout_height="wrap_content"android:layout_width="0dp" android:layout_weight="1"/><cn.gsss.iot.widgets.WheelViewandroid:id="@+id/day" android:layout_height="wrap_content"android:layout_width="0dp" android:layout_weight="1"/></LinearLayout><LinearLayout     android:layout_width="match_parent"android:layout_height="wrap_content" android:layout_centerHorizontal="true"android:layout_below="@id/timePicker1" android:layout_marginTop="12dp"android:layout_centerInParent="true"android:orientation="horizontal"><Button     android:id="@+id/btn_datetime_sure"android:layout_width="wrap_content" android:visibility="gone"android:layout_weight="1" android:layout_height="wrap_content"/><Button android:id="@+id/btn_datetime_cancel"android:visibility="gone"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1" /></LinearLayout></RelativeLayout>


时间选择器:

import java.util.Calendar;import java.util.HashMap;import cn.gsss.iot.R;import android.app.Activity;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.View;import android.widget.Button;public class TimePickWheelDialog extends Dialog {private WheelView wv_hour;private WheelView wv_min;private WheelView wv_sec;private final Context mContext;private Button btn_sure;private CharSequence positiveText;private Button btn_cancel;private CharSequence negativeText;private Calendar calendar;private View.OnClickListener positiveClickListener;private View.OnClickListener negativeClickListener;public TimePickWheelDialog(Context context) {super(context);// TODO Auto-generated constructor stubthis.mContext = context;}public TimePickWheelDialog(Context context, Calendar instance) {// TODO Auto-generated constructor stubthis(context);calendar = instance;}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.test_time);findView();adjustView();setListener();//setDate(calendar);System.out.println(calendar.toString());}private void adjustView() {// TODO Auto-generated method stub// 根据屏幕密度来指定选择器字体的大小int textSize = 0;textSize = pixelsToDip(mContext, 15);wv_hour.TEXT_SIZE = textSize;wv_min.TEXT_SIZE = textSize;wv_sec.TEXT_SIZE = textSize;}public static int pixelsToDip(Context context, int pixels) {DisplayMetrics mDisplayMetrics = new DisplayMetrics();((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);        int screenw = mDisplayMetrics.widthPixels;        float rate=(float)screenw/320;float paintsize=13*rate;//final float scale = res.getDisplayMetrics().density;//return (int) (pixels * scale + 0.5f);return (int)paintsize;}private void setListener() {// TODO Auto-generated method stub// 取消if (negativeClickListener != null) {btn_cancel.setOnClickListener(negativeClickListener);} else {btn_cancel.setOnClickListener(dismissListener);}if (positiveClickListener != null) {btn_sure.setOnClickListener(positiveClickListener);} else {btn_sure.setOnClickListener(dismissListener);}}private final View.OnClickListener dismissListener = new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubdismiss();}};private void findView() {// TODO Auto-generated method stub// 小时wv_hour = (WheelView) findViewById(R.id.hour);wv_hour.setAdapter(new NumericWheelAdapter(0,23));wv_hour.setLabel("时");// 添加文字// 分钟wv_min = (WheelView) findViewById(R.id.minute);wv_min.setAdapter(new NumericWheelAdapter(0, 59, "%02d"));wv_min.setLabel("分");// 秒wv_sec = (WheelView) findViewById(R.id.second);wv_sec.setAdapter(new NumericWheelAdapter(0, 59, "%02d"));wv_sec.setLabel("秒");btn_sure = (Button) findViewById(R.id.btn_datetime_sure);if (positiveText != null) {btn_sure.setVisibility(View.VISIBLE);btn_sure.setText(positiveText);}btn_cancel = (Button) findViewById(R.id.btn_datetime_cancel);if (negativeText != null) {btn_cancel.setVisibility(View.VISIBLE);btn_cancel.setText(negativeText);}}private void setPositiveButton(CharSequence mPositiveButtonText,View.OnClickListener onClickListener) {positiveText = mPositiveButtonText;positiveClickListener = onClickListener;// can't use btn_sure here// because it's on defined yet}private void setNegativeButton(CharSequence mNegativeButtonText,View.OnClickListener onClickListener) {negativeText = mNegativeButtonText;negativeClickListener = onClickListener;// can't use btn_sure here// because it's on defined yet}private void setCalendar(Calendar calendar) {// TODO Auto-generated method stubthis.calendar = calendar;}public TimePickWheelDialog setDate(Calendar calendar,int id) {if (calendar == null)return this;int year = calendar.get(Calendar.HOUR_OF_DAY);int month = calendar.get(Calendar.MINUTE);int day = calendar.get(Calendar.SECOND);if (id==R.id.videoca_starttime) {wv_hour.setCurrentItem(year-1);// 初始化时显示的数据}else {wv_hour.setCurrentItem(year);// 初始化时显示的数据}wv_min.setCurrentItem(month);wv_sec.setCurrentItem(day);return this;}public HashMap<String, String> getSetCalendar() {// TODO Auto-generated method stubHashMap<String, String> c = new HashMap<String, String>();c.put("hour", wv_hour.getCurrentItem()+"");c.put("minute", wv_min.getCurrentItem()+"");c.put("second", wv_sec.getCurrentItem()+"");/*c.set(wv_hour.getCurrentItem(), wv_min.getCurrentItem(),wv_sec.getCurrentItem());System.out.println(wv_hour.getCurrentItem()+""+ wv_min.getCurrentItem()+""+wv_sec.getCurrentItem());*/return c;}public static class Builder {private final TimePickParams P;public Builder(Context context) {P = new TimePickParams(context);}public Builder setTitle(CharSequence title) {P.mTitle = title;return this;}public Builder setIcon(int iconId) {P.mIconId = iconId;return this;}public Builder setPositiveButton(CharSequence text,final View.OnClickListener listener) {P.mPositiveButtonText = text;P.mPositiveButtonListener = listener;return this;}public Builder setNegativeButton(CharSequence text,final View.OnClickListener listener) {// TODO Auto-generated method stubP.mNegativeButtonText = text;P.mNegativeButtonListener = listener;return this;}public TimePickWheelDialog create() {final TimePickWheelDialog dialog = new TimePickWheelDialog(P.mContext);P.apply(dialog);return dialog;}}public static class TimePickParams {public int mIconId;public View.OnClickListener mPositiveButtonListener;public CharSequence mPositiveButtonText;public CharSequence mTitle;public final Context mContext;public Calendar calendar;private CharSequence mNegativeButtonText;private View.OnClickListener mNegativeButtonListener;public TimePickParams(Context context) {mContext = context;calendar = Calendar.getInstance();};public TimePickParams(Context context, Calendar calendar) {mContext = context;this.calendar = calendar;}public void apply(TimePickWheelDialog dialog) {if (mTitle != null) {dialog.setTitle(mTitle);}if (mPositiveButtonText != null) {dialog.setPositiveButton(mPositiveButtonText,mPositiveButtonListener);}if (mNegativeButtonText != null) {dialog.setNegativeButton(mNegativeButtonText,mNegativeButtonListener);}if (calendar != null)dialog.setCalendar(calendar);}}}


布局:time_layout.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="wrap_content" android:layout_width="fill_parent"><LinearLayout android:layout_height="wrap_content" android:layout_width="400dip"android:layout_gravity="center_horizontal" android:paddingLeft="12dp"android:paddingRight="12dp"android:paddingTop="10dp" android:id="@+id/timePicker1"><cn.gsss.iot.widgets.WheelViewandroid:id="@+id/hour" android:layout_height="wrap_content"android:layout_width="0dp" android:layout_weight="1"/><cn.gsss.iot.widgets.WheelViewandroid:id="@+id/minute" android:layout_height="wrap_content"android:layout_width="0dp" android:layout_weight="1"/><cn.gsss.iot.widgets.WheelViewandroid:id="@+id/second" android:layout_height="wrap_content"android:layout_width="0dp" android:layout_weight="1"/></LinearLayout><LinearLayout     android:layout_width="match_parent"android:layout_height="wrap_content" android:layout_centerHorizontal="true"android:layout_below="@id/timePicker1" android:layout_marginTop="12dp"android:layout_centerInParent="true"android:orientation="horizontal"><Button     android:id="@+id/btn_datetime_sure"android:layout_width="wrap_content" android:visibility="gone"android:layout_weight="1" android:layout_height="wrap_content"/><Button android:id="@+id/btn_datetime_cancel"android:visibility="gone"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1" /></LinearLayout></RelativeLayout>


根据屏幕设置字体大小要将  WheelView 里的 

private static final int TEXT_SIZE = 40;
改为
public int TEXT_SIZE =0;

记录备忘!

0 0
原创粉丝点击