Android自定義TextView實(shí)現(xiàn)文字傾斜效果
前言
由于Android自帶的TextView控件沒有提供傾斜的(我暫時(shí)沒有找到),我們可以自定義控件來實(shí)現(xiàn),下面首先來看我們實(shí)現(xiàn)的效果圖。

TextView文字傾斜
其實(shí)實(shí)現(xiàn)很簡(jiǎn)單,下面我們來看實(shí)現(xiàn)步驟:
1、新建一個(gè)類 LeanTextView繼承TextView
public class LeanTextView extends TextView {
public int getmDegrees() {
return mDegrees;
}
public void setmDegrees(int mDegrees) {
this.mDegrees = mDegrees;
invalidate();
}
private int mDegrees;
public LeanTextView(Context context) {
super(context, null);
}
public LeanTextView(Context context, AttributeSet attrs) {
super(context, attrs, android.R.attr.textViewStyle);
this.setGravity(Gravity.CENTER);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LeanTextView);
mDegrees = a.getDimensionPixelSize(R.styleable.LeanTextView_degree, 0);
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
canvas.rotate(mDegrees, this.getWidth() / 2f, this.getHeight() / 2f);
super.onDraw(canvas);
canvas.restore();
}
}
2、在values文件中新建styleable.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="LeanTextView">
<attr name="degree" format="dimension" />
</declare-styleable>
</resources>
3、頁(yè)面布局,引用自定義控件
<com.aikaifa.LeanTextView
android:id="@+id/lean"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="愛開發(fā)" />
這里我們用TextView記錄傾斜的角度,用SeekBar動(dòng)態(tài)改變角度
<com.aikaifa.LeanTextView
android:id="@+id/lean"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="愛開發(fā)" />
<TextView
android:id="@+id/degrees"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center"/>
<SeekBar
android:id="@+id/sb_lean"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:max="100"
android:progress="30" />
java代碼
mText= (LeanTextView) findViewById (R.id.lean);
degrees= (TextView) findViewById (R.id.degrees);
SeekBar sbLean = (SeekBar) findViewById(R.id.sb_lean);
sbLean.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mText.setmDegrees(progress);
degrees.setText("傾斜度:"+progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
這樣關(guān)于TextView 文字傾斜的自定義控件就算基本完成了,是不是很簡(jiǎn)單。
項(xiàng)目結(jié)構(gòu)圖:

TextView文字傾斜項(xiàng)目結(jié)構(gòu)圖
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開發(fā)者們能有所幫助,如果有疑問大家可以留言交流。
- 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ù)字滾動(dòng)動(dòng)畫
- Android 自定義TextView實(shí)現(xiàn)文本內(nèi)容自動(dòng)調(diào)整字體大小
- Android自定義豎排TextView實(shí)現(xiàn)實(shí)例
- Android自定義textview實(shí)現(xiàn)豎直滾動(dòng)跑馬燈效果
- Android開發(fā)自定義TextView省略號(hào)樣式的方法
相關(guān)文章
Android獲取分享應(yīng)用列表詳解及實(shí)例
這篇文章主要介紹了Android獲取分享應(yīng)用列表詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
詳解Android使用Html.fromHtml需要注意的地方
本篇文章主要介紹了詳解Android使用Html.fromHtml需要注意的地方,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android 混合動(dòng)畫詳解及實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 混合動(dòng)畫詳解及實(shí)現(xiàn)代碼的相關(guān)資料,簡(jiǎn)單的一種動(dòng)畫(如旋轉(zhuǎn)、縮放、漸變、位移等)有時(shí)候并不能滿足我們項(xiàng)目的要求,這時(shí)候就需要運(yùn)用到混合動(dòng)畫,需要的朋友可以參考下2016-11-11
Retrofit2.0添加Header的方法總結(jié)(推薦)
這篇文章主要介紹了Retrofit2.0添加Header的方法總結(jié)(推薦),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
Android Canvas方法總結(jié)最全面詳解API(小結(jié))
本篇文章主要介紹了Android Canvas方法總結(jié)最全面詳解API(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Android鬧鈴服務(wù)AlarmManager用法深入分析
這篇文章主要介紹了Android鬧鈴服務(wù)AlarmManager用法,結(jié)合實(shí)例形式深入分析了鬧鈴服務(wù)AlarmManager的功能、原理、定義與使用方法,需要的朋友可以參考下2016-08-08
Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵
這篇文章主要介紹了Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
XListView實(shí)現(xiàn)多條目網(wǎng)絡(luò)數(shù)據(jù)刷新加載 網(wǎng)絡(luò)加載圖片
這篇文章主要為大家詳細(xì)介紹了XListView實(shí)現(xiàn)多條目網(wǎng)絡(luò)數(shù)據(jù)刷新加載,網(wǎng)絡(luò)加載圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android下拉刷新PtrFrameLayout的使用實(shí)例代碼
本篇文章主要介紹了Android下拉刷新PtrFrameLayout的使用實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

