你好,游客 登录 注册 搜索
背景:
阅读新闻

Android仿iPhone滚轮控件实现及源码分析

[日期:2012-06-18] 来源:Linux社区  作者:chengxibeauty [字体:大 中 小]

就是说这个类可以为按钮或者背景等提供渐变颜色的绘制。

TextPaint的概述:

TextPaint is an extension of Paint that leaves room for some extra data used during text measuring and drawing.

TextPaint是Paint类的一个扩展,主要是用于文本在绘制的过程中为附件的数据留出空间。

GestureDetector:手势检测,看下开发文档中关于该类的概述:

Detects various gestures and events using the supplied MotionEvents. The GestureDetector.OnGestureListener callback will notify users when a particular motion event has occurred. This class should only be used with MotionEvents reported via touch (don't use for trackball events).  

为各种手势和事件提供MotionEvents。当一个具体的事件发生时会调用回调函数GestureDetector.OnGestureListener。这个类应该只适用于MotionEvents通过触摸触发的事件(不要使用追踪事件)。

140行到156行是构造方法,175到183行是set和getAdapter。在193行,setInterpolator()方法,设置interPolator这个动画接口,我们看下这个接口的概述:

An interpolator defines the rate of change of an animation. This allows the basic animation effects (alpha, scale, translate, rotate) to be accelerated, decelerated, repeated, etc.

定义了一种基于变率的一个动画。这使得基本的动画效果(alpha, scale, translate, rotate)是加速,减慢,重复等。这个方法在随机数这个例子中被使用。

203行到213行设置显示的item条数。在setVisibleItems()方法里面调用了View的invalidate()方法,看下文档中对该方法的介绍:

Invalidate the whole view. If the view is visible, onDraw(Android.graphics.Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().

使全部视图失效,如果View视图是可见的,会在UI线程里面从新调用onDraw()方法。

223行到233行是设置Label,既后面图片中的hours.

245行到296行是设置监听,在上面已经简单的说了一下,这里不在累述。

307行到349行是设置正被选中item,就是在那个阴影条框下的那个部分,比较简单。里面主要调用了scroll这个方法:

  1. /** 
  2.      * Scroll the wheel 
  3.      * @param itemsToSkip items to scroll 
  4.      * @param time scrolling duration 
  5.      */  
  6.     public void scroll(int itemsToScroll, int time) {  
  7.         scroller.forceFinished(true);  
  8.         lastScrollY = scrollingOffset;  
  9.         int offset = itemsToScroll * getItemHeight();         
  10.         scroller.startScroll(0, lastScrollY, 0, offset - lastScrollY, time);  
  11.         setNextMessage(MESSAGE_SCROLL);       
  12.         startScrolling();  
  13.     }  

357行到365行是设置item数据能否循环使用。

384行的initResourcesIfNecessary()方法,从字面意思,如果需要的初始化资源。

  1. private void initResourcesIfNecessary() {  
  2.         if (itemsPaint == null) {  
  3.             itemsPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG  
  4.                     | Paint.FAKE_BOLD_TEXT_FLAG);  
  5.             //itemsPaint.density = getResources().getDisplayMetrics().density;   
  6.             itemsPaint.setTextSize(TEXT_SIZE);  
  7.         }  
  8.   
  9.         if (valuePaint == null) {  
  10.             valuePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG  
  11.                     | Paint.FAKE_BOLD_TEXT_FLAG | Paint.DITHER_FLAG);  
  12.             //valuePaint.density = getResources().getDisplayMetrics().density;   
  13.             valuePaint.setTextSize(TEXT_SIZE);  
  14.             valuePaint.setShadowLayer(0.1f, 0, 0.1f, 0xFFC0C0C0);  
  15.         }  
  16.   
  17.         if (centerDrawable == null) {  
  18.             centerDrawable = getContext().getResources().getDrawable(R.drawable.wheel_val);  
  19.         }  
  20.   
  21.         if (topShadow == null) {  
  22.             topShadow = new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);  
  23.         }  
  24.   
  25.         if (bottomShadow == null) {  
  26.             bottomShadow = new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);  
  27.         }  
  28.   
  29.         setBackgroundResource(R.drawable.wheel_bg);  
  30.     }  

这个方法就是初始化在532行calculateLayoutWidth()方法中调用了这个方法,同时调用了487行的getMaxTextLength()这个方法。

471行getTextItem(int index)通过一个索引获取该item的文本。

这是第一部分,没有多少有太多意思的地方,重点的地方在以后532行到940行的内容,另起一篇,开始分析,这一篇先到这。

最后是下载地址:

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/6月/18日/Android仿iPhone滚轮控件实现及源码分析/

linux
相关资讯       Android源码 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款