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

漢字和英文字符都可以豎直排列。結(jié)合聯(lián)系人界面,可以將左側(cè)的索引改成聯(lián)系人的姓氏。
上代碼:
測(cè)試用的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;
}
}
代碼也很簡(jiǎn)單,復(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ì)整體靠上。這里要參考字符的繪制原理,明白了后就很簡(jiǎn)單了。這里我就不在過多敘述。
最后是測(cè)試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ì)本站的支持!
- android開發(fā)教程之textview內(nèi)容超出屏幕寬度顯示省略號(hào)
- Android設(shè)置TextView顯示指定個(gè)數(shù)字符,超過部分顯示...(省略號(hào))的方法
- Android中Textview和圖片同行顯示(文字超出用省略號(hào),圖片自動(dòng)靠右邊)
- Android設(shè)置當(dāng)TextView中的文字超過TextView的容量時(shí)用省略號(hào)代替
- 解析在Android中為TextView增加自定義HTML標(biāo)簽的實(shí)現(xiàn)方法
- Android TextView顯示Html類解析的網(wǎng)頁(yè)和圖片及自定義標(biāo)簽用法示例
- Android自定義View之繼承TextView繪制背景
- Android自定義TextView實(shí)現(xiàn)文字傾斜效果
- Android TextView自定義數(shù)字滾動(dòng)動(dòng)畫
- Android 自定義TextView實(shí)現(xiàn)文本內(nèi)容自動(dòng)調(diào)整字體大小
- Android自定義textview實(shí)現(xiàn)豎直滾動(dòng)跑馬燈效果
- Android開發(fā)自定義TextView省略號(hào)樣式的方法
相關(guān)文章
如何獲取Android設(shè)備掛載的所有存儲(chǔ)器
這篇文章主要為大家詳細(xì)介紹了如何獲取Android設(shè)備掛載的所有存儲(chǔ)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Android編程實(shí)現(xiàn)類似天氣預(yù)報(bào)圖文字幕垂直滾動(dòng)效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)類似天氣預(yù)報(bào)圖文字幕垂直滾動(dòng)效果的方法,涉及Android基于布局及事件響應(yīng)實(shí)現(xiàn)圖文滾動(dòng)效果的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Android Studio4.0導(dǎo)入OpenCv4.3.0的方法步驟
這篇文章主要介紹了Android Studio4.0導(dǎo)入OpenCv4.3.0的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動(dòng)效果
這篇文章主要介紹了Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動(dòng)效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android中使用開源框架eventbus3.0實(shí)現(xiàn)fragment之間的通信交互
本文主要介紹了Android中使用開源框架eventbus3.0實(shí)現(xiàn)fragment之間的通信交互的方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02
用MOB實(shí)例開發(fā)實(shí)現(xiàn)短信驗(yàn)證功能
本篇文章通學(xué)習(xí)通過MOB平臺(tái)開發(fā)APP實(shí)現(xiàn)簡(jiǎn)單的短信驗(yàn)證功能,對(duì)此有需求的朋友跟著好好學(xué)習(xí)下吧。2018-01-01
Android實(shí)現(xiàn)懸浮窗的簡(jiǎn)單方法實(shí)例
相信大家應(yīng)該也都發(fā)現(xiàn)了,現(xiàn)在很多應(yīng)用都使用到懸浮窗,例如微信在視頻的時(shí)候,點(diǎn)擊Home鍵,視頻小窗口仍然會(huì)在屏幕上顯示,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)懸浮窗的簡(jiǎn)單方法,需要的朋友可以參考下2021-09-09
Android系統(tǒng)自帶樣式 (android:theme)
Android系統(tǒng)中自帶樣式分享,需要的朋友可以參考下2013-01-01

