【Android源码 栏目提醒】:网学会员,鉴于大家对Android源码 十分关注,论文会员在此为大家搜集整理了“android仿iPhone滚轮控件实现及源码分析(一) - 企业软件开发”一文,供大家参考学习!
敬告由于本文代码较多所以文章分为了一二两篇如果不便敬请谅解可以先下载文章下方的代码打开参考本文查看效果更好首先先看下效果图这三张图分别是使用滚动控件实现城市随机数和时间三个简单的例子当然界面有点简陋下面我们就以时间这个为例开始解析一下。
首先先看下布局文件里面只有三个控件两个自定义的WheelView还有一个TimePicker然后进入代码里面看一下看一下里面调用WheelView的方法有setAdapter、setLabelquotminsquot、setCyclictrue、setCurrentItem、getCurrentItem、addChangingListener、addScrollingListener这些方法其中setAapter设置数据适配器setCyclic设置是否是循环setCurrentItem和getCurrentItem分别是设置现在选择的item和返回现在选择的item。
后面两个设置监听的方法中需要重写两个接口和在这里使用的是典型的回调方法模式。
然后现在我们进入WheelView类看一下他是如何构建首先WheelView继承了View类。
代码的22行到45行是导入的所需要的类。
从54行到135行是声明一些变量和类在这里面使用到了StaticLayout在开发文档中找一下这个类staticLayout被创建以后就不能被修改了通常被用于控制文本组件布局。
还使用到了Drawable、TextPaint、GradientDrawable、GestureDetector、Scroller类在开发文档中GradientDrawable的概述就是说这个类可以为按钮或者背景等提供渐变颜色的绘制。
TextPaint的概述TextPaint是Paint类的一个扩展主要是用于文本在绘制的过程中为附件的数据留出空间。
GestureDetector:手势检测看下开发文档中关于该类的概述为各种手势和事件提供MotionEvents。
当一个具体的事件发生时会调用回调函数GestureDetector.OnGestureListener。
这个类应该只适用于MotionEvents通过触摸触发的事件不要使用追踪事件。
140行到156行是构造方法175到183行是set和getAdapter。
在193行setInterpolator方法设置interPolator这个动画接口我们看下这个接口的概述定义了一种基于变率的一个动画。
这使得基本的动画效果alpha scale translate rotate是加速减慢重复等。
这个方法在随机数这个例子中被使用。
203行到213行设置显示的item条数。
在setVisibleItems方法里面调用了View的invalidate方法看下文档中对该方法的介绍使全部视图失效如果View视图是可见的会在UI线程里面从新调用onDraw方法。
223行到233行是设置Label既后面图片中的hours.245行到296行是设置监听在上面已经简单的说了一下这里不在累述。
307行到349行是设置正被选中item就是在那个阴影条框下的那个部分比较简单。
里面主要调用了scroll这个方法357行到365行是设置item数据能否循环使用。
384行的initResourcesIfNecessary方法从字面意思如果需要的初始化资源。
这个方法就是初始化在532行calculateLayoutWidth方法中调用了这个方法同时调用了487行的getMaxTextLength这个方法。
471行getTextItemint index通过一个索引获取该item的文本。
这是第一部分没有多少有太多意思的地方重点的地方在以后532行到940行的内容另起一篇开始分析这一篇先到这。
最后是下载地址http://download.csdn.net/detail/aomandeshangxiao/4175719未完待续敬请下篇
android仿iPhone滚轮控件实现及
源码分析二html1.ltxmlversionquot1.0quotencodingquotutf-8quotgt2.3.ltLinearLayoutxmlns:androidquothttp://schemas.
android.com/apk/res/androidquot4.
android:layout_heightquotwrap_contentquot5.
android:layout_widthquotfill_parentquot6.
android:layout_marginTopquot12dpquot7.
android:orientationquotverticalquot8.
android:backgroundquotdrawable/layout_bgquotgt9.10.ltLinearLayoutxmlns:androidquothttp://schemas.
android.com/apk/res/androidquot11.
android:layout_heightquotwrap_contentquot12.
android:layout_widthquotfill_parentquot13.
android:layout_gravityquotcenter_horizontalquot14.
android:paddingLeftquot12dpquot15.
android:paddingRightquot12dpquot16.
android:paddingTopquot10dpquotgt17.18.ltkankan.wheel.widget.WheelViewandroid:idquotid/hourquot19.
android:layout_heightquotwrap_contentquot20.
android:layout_widthquotfill_parentquot21.
android:layout_weightquot1quot/gt22.ltkankan.wheel.widget.WheelViewandroid:idquotid/minsquot23.
android:layout_heightquotwrap_contentquot24.
android:layout_widthquotfill_parentquot25.
android:layout_weightquot1quot/gt26.lt/LinearLayoutgt27.28.ltTimePickerandroid:idquotid/timequot29.
android:layout_marginTopquot12dpquot30.
android:layout_heightquotwrap_contentquot31.
android:layout_widthquotfill_parentquot32.
android:layout_weightquot1quot/gt33.34.lt/LinearLayoutgtjava1.publicclassTimeActivityextendsActivity2.//Timechangedflag 3.privatebooleantimeChangedfalse4.5.// 6.privatebooleantimeScrolledfalse7.8.Override9.publicvoidonCreateBundlesavedInstanceState10.super.onCreatesavedInstanceState11.12.setContentViewR.layout.time_layout13.14.finalWheelViewhoursWheelViewfindViewByIdR.id.hour15.hours.setAdapternewNumericWheelAdapter02316.hours.setLabelquothoursquot17.18.finalWheelViewminsWheelViewfindViewByIdR.id.mins19.mins.setAdapternewNumericWheelAdapter059quot02dquot20.mins.setLabelquotminsquot21.mins.setCyclictrue22.23.finalTimePickerpickerTimePickerfindViewByIdR.id.time24.picker.setIs24HourViewtrue25.26.//setcurrenttime 27.CalendarcCalendar.getInstance28.intcurHoursc.getCalendar.HOUR_OF_DAY29.intcurMinutesc.getCalendar.MINUTE30.31.hours.setCurrentItemcurHours32.mins.setCurrentItemcurMinutes33.34.picker.setCurrentHourcurHours35.picker.setCurrentMinutecurMinutes36.37.//addlisteners 38.addChangingListenerminsquotminquot39.addChangingListenerhoursquothourquot40.41.OnWheelChangedListenerwheelListenernewOnWheelChangedListener42.publicvoidonChangedWheelViewwheelintoldValueintnewValue43.iftimeScrolled44.timeChangedtrue45.picker.setCurrentHourhours.getCurrentItem46.picker.setCurrentMinutemins.getCurrentItem47.timeChangedfalse48.49.50.51.52.hours.addChangingListenerwheelListener53.mins.addChangingListenerwheelListener54.55.OnWheelScrollListenerscrollListenernewOnWheelScrollListener56.publicvoidonScrollingStartedWheelViewwheel57.timeScrolledtrue58.59.publicvoidonScrollingFinishedWheelViewwheel60.timeScrolledfalse61.timeChangedtrue62.picker.setCurrentHourhours.getCurrentItem63.picker.setCurrentMinutemins.getCurrentItem64.timeChangedfalse65.66.67.68.hours.addScrollingListenerscrollListener69.mins.addScrollingListenerscrollListener70.71.picker.setOnTimeChangedListenernewTimePicker.OnTimeChangedListener72.publicvoidonTimeChangedTimePickerviewinthourOfDayintminute73.iftimeChanged74.hours.setCurrentItemhourOfDaytrue75.mins.setCurrentItemminutetrue76.77.78.79.80.81./82.Addschanginglistenerforwheelthatupdatesthewheellabel83.paramwheelthewheel84.paramlabelthewheellabel85./86.privatevoidaddChangingListenerfinalWheelViewwheelfinalStringlabel87.wheel.addChangingListenernewOnWheelChangedListener88.publicvoidonChangedWheelViewwheelintoldValueintnewValue89.wheel.setLabelnewValue1labelquotsquot:label90.91.92.93.java1./2.Wheelscrolledlistenerinterface.3./4.publicinterfaceOnWheelScrollListener5./6.Callbackmethodtobeinvokedwhenscrollingstarted.7.paramwheelthewheelviewwhosestatehaschanged.8./9.voidonScrollingStartedWheelViewwheel10.11./12.Callbackmethodtobeinvokedwhenscrollingended.13.paramwheelthewheelviewwhosestatehaschanged.14./15.voidonScrollingFinishedWheelViewwheel16.java1.publicinterfaceOnWheelChangedListener2./3.Callbackmethodtobeinvokedwhencurrentitemchanged4.paramwheelthewheelviewwhosestatehaschanged5.paramoldValuetheoldvalueofcurrentitem6.paramnewValuethenewvalueofcurrentitem7./8.voidonChangedWheelViewwheelintoldValueintnewValue9.java1./Scrollingduration/2.privatestaticfinalintSCROLLING_DURATION4003.4./Minimumdeltaforscrolling/5.privatestaticfinalintMIN_DELTA_FOR_SCROLLING16.7./Currentvalueamplabeltextcolor/8.privatestaticfinalintVALUE_TEXT_COLOR0xF00000009.10./Itemstextcolor/11.privatestaticfinalintITEMS_TEXT_COLOR0xFF00000012.13./Topandbottomshadowscolors/14.privatestaticfinalintSHADOWS_COLORSnewint0xFF11111115.0x00AAAAAA0x00AAAAAA16.17./Additionalitemsheightisaddedtostandardtextitemheight/18.privatestaticfinalintADDITIONAL_ITEM_HEIGHT1519.20./Textsize/21.privatestaticfinalintTEXT_SIZE2422.23./Topandbottomitemsoffsettohidethat/24.privatestaticfinalintITEM_OFFSETTEXT_SIZE/525.26./Additionalwidthforitemslayout/27.privatestaticfinalintADDITIONAL_ITEMS_SPACE1028.29./Labeloffset/30.privatestaticfinalintLABEL_OFFSET831.32./Leftandrightpaddingvalue/33.privatestaticfinalintPADDING1034.35./Defaultcountofvisibleitems/36.privatestaticfinalintDEF_VISIBLE_ITEMS537.38.//WheelValues 39.privateWheelAdapteradapternull40.privateintcurrentItem041.42.//Widths 43.privateintitemsWidth044.privateintlabelWidth045.46.//Countofvisibleitems 47.privateintvisibleItemsDEF_VISIBLE_ITEMS48.49.//Itemheight 50.privateintitemHeight051.52.//Textpaints 53.privateTextPaintitemsPaint54.privateTextPaintvaluePaint55.56.//Layouts 57.privateStaticLayoutitemsLayout58.privateStaticLayoutlabelLayout59.privateStaticLayoutvalueLayout60.61.//Labelampbackground 62.privateStringlabel63.privateDrawablecenterDrawable64.65.//Shadowsdrawables 66.privateGradientDrawabletopShadow67.privateGradientDrawablebottomShadow68.69.//Scrolling 70.privatebooleanisScrollingPerformed71.privateintscrollingOffset72.73.//Scrollinganimation 74.privateGestureDetectorgestureDetector75.privateScrollerscroller76.privateintlastScrollY77.78.//Cyclic 79.booleanisCyclicfalse80.81.//Listeners 82.privateListltOnWheelChangedListenergtchangingListenersnewLinkedListltOnWheelChangedListenergt83.privateListltOnWheelScrollListenergtscrollingListenersnewLinkedListltOnWheelScrollListenergtplain1.StaticLayoutisaLayoutfortextthatwillnotbeeditedafteritislaidout.UseDynamicLayoutfortextthatmaychange.2.3.Thisisusedbywidgetstocontroltextlayout.YoushouldnotneedtousethisclassdirectlyunlessyouareimplementingyourownwidgetorcustomdisplayobjectorwouldbetemptedtocallCanvas.drawTextdirectly.plain1.ADrawablewithacolorgradientforbuttonsbackgroundsetc.2.3.ItcanbedefinedinanXMLfilewiththeltshapegtelement.FormoreinformationseetheguidetoDrawableResources.plain1.TextPaintisanextensionofPaintthatleavesroomforsomeextradatausedduringtextmeasuringanddrawing.plain1.DetectsvariousgesturesandeventsusingthesuppliedMotionEvents.TheGestureDetector.OnGestureListenercallbackwillnotifyuserswhenaparticularmotioneventhasoccurred.ThisclassshouldonlybeusedwithMotionEventsreportedviatouchdontusefortrackballevents.plain1.Aninterpolatordefinestherateofchangeofananimation.Thisallowsthebasicanimationeffectsalphascaletranslaterotatetobeaccelerateddeceleratedrepeatedetc.plain1.Invalidatethewholeview.IftheviewisvisibleonDrawandroid.graphics.Canvaswillbecalledatsomepointinthefuture.ThismustbecalledfromaUIthread.Tocallfromanon-UIthreadcallpostInvalidate.java1./2.Scrollthewheel3.paramitemsToSkipitemstoscroll4.paramtimescrollingduration5./6.publicvoidscrollintitemsToScrollinttime7.scroller.forceFinishedtrue8.lastScrollYscrollingOffset9.intoffsetitemsToScrollgetItemHeight10.scroller.startScroll0lastScrollY0offset-lastScrollYtime11.setNextMessageMESSAGE_SCROLL12.startScrolling13.java1.privatevoidinitResourcesIfNecessary2.ifitemsPaintnull3.itemsPaintnewTextPaintPaint.ANTI_ALIAS_FLAG4.Paint.FAKE_BOLD_TEXT_FLAG5.//itemsPaint.densitygetResources.getDisplayMetrics.density 6.itemsPaint.setTextSizeTEXT_SIZE7.8.9.i.