欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義View用切圖顯示字符串

 更新時(shí)間:2021年01月28日 15:28:18   作者:JaJa非  
這篇文章主要為大家詳細(xì)介紹了Android自定義View用切圖顯示字符串,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

近期開發(fā)收音機(jī)有個(gè)需求,將頻率值以圖片的形式顯示出來(如結(jié)尾效果圖所示)。然而,一開始用TextView寫出來的效果太丑了,提交上去肯定不合格。于是乎我想到了寫一個(gè)自定義View,將頻率的數(shù)字切圖排布在View上,滿足效果圖的需求,在此記錄一下。

TextView表示的數(shù)字,Low得一批。

主要代碼及相關(guān)注釋

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);
  }
  /**
   * 獲取調(diào)頻圖片
   *
   * @param index 頻率值
   * @return 對應(yīng)頻率值的圖片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ù)傳遞進(jìn)來的字符,返回對應(yīng)的圖片資源
   *
   * @param c 傳遞進(jìn)來的字符
   * @return 對應(yīng)的圖片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)建一個(gè)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 傳遞進(jìn)來的字符串
   */
  public void setDigitalText(String text) {

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

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

    //下面是根據(jù)字符串的長度,循環(huán)更換為對應(yīng)的圖片
    for (int i = 0; i < text.length(); i++) {
      int childId = i + startIndex;
      int resId = getResourceForChar(text.charAt(i));//將每個(gè)字符轉(zhuǎn)換為數(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)實(shí)現(xiàn)了把頻率用drawable下的num_0~num9來顯示了,因此只需要在Activity更新頻率的方法里調(diào)用setDigitalText(mFreq)即可完美實(shí)現(xiàn)需求。

最后的效果圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android studio點(diǎn)擊跳轉(zhuǎn)WebView詳解

    Android studio點(diǎn)擊跳轉(zhuǎn)WebView詳解

    這篇文章主要為大家詳細(xì)介紹了Android studio點(diǎn)擊跳轉(zhuǎn)WebView的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Android數(shù)據(jù)持久化之I/O操作詳解

    Android數(shù)據(jù)持久化之I/O操作詳解

    這篇文章主要介紹了Android數(shù)據(jù)持久化之I/O操作,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android I/O操作的相關(guān)原理與具體的持久化實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-05-05
  • Android RecyclerView item選中放大被遮擋問題詳解

    Android RecyclerView item選中放大被遮擋問題詳解

    這篇文章主要介紹了Android RecyclerView item選中放大被遮擋問題詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • android嵌套滾動入門實(shí)踐

    android嵌套滾動入門實(shí)踐

    嵌套滾動是 Android OS 5.0之后,google 為我們提供的新特性,本篇文章主要介紹了android嵌套滾動入門實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • Android實(shí)現(xiàn)帶頭像的用戶注冊頁面

    Android實(shí)現(xiàn)帶頭像的用戶注冊頁面

    這篇文章主要介紹了Android實(shí)現(xiàn)帶頭像的用戶注冊頁面的相關(guān)資料,介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • EditText限制小數(shù)點(diǎn)前后位數(shù)的實(shí)例

    EditText限制小數(shù)點(diǎn)前后位數(shù)的實(shí)例

    下面小編就為大家?guī)硪黄狤ditText限制小數(shù)點(diǎn)前后位數(shù)的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android RecyclerView藝術(shù)般的控件使用完全解析

    Android RecyclerView藝術(shù)般的控件使用完全解析

    這篇文章主要介紹了Android RecyclerView藝術(shù)般的控件使用完全解析的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • Android視頻/音頻緩存框架AndroidVideoCache(Okhttp)詳解

    Android視頻/音頻緩存框架AndroidVideoCache(Okhttp)詳解

    這篇文章主要為大家詳細(xì)介紹了Android視頻、音頻緩存框架AndroidVideoCache,實(shí)現(xiàn)邊下邊播功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • android7.0實(shí)現(xiàn)分享圖片到朋友圈功能

    android7.0實(shí)現(xiàn)分享圖片到朋友圈功能

    這篇文章主要為大家詳細(xì)介紹了android7.0實(shí)現(xiàn)分享圖片到朋友圈功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Flutter實(shí)現(xiàn)彈窗攔截器的示例代碼

    Flutter實(shí)現(xiàn)彈窗攔截器的示例代碼

    彈窗的排隊(duì)執(zhí)行在App中是一個(gè)很常見的應(yīng)用場景,這篇文章為大家介紹了兩個(gè)Flutter實(shí)現(xiàn)彈窗攔截器的示例代碼,感興趣的小伙伴可以學(xué)習(xí)一下
    2023-09-09

最新評論