溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在Android中自定義View顯示字符串

發(fā)布時間:2021-01-29 15:02:28 來源:億速云 閱讀:263 作者:Leah 欄目:開發(fā)技術

這期內容當中小編將會給大家?guī)碛嘘P如何在Android中自定義View顯示字符串,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

public class DigitalTextView extends LinearLayout {

  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 getFreqDrawable(int index) {
    int drawableId = -1;
    switch (index) {
      case 0:
        drawableId = R.drawable.num_0;
        break;
      case 1:
        drawableId = R.drawable.num_1;
        break;
      case 2:
        drawableId = R.drawable.num_2;
        break;
      case 3:
        drawableId = R.drawable.num_3;
        break;
      case 4:
        drawableId = R.drawable.num_4;
        break;
      case 5:
        drawableId = R.drawable.num_5;
        break;
      case 6:
        drawableId = R.drawable.num_6;
        break;
      case 7:
        drawableId = R.drawable.num_7;
        break;
      case 8:
        drawableId = R.drawable.num_8;
        break;
      case 9:
        drawableId = R.drawable.num_9;
        break;
    }
    return drawableId;
  }

  /**
   * 根據(jù)傳遞進來的字符,返回對應的圖片資源
   *
   * @param c 傳遞進來的字符
   * @return 對應的圖片id
   */
  private int getResourceForChar(char c) {
    if (c == '.') {
      return R.drawable.num_dot;
    } else if (c >= '0' && c <= '9') {
      return getFreqDrawable(c - '0');
    } else {
      return -1;
    }
  }

  // 創(chuàng)建一個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;
  }


  /**
   * 更新自定義TextView
   * @param text 傳遞進來的字符串
   */
  public void setDigitalText(String text) {

    int startIndex = getChildCount() - text.length();// 起始位置,因為imageView的數(shù)量是根據(jù)字符串的長度創(chuàng)建的
    if (startIndex < 0)//第一次更新的時候肯定是小于0的
      startIndex = 0;

    for (int i = 0; i < startIndex; i++) {
      getChildAt(i).setVisibility(View.GONE);
    }

    //下面是根據(jù)字符串的長度,循環(huán)更換為對應的圖片
    for (int i = 0; i < text.length(); i++) {
      int childId = i + startIndex;
      int resId = getResourceForChar(text.charAt(i));//將每個字符轉換為數(shù)字

      if (resId != -1) {
        if (childId == getChildCount()) {
          addView(createImageView());//添加到LinearLayout中
        }
        ImageView child = ((ImageView) getChildAt(childId));
        child.setVisibility(View.VISIBLE);
        child.setImageResource(resId);
      }
    }
  }
}

DigitalTextView 已經(jīng)實現(xiàn)了把頻率用drawable下的num_0~num9來顯示了,因此只需要在Activity更新頻率的方法里調用setDigitalText(mFreq)即可完美實現(xiàn)需求。

上述就是小編為大家分享的如何在Android中自定義View顯示字符串了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI