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

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

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

我们看下重写的系统回调函数onMeasure()(用于测量各个控件距离,父子控件空间大小等):

  1. @Override  
  2.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  3.         int widthMode = MeasureSpec.getMode(widthMeasureSpec);  
  4.         int heightMode = MeasureSpec.getMode(heightMeasureSpec);  
  5.         int widthSize = MeasureSpec.getSize(widthMeasureSpec);  
  6.         int heightSize = MeasureSpec.getSize(heightMeasureSpec);  
  7.   
  8.         int width = calculateLayoutWidth(widthSize, widthMode);  
  9.   
  10.         int height;  
  11.         if (heightMode == MeasureSpec.EXACTLY) {  
  12.             height = heightSize;  
  13.         } else {  
  14.             height = getDesiredHeight(itemsLayout);  
  15.   
  16.             if (heightMode == MeasureSpec.AT_MOST) {  
  17.                 height = Math.min(height, heightSize);  
  18.             }  
  19.         }  
  20.   
  21.         setMeasuredDimension(width, height);  
  22.     }  

里面用到了532行calculateLayoutWidth()的方法,就是计算Layout的宽度,在calculateLayoutWidth()这个方法里面调用了

  1. /** 
  2.      * Creates layouts 
  3.      * @param widthItems width of items layout 
  4.      * @param widthLabel width of label layout 
  5.      */  
  6.     private void createLayouts(int widthItems, int widthLabel) {  
  7.         if (itemsLayout == null || itemsLayout.getWidth() > widthItems) {  
  8.             itemsLayout = new StaticLayout(buildText(isScrollingPerformed), itemsPaint, widthItems,  
  9.                     widthLabel > 0 ? Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_CENTER,  
  10.                     1, ADDITIONAL_ITEM_HEIGHT, false);  
  11.         } else {  
  12.             itemsLayout.increaseWidthTo(widthItems);  
  13.         }  
  14.   
  15.         if (!isScrollingPerformed && (valueLayout == null || valueLayout.getWidth() > widthItems)) {  
  16.             String text = getAdapter() != null ? getAdapter().getItem(currentItem) : null;  
  17.             valueLayout = new StaticLayout(text != null ? text : "",  
  18.                     valuePaint, widthItems, widthLabel > 0 ?  
  19.                             Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_CENTER,  
  20.                             1, ADDITIONAL_ITEM_HEIGHT, false);  
  21.         } else if (isScrollingPerformed) {  
  22.             valueLayout = null;  
  23.         } else {  
  24.             valueLayout.increaseWidthTo(widthItems);  
  25.         }  
  26.   
  27.         if (widthLabel > 0) {  
  28.             if (labelLayout == null || labelLayout.getWidth() > widthLabel) {  
  29.                 labelLayout = new StaticLayout(label, valuePaint,  
  30.                         widthLabel, Layout.Alignment.ALIGN_NORMAL, 1,  
  31.                         ADDITIONAL_ITEM_HEIGHT, false);  
  32.             } else {  
  33.                 labelLayout.increaseWidthTo(widthLabel);  
  34.             }  
  35.         }  
  36.     }  
linux
相关资讯       Android源码 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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