Android 实现 WheelView
我们都知道,在iOS里面有一种控件------滚筒控件(Wheel View),这通常用于设置时间/日期,非常方便,但Android SDK并没有提供类似的控件。这里介绍一下如何Android实现WheelView。
先来看一看iOS中的WheelView的效果图:

这个效果不错吧,我们应该如何实现呢?
那在Android如果也要实现这样一个效果,应该怎么做呢?
1.Android WheelView效果图

2.网上的开源代码
我们从网上找到了一个开源的代码,它也实现了这样的效果,而且效果也不错,大家可以用SVN来checkout:
http://android-wheel.googlecode.com/svn/trunk
它这个Demo最本质是自己写布局,好像是利用一个LinearLayout来布局child,然后调用LinearLayout.draw(canvas)方法,把child绘制在指定的canvas上面。它同时还提供了类似AdapterView的访问方式,用户可以设置Adapter来提供数据。我在这里主要不是讲解这个Demo的结构,如果大家感兴趣,可以自己下载代码研究。
3.实现思路
由于我之前修改过Gallery的源代码,可以使其循环滚动,并且第一个child可以排列在最左端,所以,我在想,如果我能把Gallery修改成竖的(垂直排列),那这个不就是OK了吗?基于这样的想法,我就准备修改代码了。
我们这里需要把Gallery的源码复制到我们的工程中,然后修改,保证能编译通过。
与Gallery相关的的几个文件如下所示,它们都是放在widget文件夹和res/value文件夹下面。
- AbsSpinner.java
- AdapterView.java
- Gallery.java
- attr.xml
修改的过程比较麻烦,我这里不详细说明(要细说的话,内容太多了),在修改之后,我们的Gallery提供了一个方法:setOrientation(int),你可以让这个Gallery水平滑动,也可以垂直滑动。
我们还应该提供以下几个核心方法:
- setOnEndFlingListener ------ 当Gallery停止滑动时的回调用,这样调用者可以在停止滑动时来得到当前选中的项。
- setOrientation(int) ------ 支持布局方向:HORIZONTAL和VERTICAL。
- setScrollCycle(boolean) ------ 是否支持循环滑动。
- setSlotInCenter(boolean) ------ 是否让Gallery选中的项居中。
4. 扩展Gallery
- package com.nj1s.lib.widget;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Rect;
- import android.graphics.drawable.Drawable;
- import android.graphics.drawable.GradientDrawable;
- import android.graphics.drawable.GradientDrawable.Orientation;
- import android.util.AttributeSet;
- import android.view.Gravity;
- import android.view.View;
- import com.nj1s.lib.R;
- public class WheelView extends TosGallery
- {
- private Drawable mSelectorDrawable = null;
- private Rect mSelectorBound = new Rect();
- private GradientDrawable mTopShadow = null;
- private GradientDrawable mBottomShadow = null;
- private static final int[] SHADOWS_COLORS =
- {
- 0xFF111111,
- 0x00AAAAAA,
- 0x00AAAAAA
- };
- public WheelView(Context context)
- {
- super(context);
- initialize(context);
- }
- public WheelView(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- initialize(context);
- }
- public WheelView(Context context, AttributeSet attrs, int defStyle)
- {
- super(context, attrs, defStyle);
- initialize(context);
- }
- private void initialize(Context context)
- {
- this.setVerticalScrollBarEnabled(false);
- this.setSlotInCenter(true);
- this.setOrientation(TosGallery.VERTICAL);
- this.setGravity(Gravity.CENTER_HORIZONTAL);
- this.setUnselectedAlpha(1.0f);
- // This lead the onDraw() will be called.
- this.setWillNotDraw(false);
- // The selector rectangle drawable.
- this.mSelectorDrawable =
- getContext().getResources().getDrawable(R.drawable.wheel_val);
- this.mTopShadow =
- new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);
- this.mBottomShadow =
- new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);
- // The default background.
- this.setBackgroundResource(R.drawable.wheel_bg);
- }
- @Override
- protected void dispatchDraw(Canvas canvas)
- {
- super.dispatchDraw(canvas);
- // After draw child, we do the following things:
- // +1, Draw the center rectangle.
- // +2, Draw the shadows on the top and bottom.
- drawCenterRect(canvas);
- drawShadows(canvas);
- }
- /**
- * setOrientation
- */
- @Override
- public void setOrientation(int orientation)
- {
- if (TosGallery.HORIZONTAL == orientation)
- {
- throw new IllegalArgumentException("The orientation must be VERTICAL");
- }
- super.setOrientation(orientation);
- }
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b)
- {
- super.onLayout(changed, l, t, r, b);
- int galleryCenter = getCenterOfGallery();
- View v = this.getChildAt(0);
- int height = (null != v) ? v.getMeasuredHeight() : 50;
- int top = galleryCenter - height / 2;
- int bottom = top + height;
- mSelectorBound.set(
- getPaddingLeft(),
- top,
- getWidth() - getPaddingRight(),
- bottom);
- }
- private void drawCenterRect(Canvas canvas)
- {
- if (null != mSelectorDrawable)
- {
- mSelectorDrawable.setBounds(mSelectorBound);
- mSelectorDrawable.draw(canvas);
- }
- }
- private void drawShadows(Canvas canvas)
- {
- int height = (int)(2.0 * mSelectorBound.height());
- mTopShadow.setBounds(0, 0, getWidth(), height);
- mTopShadow.draw(canvas);
- mBottomShadow.setBounds(0, getHeight() - height, getWidth(), getHeight());
- mBottomShadow.draw(canvas);
- }
- }
上面代码没有什么特别的东西,只是有几点需要注意:
5. 如何使用
- // 设置listener
- mDateWheel.setOnEndFlingListener(mListener);
- // 设置滑动时的声音
- mDateWheel.setSoundEffectsEnabled(true);
- // 设置adapter
- mDateWheel.setAdapter(new WheelTextAdapter(this));
- // Adapter的实现
- protected class WheelTextAdapter extends BaseAdapter
- {
- ArrayList<TextInfo> mData = null;
- int mWidth = ViewGroup.LayoutParams.MATCH_PARENT;
- int mHeight = 50;
- Context mContext = null;
- public WheelTextAdapter(Context context)
- {
- mContext = context;
- }
- public void setData(ArrayList<TextInfo> data)
- {
- mData = data;
- this.notifyDataSetChanged();
- }
- public void setItemSize(int width, int height)
- {
- mWidth = width;
- mHeight = height;
- }
- @Override
- public int getCount()
- {
- return (null != mData) ? mData.size() : 0;
- }
- @Override
- public Object getItem(int position)
- {
- return null;
- }
- @Override
- public long getItemId(int position)
- {
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent)
- {
- TextView textView = null;
- if (null == convertView)
- {
- convertView = new TextView(mContext);
- convertView.setLayoutParams(new TosGallery.LayoutParams(mWidth, mHeight));
- textView = (TextView)convertView;
- textView.setGravity(Gravity.CENTER);
- textView.setTextSize(26);
- textView.setTextColor(Color.BLACK);
- }
- if (null == textView)
- {
- textView = (TextView)convertView;
- }
- TextInfo info = mData.get(position);
- textView.setText(info.mText);
- textView.setTextColor(info.mColor);
- return convertView;
- }
- }
Android 实现 WheelView的更多相关文章
- android: WheelView组件(滑轮组件)的应用!
android前段组件中, 填表单,选择条目 的样式有很多, WheelView滚动组件为其中一种,如下图所示: 前两 ...
- 揭破android中的闹钟app 二
· 这节,我们通过wheelview来模仿一个简易的正点闹钟. 我这里不说wheelview来龙去脉,只阐述几个简单的方法,如果,想看一看具体wheelview的内容,请看下面两篇文章: androi ...
- android 时间控件概述
android的自带时间选择控件,是一个让用户既能输入的又能选择的样子.这本来没有太大的问题了. 但是,坑爹的android是开源的.自带的时间控件在某些机型上,早已经是面目全非了,在用以一个普通用户 ...
- VLC For Android Ubuntu14.04编译环境搭建
VLC多媒体播放器(英语:VLC media player,最初为VideoLAN Client.是VideoLAN计划的开放源码多媒体播放器.)支持众多音频与视频解码器及文件格式,并支持DVD影音光 ...
- 7.Android开源项目WheelView的时间和地址联动选择对话框
类似WheelView的时间和地址联动选择对话框在现在App经常看到,今天小结下. 主布局界面: <LinearLayout xmlns:android="http://schemas ...
- 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件
博客地址 : http://blog.csdn.net/shulianghan/article/details/41520569 代码下载 : -- GitHub : https://github.c ...
- Android自己定义实现循环滚轮控件WheelView
首先呈上效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- [Android]竖直滑动选择器WheelView的实现
以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3819304.html 公司项目中有这么一个需求,所以需要自己实现下.效 ...
- android wheelview实现三级城市选择
很早之前看淘宝就有了ios那种的城市选择控件,当时也看到网友有分享,不过那个写的很烂,后来(大概是去年吧),我们公司有这么一个项目,当时用的还是网上比较流行的那个黑框的那个,感觉特别的丑,然后我在那个 ...
随机推荐
- Oracle EBS OPM update material txn
--update_material_txn --created by jenrry DECLARE p_mmti_rec mtl_transactions_interface%ROWTYPE; p_m ...
- 超经典sql练习题,在teradata上实现
题目来源:https://blog.csdn.net/flycat296/article/details/63681089 teradata实现: drop table student; create ...
- 通过HTTP参数污染绕过WAF拦截 (转)
上个星期我被邀请组队去参加一个由CSAW组织的CTF夺旗比赛.因为老婆孩子的缘故,我只能挑一个与Web漏洞利用相关的题目,名字叫做”HorceForce”.这道题价值300点.这道题大概的背景是,你拥 ...
- 转:log4net使用详解
说明:本程序演示如何利用log4net记录程序日志信息.log4net是一个功能著名的开源日志记录组件.利用log4net可以方便地将日志信息记录到文件.控制台.Windows事件日志和数据库(包括M ...
- django导入自定义模块
自定义模块cust.py位于应用aptest目录下 1.编辑settings.py from aptest import cust 2.编辑views.py from cust import pc # ...
- 大数据开发实战:HDFS和MapReduce优缺点分析
一. HDFS和MapReduce优缺点 1.HDFS的优势 HDFS的英文全称是 Hadoop Distributed File System,即Hadoop分布式文件系统,它是Hadoop的核心子 ...
- VMware 导出镜像文件供 Virtual Box 使用
1. 问题描述 Windows 系统安装的 VMware 里的安装配置好的虚拟机需要拷贝到 MAC 的 Virtual Box 中. 需要将 VMware 中的虚拟机导出为镜像文件供 Virtual ...
- ansible 碎记录
https://www.zhukun.net/archives/8167 ansible -i new/hosts new -m authorized_key -a "user=root k ...
- .net core 入坑经验 - 2、MVC Core之获取网站运行路径
这次是建立了asp.net mvc core项目,在controller中想获取网站在硬盘中的路径,找了一圈Server.MapPath() 已不存在,HttpContent也一样,经过查阅资料发现是 ...
- C语言实现输出杨辉三角
1.倒推法实现输出杨辉三角右半部分,代码如下: #include<stdio.h> int main() { ]; printf("请输入行数n:"); scanf(& ...