【Android源码 栏目提醒】:网学会员在Android源码 频道为大家收集整理了“android仿iPhone滚轮控件实现及源码分析(二) - 企业软件开发“提供大家参考,希望对大家有所帮助!
在上一篇
android仿iPhone滚轮控件实现及
源码分析一简单的说了下架构还有效果图但是关于图形的绘制各方面的代码在532行到940行如果写在一篇文章里面可能会导致文章太长效果不好所以自作聪明的分成了两篇。
闲言碎语不要讲下面开始正事。
首先先把代码贴出来在629行到744行的代码是绘制图形747行onTouchEvent里面主要是调用了882行的justify方法用于调整画面我们看下重写的系统回调函数onMeasure用于测量各个控件距离父子控件空间大小等里面用到了532行calculateLayoutWidth的方法就是计算Layout的宽度在calculateLayoutWidth这个方法里面调用了然后我们接着看onDraw方法在onDraw方法中也调用了CreateLayout方法然后在后面调用drawCenterRect、drawItems、drawValue、绘制阴影drawShadows两个方法主要就是通过canvas类进行图形的绘制。
最后我们看下840行定义的手势监听里面主要调用的方法clearMessages、startScrolling、doScroll、setNextMessage先看下中间的两个方法开始滑动和滑动在startScrolling方法里面有287行的notifyScrollingListenersAboutStart函数。
再看clearMessages、setMessageNext里面使用到了animationHandler用来传递动画有段的操作里面调用了finishScrolling完ps:天热烦躁说明较少如有问题敬请留言探讨很多不足之处望多多指教。
java1./2.Calculatescontrolwidthandcreatestextlayouts3.paramwidthSizetheinputlayoutwidth4.parammodethelayoutmode5.returnthecalculatedcontrolwidth6./7.privateintcalculateLayoutWidthintwidthSizeintmode8.initResourcesIfNecessary9.10.intwidthwidthSize11.intmaxLengthgetMaxTextLength12.ifmaxLengthgt013.floattextWidthFloatMath.ceilLayout.getDesiredWidthquot0quotitemsPaint14.itemsWidthintmaxLengthtextWidth15.else16.itemsWidth017.18.itemsWidthADDITIONAL_ITEMS_SPACE//makeitsomemore 19.20.labelWidth021.iflabelnullampamplabel.lengthgt022.labelWidthintFloatMath.ceilLayout.getDesiredWidthlabelvaluePaint23.24.25.booleanrecalculatefalse26.ifmodeMeasureSpec.EXACTLY27.widthwidthSize28.recalculatetrue29.else30.widthitemsWidthlabelWidth2PADDING31.iflabelWidthgt032.widthLABEL_OFFSET33.34.35.//Checkagainstourminimumwidth 36.widthMath.maxwidthgetSuggestedMinimumWidth37.38.ifmodeMeasureSpec.AT_MOSTampampwidthSizeltwidth39.widthwidthSize40.recalculatetrue41.42.43.44.ifrecalculate45.//recalculatewidth 46.intpureWidthwidth-LABEL_OFFSET-2PADDING47.ifpureWidthlt048.itemsWidthlabelWidth049.50.iflabelWidthgt051.doublenewWidthItemsdoubleitemsWidthpureWidth52./itemsWidthlabelWidth53.itemsWidthintnewWidthItems54.labelWidthpureWidth-itemsWidth55.else56.itemsWidthpureWidthLABEL_OFFSET//nolabel 57.58.59.60.ifitemsWidthgt061.createLayoutsitemsWidthlabelWidth62.63.64.returnwidth65.66.67./68.Createslayouts69.paramwidthItemswidthofitemslayout70.paramwidthLabelwidthoflabellayout71./72.privatevoidcreateLayoutsintwidthItemsintwidthLabel73.ifitemsLayoutnullitemsLayout.getWidthgtwidthItems74.itemsLayoutnewStaticLayoutbuildTextisScrollingPerformeditemsPaintwidthItems75.widthLabelgt0Layout.Alignment.ALIGN_OPPOSITE:Layout.Alignment.ALIGN_CENTER76.1ADDITIONAL_ITEM_HEIGHTfalse77.else78.itemsLayout.increaseWidthTowidthItems79.80.81.ifisScrollingPerformedampampvalueLayoutnullvalueLayout.getWidthgtwidthItems82.StringtextgetAdapternullgetAdapter.getItemcurrentItem:null83.valueLayoutnewStaticLayouttextnulltext:quotquot84.valuePaintwidthItemswidthLabelgt085.Layout.Alignment.ALIGN_OPPOSITE:Layout.Alignment.ALIGN_CENTER86.1ADDITIONAL_ITEM_HEIGHTfalse87.elseifisScrollingPerformed88.valueLayoutnull89.else90.valueLayout.increaseWidthTowidthItems91.92.93.ifwidthLabelgt094.iflabelLayoutnulllabelLayout.getWidthgtwidthLabel95.labelLayoutnewStaticLayoutlabelvaluePaint96.widthLabelLayout.Alignment.ALIGN_NORMAL197.ADDITIONAL_ITEM_HEIGHTfalse98.else99.labelLayout.increaseWidthTowidthLabel100.101.102.103.104.Override105.protectedvoidonMeasureintwidthMeasureSpecintheightMeasureSpec106.intwidthModeMeasureSpec.getModewidthMeasureSpec107.intheightModeMeasureSpec.getModeheightMeasureSpec108.intwidthSizeMeasureSpec.getSizewidthMeasureSpec109.intheightSizeMeasureSpec.getSizeheightMeasureSpec110.111.intwidthcalculateLayoutWidthwidthSizewidthMode112.113.intheight114.ifheightModeMeasureSpec.EXACTLY115.heightheightSize116.else117.heightgetDesiredHeightitemsLayout118.119.ifheightModeMeasureSpec.AT_MOST120.heightMath.minheightheightSize121.122.123.124.setMeasuredDimensionwidthheight125.126.127.Override128.protectedvoidonDrawCanvascanvas129.super.onDrawcanvas130.131.ifitemsLayoutnull132.ifitemsWidth0133.calculateLayoutWidthgetWidthMeasureSpec.EXACTLY134.else135.createLayoutsitemsWidthlabelWidth136.137.138.139.ifitemsWidthgt0140.canvas.save141.//Skippaddingspaceandhideapartoftopandbottomitems 142.canvas.translatePADDING-ITEM_OFFSET143.drawItemscanvas144.drawValuecanvas145.canvas.restore146.147.148.drawCenterRectcanvas149.drawShadowscanvas150.151.152./153.Drawsshadowsontopandbottomofcontrol154.paramcanvasthecanvasfordrawing155./156.privatevoiddrawShadowsCanvascanvas157.topShadow.setBounds00getWidthgetHeight/visibleItems158.topShadow.drawcanvas159.160.bottomShadow.setBounds0getHeight-getHeight/visibleItems161.getWidthgetHeight162.bottomShadow.drawcanvas163.164.165./166.Drawsvalueandlabellayout167.paramcanvasthecanvasfordrawing168./169.privatevoiddrawValueCanvascanvas170.valuePaint.setColorVALUE_TEXT_COLOR171.valuePaint.drawableStategetDrawableState172.173.RectboundsnewRect174.itemsLayout.getLineBoundsvisibleItems/2bounds175.176.//drawlabel 177.iflabelLayoutnull178.canvas.save179.canvas.translateitemsLayout.getWidthLABEL_OFFSETbounds.top180.labelLayout.drawcanvas181.canvas.restore182.183.184.//drawcurrentvalue 185.ifvalueLayoutnull186.canvas.save187.canvas.translate0bounds.topscrollingOffset188.valueLayout.drawcanvas189.canvas.restore190.191.192.193./194.Drawsitems195.paramcanvasthecanvasfordrawing196./197.privatevoiddrawItemsCanvascanvas198.canvas.save199.200.inttopitemsLayout.getLineTop1201.canvas.translate0-topscrollingOffset202.203.itemsPaint.setColorITEMS_TEXT_COLOR204.itemsPaint.drawableStategetDrawableState205.itemsLayout.drawcanvas206.207.canvas.restore208.209.210./211.Drawsrectforcurrentvalue212.paramcanvasthecanvasfordrawing213./214.privatevoiddrawCenterRectCanvascanvas215.intcentergetHeight/2216.intoffsetgetItemHeight/2217.centerDrawable.setBounds0center-offsetgetWidthcenteroffset218.centerDrawable.drawcanvas219.220.221.Override222.publicbooleanonTouchEventMotionEventevent223.WheelAdapteradaptergetAdapter224.ifadapternull225.returntrue226.227.228.ifgestureDetector.onTouchEventeventampampevent.getActionMotionEvent.ACTION_UP229.justify230.231.returntrue232.233.234./235.Scrollsthewheel236.paramdeltathescrollingvalue237./238.privatevoiddoScrollintdelta239.scrollingOffsetdelta240.241.intcountscrollingOffset/getItemHeight242.intposcurrentItem-count243.ifisCyclicampampadapter.getItemsCountgt0244.//fixpositionbyrotating 245.whileposlt0246.posadapter.getItemsCount247.248.posadapter.getItemsCount249.elseifisScrollingPerformed250.// 251.ifposlt0252.countcurrentItem253.pos0254.elseifposgtadapter.getItemsCount255.countcurrentItem-adapter.getItemsCount1256.posadapter.getItemsCount-1257.258.else259.//fixposition 260.posMath.maxpos0261.posMath.minposadapter.getItemsCount-1262.263.264.intoffsetscrollingOffset265.ifposcurrentItem266.setCurrentItemposfalse267.else268.invalidate269.270.271.//updateoffset 272.scrollingOffsetoffset-countgetItemHeight273.ifscrollingOffsetgtgetHeight274.scrollingOffsetscrollingOffsetgetHeightgetHeight275.276.277.278.//gesturelistener 279.privateSimpleOnGestureListenergestureListenernewSimpleOnGestureListener280.publicbooleanonDownMotionEvente281.ifisScrollingPerformed282.scroller.forceFinishedtrue283.clearMessages284.returntrue285.286.returnfalse287.288.289.publicbooleanonScrollMotionEvente1MotionEvente2floatdistanceXfloatdistanceY290.startScrolling291.doScrollint-distanceY292.returntrue293.294.295.publicbooleanonFlingMotionEvente1MotionEvente2floatvelocityXfloatvelocityY296.lastScrollYcurrentItemgetItemHeightscrollingOffset297.intmaxYisCyclic0x7FFFFFFF:adapter.getItemsCountgetItemHeight298.intminYisCyclic-maxY:0299.scroller.fling0lastScrollY0int-velocityY/200minYmaxY300.setNextMessageMESSAGE_SCROLL301.returntrue.
上一篇:
Android、Symbian、WM三大手机操作系统对比测评
下一篇:
天亮之后