中文字幕第五页-中文字幕第页-中文字幕韩国-中文字幕最新-国产尤物二区三区在线观看-国产尤物福利视频一区二区

如何在Android中利用TextView實現(xiàn)自定義豎排

這篇文章給大家介紹如何在Android中利用TextView實現(xiàn)自定義豎排,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創(chuàng)新互聯(lián)主要從事做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)寶清,十余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108

測試用的Activity。

public class MainActivity extends Activity implements OnTouchListener { 
 
  private VerticalTextView mVerticalTextView; 
 
  private TextView mTextView; 
 
  private int mTextCount; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main); 
 
    mVerticalTextView = (VerticalTextView) findViewById(R.id.vertical_tv); 
    mTextView = (TextView) findViewById(R.id.content_tx); 
    mTextCount = mVerticalTextView.getText().length(); 
    mVerticalTextView.setOnTouchListener(this); 
    mTextView.setBackgroundColor(Color.LTGRAY); 
  } 
 
  @Override 
  public boolean onTouch(View v, MotionEvent event) { 
    float verticalTextViewHeight = mVerticalTextView.getHeight(); 
    float y = event.getY(); 
    int sectionPosition = (int) Math.ceil((y / verticalTextViewHeight) 
        / (1f / mTextCount)) - 1; 
    if (sectionPosition < 0) { 
      sectionPosition = 0; 
    } else if (sectionPosition >= mTextCount) { 
      sectionPosition = mTextCount - 1; 
    } 
    String sectionLetter = String.valueOf(mVerticalTextView.getText() 
        .charAt(sectionPosition)); 
    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
      mTextView.setVisibility(View.VISIBLE); 
      mTextView.setText(sectionLetter); 
      break; 
    case MotionEvent.ACTION_MOVE: 
      mTextView.setText(sectionLetter); 
      mTextView.setVisibility(View.VISIBLE); 
      break; 
 
    case MotionEvent.ACTION_UP: 
      mTextView.setVisibility(View.INVISIBLE); 
    default: 
      break; 
 
    } 
 
    return true; 
  } 
} 

這里主要說下如何通過點擊或者滑動左側(cè)的自定義TextView,將索引值取出來。主要的實現(xiàn)點在代碼:

float verticalTextViewHeight = mVerticalTextView.getHeight(); 
float y = event.getY(); 
int sectionPosition = (int) Math.ceil((y / verticalTextViewHeight) 
    / (1f / mTextCount)) - 1; 
if (sectionPosition < 0) { 
  sectionPosition = 0; 
} else if (sectionPosition >= mTextCount) { 
  sectionPosition = mTextCount - 1; 
} 
String sectionLetter = String.valueOf(mVerticalTextView.getText() 
    .charAt(sectionPosition)); 

這里verticalTextViewHeight 是整個控件的高度,y按下控件后的y軸坐標(biāo),然后通過一個比例式將點擊位置換算成豎排索引字符集中的對應(yīng)字符位置,通過這個位置就可以判斷出點擊的是哪一個索引字符了。這里要注意比例式中的運算要轉(zhuǎn)成浮點型計算,否則無法得到正確的索引值,樓主當(dāng)時就在此深深的坑過。

下面是重點自定義TextView的實現(xiàn)代碼:

public class VerticalTextView extends TextView { 
 
  /** 
   * 繪制整個VerticalTextView區(qū)域大小的畫筆 
   */ 
  private Paint mPaint; 
  /** 
   * 繪制每個豎排字符間的間隔橫線的畫筆 
   */ 
  private Paint linePaint; 
  /** 
   * 繪制單個字符的畫筆 
   */ 
  private Paint charPaint; 
 
  private char[] indexs; 
 
  private int textCount; 
 
  private String textString; 
 
  public VerticalTextView(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
  } 
 
  public VerticalTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
 
    mPaint = new Paint(); 
 
    linePaint = new Paint(); 
 
    charPaint = new Paint(); 
 
