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

Android之收音机UI实现

[日期:2012-11-26] 来源:Linux社区  作者:weidi1989 [字体:大 中 小]

第三,是中间那个显示时间和调频自定义的View,DigitalTextView.java:

  1. /** 
  2.  * 自定义的一个显示调频或时间的view 
  3.  *  
  4.  * @author way 
  5.  *  
  6.  */ 
  7. public class DigitalTextView extends LinearLayout { 
  8.  
  9.     private static final String TAG = "DigitalTextView"; 
  10.     private String mResourcePrefix = "";// 资源前缀,时间或调频  
  11.  
  12.     // 两个构造器,自定义view中至少要有2个构造器  
  13.     public DigitalTextView(Context context) { 
  14.         super(context); 
  15.         init(); 
  16.     } 
  17.  
  18.     public DigitalTextView(Context context, AttributeSet attrs) { 
  19.         super(context, attrs); 
  20.         init(); 
  21.     } 
  22.  
  23.     // 初始化  
  24.     private void init() { 
  25.         this.setOrientation(LinearLayout.HORIZONTAL); 
  26.     } 
  27.  
  28.     /** 
  29.      * 获取对应的时间图片 
  30.      *  
  31.      * @param index 
  32.      *            时间值 
  33.      * @return 对应的时间图片id 
  34.      */ 
  35.     private int getTimeDrawable(int index) { 
  36.         int drawableId = -1; 
  37.         switch (index) { 
  38.         case 0: 
  39.             drawableId = R.drawable.time_0; 
  40.             break; 
  41.         case 1: 
  42.             drawableId = R.drawable.time_1; 
  43.             break; 
  44.         case 2: 
  45.             drawableId = R.drawable.time_2; 
  46.             break; 
  47.         case 3: 
  48.             drawableId = R.drawable.time_3; 
  49.             break; 
  50.         case 4: 
  51.             drawableId = R.drawable.time_4; 
  52.             break; 
  53.         case 5: 
  54.             drawableId = R.drawable.time_5; 
  55.             break; 
  56.         case 6: 
  57.             drawableId = R.drawable.time_6; 
  58.             break; 
  59.         case 7: 
  60.             drawableId = R.drawable.time_7; 
  61.             break; 
  62.         case 8: 
  63.             drawableId = R.drawable.time_8; 
  64.             break; 
  65.         case 9: 
  66.             drawableId = R.drawable.time_9; 
  67.             break; 
  68.         } 
  69.         return drawableId; 
  70.     } 
  71.  
  72.     /** 
  73.      * 获取调频图片 
  74.      *  
  75.      * @param index 
  76.      *            频率值 
  77.      * @return 对应频率值的图片id 
  78.      */ 
  79.     private int getFreqDrawable(int index) { 
  80.         int drawableId = -1; 
  81.         switch (index) { 
  82.         case 0: 
  83.             drawableId = R.drawable.freq_0; 
  84.             break; 
  85.         case 1: 
  86.             drawableId = R.drawable.freq_1; 
  87.             break; 
  88.         case 2: 
  89.             drawableId = R.drawable.freq_2; 
  90.             break; 
  91.         case 3: 
  92.             drawableId = R.drawable.freq_3; 
  93.             break; 
  94.         case 4: 
  95.             drawableId = R.drawable.freq_4; 
  96.             break; 
  97.         case 5: 
  98.             drawableId = R.drawable.freq_5; 
  99.             break; 
  100.         case 6: 
  101.             drawableId = R.drawable.freq_6; 
  102.             break; 
  103.         case 7: 
  104.             drawableId = R.drawable.freq_7; 
  105.             break; 
  106.         case 8: 
  107.             drawableId = R.drawable.freq_8; 
  108.             break; 
  109.         case 9: 
  110.             drawableId = R.drawable.freq_9; 
  111.             break; 
  112.         } 
  113.         return drawableId; 
  114.     } 
  115.  
  116.     /** 
  117.      * 根据传递进来的字符,返回对应的图片资源 
  118.      *  
  119.      * @param c 
  120.      *            传递进来的字符 
  121.      * @return 对应的图片id 
  122.      */ 
  123.     private int getResourceForChar(char c) { 
  124.         if (c == '.') { 
  125.             if (!mResourcePrefix.equals("time")) { 
  126.                 return R.drawable.freq_dot; 
  127.             } 
  128.         } else if (c == ':') { 
  129.             if (mResourcePrefix.equals("time")) { 
  130.                 return R.drawable.time_colon; 
  131.             } 
  132.         } else if (c >= '0' && c <= '9') { 
  133.             if (mResourcePrefix.equals("time")) { 
  134.                 return getTimeDrawable(c - '0'); 
  135.             } else { 
  136.                 return getFreqDrawable(c - '0'); 
  137.             } 
  138.         } else { 
  139.             return -1; 
  140.         } 
  141.         return -1; 
  142.     } 
  143.  
  144.     // 创建一个ImageView  
  145.     private ImageView createImageView() { 
  146.         ImageView imageView = new ImageView(getContext()); 
  147.         LayoutParams param = new LayoutParams( 
  148.                 LinearLayout.LayoutParams.WRAP_CONTENT, 
  149.                 LinearLayout.LayoutParams.WRAP_CONTENT); 
  150.         imageView.setLayoutParams(param); 
  151.         return imageView; 
  152.     } 
  153.  
  154.     /** 
  155.      * 设置资源前缀,是时间还是调频 
  156.      *  
  157.      * @param resourcePrefix 
  158.      */ 
  159.     public void setResourcePrefix(String resourcePrefix) { 
  160.         mResourcePrefix = resourcePrefix; 
  161.     } 
  162.  
  163.     /** 
  164.      * 设置数字值,类似TextView的setText() 
  165.      *  
  166.      * @param text 
  167.      *            传递进来的字符串 
  168.      */ 
  169.     public void setDigitalText(String text) { 
  170.         updateView(text); 
  171.     } 
  172.  
  173.     /** 
  174.      * 更新自定义TextView 
  175.      *  
  176.      * @param text 
  177.      *            传递进来的字符串 
  178.      */ 
  179.     private void updateView(String text) { 
  180.  
  181.         int startIndex = getChildCount() - text.length();// 起始位置,因为imageView的数量是根据字符串的长度创建的  
  182.         if (startIndex < 0)//第一次更新的时候肯定是小于0的  
  183.             startIndex = 0; 
  184.  
  185.         for (int i = 0; i < startIndex; i++) { 
  186.             getChildAt(i).setVisibility(View.GONE);//把之前的图片隐藏起来,个人感觉这样做得不怎么样  
  187.         } 
  188.  
  189.         //下面是根据字符串的长度,循环更换为对应的图片  
  190.         for (int i = 0; i < text.length(); i++) { 
  191.             int childId = i + startIndex; 
  192.             int resId = getResourceForChar(text.charAt(i));//将每个字符转换为数字  
  193.  
  194.             if (resId != -1) { 
  195.                 if (childId == getChildCount()) { 
  196.                     addView(createImageView());//添加到LinearLayout中  
  197.                 } 
  198.                 ImageView child = ((ImageView) getChildAt(childId)); 
  199.                 child.setVisibility(View.VISIBLE); 
  200.                 child.setImageResource(resId); 
  201.             } 
  202.         } 
  203.     } 
  204. } 
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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