第三,是中间那个显示时间和调频自定义的View,DigitalTextView.java:
- /**
- * 自定义的一个显示调频或时间的view
- *
- * @author way
- *
- */
- public class DigitalTextView extends LinearLayout {
- private static final String TAG = "DigitalTextView";
- private String mResourcePrefix = "";// 资源前缀,时间或调频
- // 两个构造器,自定义view中至少要有2个构造器
- public DigitalTextView(Context context) {
- super(context);
- init();
- }
- public DigitalTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
- // 初始化
- private void init() {
- this.setOrientation(LinearLayout.HORIZONTAL);
- }
- /**
- * 获取对应的时间图片
- *
- * @param index
- * 时间值
- * @return 对应的时间图片id
- */
- private int getTimeDrawable(int index) {
- int drawableId = -1;
- switch (index) {
- case 0:
- drawableId = R.drawable.time_0;
- break;
- case 1:
- drawableId = R.drawable.time_1;
- break;
- case 2:
- drawableId = R.drawable.time_2;
- break;
- case 3:
- drawableId = R.drawable.time_3;
- break;
- case 4:
- drawableId = R.drawable.time_4;
- break;
- case 5:
- drawableId = R.drawable.time_5;
- break;
- case 6:
- drawableId = R.drawable.time_6;
- break;
- case 7:
- drawableId = R.drawable.time_7;
- break;
- case 8:
- drawableId = R.drawable.time_8;
- break;
- case 9:
- drawableId = R.drawable.time_9;
- break;
- }
- return drawableId;
- }
- /**
- * 获取调频图片
- *
- * @param index
- * 频率值
- * @return 对应频率值的图片id
- */
- private int getFreqDrawable(int index) {
- int drawableId = -1;
- switch (index) {
- case 0:
- drawableId = R.drawable.freq_0;
- break;
- case 1:
- drawableId = R.drawable.freq_1;
- break;
- case 2:
- drawableId = R.drawable.freq_2;
- break;
- case 3:
- drawableId = R.drawable.freq_3;
- break;
- case 4:
- drawableId = R.drawable.freq_4;
- break;
- case 5:
- drawableId = R.drawable.freq_5;
- break;
- case 6:
- drawableId = R.drawable.freq_6;
- break;
- case 7:
- drawableId = R.drawable.freq_7;
- break;
- case 8:
- drawableId = R.drawable.freq_8;
- break;
- case 9:
- drawableId = R.drawable.freq_9;
- break;
- }
- return drawableId;
- }
- /**
- * 根据传递进来的字符,返回对应的图片资源
- *
- * @param c
- * 传递进来的字符
- * @return 对应的图片id
- */
- private int getResourceForChar(char c) {
- if (c == '.') {
- if (!mResourcePrefix.equals("time")) {
- return R.drawable.freq_dot;
- }
- } else if (c == ':') {
- if (mResourcePrefix.equals("time")) {
- return R.drawable.time_colon;
- }
- } else if (c >= '0' && c <= '9') {
- if (mResourcePrefix.equals("time")) {
- return getTimeDrawable(c - '0');
- } else {
- return getFreqDrawable(c - '0');
- }
- } else {
- return -1;
- }
- return -1;
- }
- // 创建一个ImageView
- private ImageView createImageView() {
- ImageView imageView = new ImageView(getContext());
- LayoutParams param = new LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- imageView.setLayoutParams(param);
- return imageView;
- }
- /**
- * 设置资源前缀,是时间还是调频
- *
- * @param resourcePrefix
- */
- public void setResourcePrefix(String resourcePrefix) {
- mResourcePrefix = resourcePrefix;
- }
- /**
- * 设置数字值,类似TextView的setText()
- *
- * @param text
- * 传递进来的字符串
- */
- public void setDigitalText(String text) {
- updateView(text);
- }
- /**
- * 更新自定义TextView
- *
- * @param text
- * 传递进来的字符串
- */
- private void updateView(String text) {
- int startIndex = getChildCount() - text.length();// 起始位置,因为imageView的数量是根据字符串的长度创建的
- if (startIndex < 0)//第一次更新的时候肯定是小于0的
- startIndex = 0;
- for (int i = 0; i < startIndex; i++) {
- getChildAt(i).setVisibility(View.GONE);//把之前的图片隐藏起来,个人感觉这样做得不怎么样
- }
- //下面是根据字符串的长度,循环更换为对应的图片
- for (int i = 0; i < text.length(); i++) {
- int childId = i + startIndex;
- int resId = getResourceForChar(text.charAt(i));//将每个字符转换为数字
- if (resId != -1) {
- if (childId == getChildCount()) {
- addView(createImageView());//添加到LinearLayout中
- }
- ImageView child = ((ImageView) getChildAt(childId));
- child.setVisibility(View.VISIBLE);
- child.setImageResource(resId);
- }
- }
- }
- }