    textString = getText().toString(); 
    indexs = getKeyChar(textString); 
    textCount = textString.toCharArray().length; 
  } 
 
  @Override 
  protected void onDraw(Canvas canvas) { 
    float childHeight = getHeight() / textCount; 
    if (TextUtils.isEmpty(textString)) 
      return; 
    canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint); 
    canvas.drawColor(Color.GRAY); 
 
    linePaint.setColor(Color.BLACK); 
 
    charPaint.setTextSize((float) (getWidth() * 0.75)); 
    charPaint.setTextAlign(Paint.Align.CENTER); 
 
    FontMetrics fm = charPaint.getFontMetrics(); 
    for (int i = 0; i < textCount; i++) { 
      canvas.drawLine(0, i * childHeight, getWidth(), i * childHeight, 
          linePaint); 
      canvas.drawText( 
          String.valueOf(indexs[i]), 
          getWidth() / 2, 
          (float) (((i + 0.5) * childHeight) - (fm.ascent + fm.descent) / 2), 
          charPaint); 
    } 
  } 
 
  protected char[] getKeyChar(String str) { 
    char[] keys = new char[str.length()]; 
    for (int i = 0; i < keys.length; i++) { 
      keys[i] = str.charAt(i); 
    } 
    return keys; 
  } 
} 

代碼也很簡單,復(fù)寫了onDraw函數(shù)。將要顯示的字符串拆分成一個個字符放在一個數(shù)組中。通過一個循環(huán)遍歷這個數(shù)組,挨個將他們繪制出來。精華只有一句:

canvas.drawText(String.valueOf(indexs[i]),getWidth() / 2,(float) (((i + 0.5) * childHeight) -  
 
(fm.ascent + fm.descent) / 2 
 
),charPaint); 

第一個參數(shù)是要繪制的字符,第二個參數(shù)繪制字符的x軸起始點,第三個參數(shù)比較復(fù)雜,重點說下前半部分

(i + 0.5) * childHeight 

表示每個字符區(qū)域高度的一辦所在的y軸坐標(biāo),后半部分

(fm.ascent + fm.descent) / 2 

這個是個矯正值,如果不跟上它,繪制出來的字符會整體靠上。這里要參考字符的繪制原理,明白了后就很簡單了。這里我就不在過多敘述。

最后是測試Activity的布局文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" > 
<com.example.demoindextextview.widget.VerticalTextView 
    android:id="@+id/vertical_tv" 
    android:layout_width="20dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="right" 
    android:text="ABCDEFGsdfsf你好嗎sdfsdklmnopqrstuvwxyz" /> 
 
<TextView  
  android:id="@+id/content_tx" 
  android:layout_gravity="center" 
  android:gravity="center" 
  android:layout_height="50dp" 
  android:layout_width="50dp" 
  android:textSize="30sp" 
  android:visibility="invisible"/> 
   
</FrameLayout> 

關(guān)于如何在Android中利用TextView實現(xiàn)自定義豎排就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

標(biāo)題名稱:如何在Android中利用TextView實現(xiàn)自定義豎排
鏈接URL:http://www.2m8n56k.cn/article26/pooccg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)外貿(mào)網(wǎng)站建設(shè)靜態(tài)網(wǎng)站外貿(mào)建站微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)
主站蜘蛛池模板: 美女黄影院 | 太平公主三级dvd | 一级特级欧美a毛片免费 | 理论片免费午夜 | 欧美手机看片 | 欧美中文字幕在线看 | 成人五级毛片免费播放 | 久久久久久久久久久9精品视频 | 亚洲性天堂| 久久午夜视频 | 欧美成人三级伦在线观看 | 成人精品在线观看 | 免费看黄色的网址 | 俄罗斯极品美女毛片免费播放 | 国产毛片久久久久久国产毛片 | 日本不卡一区视频 | 国产超薄肉色丝袜足j | 国产一级特黄aa级特黄裸毛片 | 九九久久久久午夜精选 | 94欧美setu| 日本在线观看免费视频网址 | 免费一级毛片在线播放 | 黄色三级视频在线 | 男女扒开双腿猛进入免费网站 | 欧美一区二区三区不卡片 | 99精品视频在线观看 | 日本红怡院亚洲红怡院最新 | 在线小毛片 | 久久免视频| 97在线免费观看视频 | 亚洲综合色在线观看 | 91挑色| 久久九九色 | 视频在线观看一区 | 91在线免费观看网站 | 国产欧美亚洲精品a | 普通话对白国产情侣自啪 | 欧美人成在线观看ccc36 | 91视频最新网站 | 日本高清不卡在线观看 | 视频一区免费 |