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

Android自定義豎排TextView實(shí)現(xiàn)實(shí)例

 更新時(shí)間:2017年05月05日 16:28:28   投稿:lqh  
這篇文章主要介紹了Android自定義豎排TextView實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下

Android自定義豎排TextView實(shí)現(xiàn)實(shí)例

前言:

之前做聯(lián)系人模塊的時(shí)候遇到一個(gè)左側(cè)索引控件,里面的字符都是豎直方向上排列的。當(dāng)時(shí)這個(gè)控件是用一個(gè)圖片代替的?,F(xiàn)在想來如果索引的字符變更了,那么就得重新更換圖片了,感覺很麻煩。今天通過一個(gè)自定義TextView實(shí)現(xiàn)類似的功能。先上效果圖:


漢字和英文字符都可以豎直排列。結(jié)合聯(lián)系人界面,可以將左側(cè)的索引改成聯(lián)系人的姓氏。

上代碼:

測試用的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; 
  } 
} 

這里主要說下如何通過點(diǎn)擊或者滑動(dòng)左側(cè)的自定義TextView,將索引值取出來。主要的實(shí)現(xiàn)點(diǎ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 是整個(gè)控件的高度,y按下控件后的y軸坐標(biāo),然后通過一個(gè)比例式將點(diǎn)擊位置換算成豎排索引字符集中的對(duì)應(yīng)字符位置,通過這個(gè)位置就可以判斷出點(diǎn)擊的是哪一個(gè)索引字符了。這里要注意比例式中的運(yùn)算要轉(zhuǎn)成浮點(diǎn)型計(jì)算,否則無法得到正確的索引值,樓主當(dāng)時(shí)就在此深深的坑過。

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

public class VerticalTextView extends TextView { 
 
  /** 
   * 繪制整個(gè)VerticalTextView區(qū)域大小的畫筆 
   */ 
  private Paint mPaint; 
  /** 
   * 繪制每個(gè)豎排字符間的間隔橫線的畫筆 
   */ 
  private Paint linePaint; 
  /** 
   * 繪制單個(gè)字符的畫筆 
   */ 
  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ù)。將要顯示的字符串拆分成一個(gè)個(gè)字符放在一個(gè)數(shù)組中。通過一個(gè)循環(huán)遍歷這個(gè)數(shù)組,挨個(gè)將他們繪制出來。精華只有一句:

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

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

(i + 0.5) * childHeight 

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

(fm.ascent + fm.descent) / 2 

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

最后是測試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> 

當(dāng)字符集變化后依然很好的適應(yīng),效果圖:

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論