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

漢字和英文字符都可以豎直排列。結合聯(lián)系人界面,可以將左側的索引改成聯(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;
}
}
這里主要說下如何通過點擊或者滑動左側的自定義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軸坐標,然后通過一個比例式將點擊位置換算成豎排索引字符集中的對應字符位置,通過這個位置就可以判斷出點擊的是哪一個索引字符了。這里要注意比例式中的運算要轉成浮點型計算,否則無法得到正確的索引值,樓主當時就在此深深的坑過。
下面是重點自定義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;
}
}
代碼也很簡單,復寫了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ù)比較復雜,重點說下前半部分
(i + 0.5) * childHeight
表示每個字符區(qū)域高度的一辦所在的y軸坐標,后半部分
(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>
當字符集變化后依然很好的適應,效果圖:

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- android開發(fā)教程之textview內(nèi)容超出屏幕寬度顯示省略號
- Android設置TextView顯示指定個數(shù)字符,超過部分顯示...(省略號)的方法
- Android中Textview和圖片同行顯示(文字超出用省略號,圖片自動靠右邊)
- Android設置當TextView中的文字超過TextView的容量時用省略號代替
- 解析在Android中為TextView增加自定義HTML標簽的實現(xiàn)方法
- Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標簽用法示例
- Android自定義View之繼承TextView繪制背景
- Android自定義TextView實現(xiàn)文字傾斜效果
- Android TextView自定義數(shù)字滾動動畫
- Android 自定義TextView實現(xiàn)文本內(nèi)容自動調(diào)整字體大小
- Android自定義textview實現(xiàn)豎直滾動跑馬燈效果
- Android開發(fā)自定義TextView省略號樣式的方法
相關文章
Android編程實現(xiàn)類似天氣預報圖文字幕垂直滾動效果的方法
這篇文章主要介紹了Android編程實現(xiàn)類似天氣預報圖文字幕垂直滾動效果的方法,涉及Android基于布局及事件響應實現(xiàn)圖文滾動效果的相關操作技巧,需要的朋友可以參考下2017-08-08
Android Studio4.0導入OpenCv4.3.0的方法步驟
這篇文章主要介紹了Android Studio4.0導入OpenCv4.3.0的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10
Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能
這篇文章主要為大家詳細介紹了Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Android中使用開源框架eventbus3.0實現(xiàn)fragment之間的通信交互
本文主要介紹了Android中使用開源框架eventbus3.0實現(xiàn)fragment之間的通信交互的方法,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02
Android系統(tǒng)自帶樣式 (android:theme)
Android系統(tǒng)中自帶樣式分享,需要的朋友可以參考下2013-01-01

