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

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

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

然后我们接着看onDraw()方法:

  1. @Override  
  2.     protected void onDraw(Canvas canvas) {  
  3.         super.onDraw(canvas);  
  4.           
  5.         if (itemsLayout == null) {  
  6.             if (itemsWidth == 0) {  
  7.                 calculateLayoutWidth(getWidth(), MeasureSpec.EXACTLY);  
  8.             } else {  
  9.                 createLayouts(itemsWidth, labelWidth);  
  10.             }  
  11.         }  
  12.   
  13.         if (itemsWidth > 0) {  
  14.             canvas.save();  
  15.             // Skip padding space and hide a part of top and bottom items   
  16.             canvas.translate(PADDING, -ITEM_OFFSET);  
  17.             drawItems(canvas);  
  18.             drawValue(canvas);  
  19.             canvas.restore();  
  20.         }  
  21.   
  22.         drawCenterRect(canvas);  
  23.         drawShadows(canvas);  
  24.     }  

在onDraw方法中,也调用了CreateLayout()方法,然后在后面调用drawCenterRect()、drawItems()、drawValue()、绘制阴影drawShadows()两个方法:

  1. /** 
  2.      * Draws shadows on top and bottom of control 
  3.      * @param canvas the canvas for drawing 
  4.      */  
  5.     private void drawShadows(Canvas canvas) {  
  6.         topShadow.setBounds(0, 0, getWidth(), getHeight() / visibleItems);  
  7.         topShadow.draw(canvas);  
  8.   
  9.   
  10.         bottomShadow.setBounds(0, getHeight() - getHeight() / visibleItems,  
  11.                 getWidth(), getHeight());  
  12.         bottomShadow.draw(canvas);  
  13.     }  
  14.   
  15.   
  16.     /** 
  17.      * Draws value and label layout 
  18.      * @param canvas the canvas for drawing 
  19.      */  
  20.     private void drawValue(Canvas canvas) {  
  21.         valuePaint.setColor(VALUE_TEXT_COLOR);  
  22.         valuePaint.drawableState = getDrawableState();  
  23.   
  24.   
  25.         Rect bounds = new Rect();  
  26.         itemsLayout.getLineBounds(visibleItems / 2, bounds);  
  27.   
  28.   
  29.         // draw label   
  30.         if (labelLayout != null) {  
  31.             canvas.save();  
  32.             canvas.translate(itemsLayout.getWidth() + LABEL_OFFSET, bounds.top);  
  33.             labelLayout.draw(canvas);  
  34.             canvas.restore();  
  35.         }  
  36.   
  37.   
  38.         // draw current value   
  39.         if (valueLayout != null) {  
  40.             canvas.save();  
  41.             canvas.translate(0, bounds.top + scrollingOffset);  
  42.             valueLayout.draw(canvas);  
  43.             canvas.restore();  
  44.         }  
  45.     }  
  46.   
  47.   
  48.     /** 
  49.      * Draws items 
  50.      * @param canvas the canvas for drawing 
  51.      */  
  52.     private void drawItems(Canvas canvas) {  
  53.         canvas.save();  
  54.           
  55.         int top = itemsLayout.getLineTop(1);  
  56.         canvas.translate(0, - top + scrollingOffset);  
  57.           
  58.         itemsPaint.setColor(ITEMS_TEXT_COLOR);  
  59.         itemsPaint.drawableState = getDrawableState();  
  60.         itemsLayout.draw(canvas);  
  61.           
  62.         canvas.restore();  
  63.     }  
  64.   
  65.   
  66.     /** 
  67.      * Draws rect for current value 
  68.      * @param canvas the canvas for drawing 
  69.      */  
  70.     private void drawCenterRect(Canvas canvas) {  
  71.         int center = getHeight() / 2;  
  72.         int offset = getItemHeight() / 2;  
  73.         centerDrawable.setBounds(0, center - offset, getWidth(), center + offset);  
  74.         centerDrawable.draw(canvas);  
  75.     }  
linux
相关资讯       Android源码 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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