Android自定義TextView實(shí)現(xiàn)文字傾斜效果
前言
由于Android自帶的TextView控件沒(méi)有提供傾斜的(我暫時(shí)沒(méi)有找到),我們可以自定義控件來(lái)實(shí)現(xiàn),下面首先來(lái)看我們實(shí)現(xiàn)的效果圖。
TextView文字傾斜
其實(shí)實(shí)現(xiàn)很簡(jiǎn)單,下面我們來(lái)看實(shí)現(xiàn)步驟:
1、新建一個(gè)類(lèi) 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="愛(ài)開(kāi)發(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="愛(ài)開(kāi)發(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開(kāi)發(fā)者們能有所幫助,如果有疑問(wèn)大家可以留言交流。
- android開(kāi)發(fā)教程之textview內(nèi)容超出屏幕寬度顯示省略號(hào)
- Android設(shè)置TextView顯示指定個(gè)數(shù)字符,超過(guò)部分顯示...(省略號(hào))的方法
- Android中Textview和圖片同行顯示(文字超出用省略號(hào),圖片自動(dòng)靠右邊)
- Android設(shè)置當(dāng)TextView中的文字超過(guò)TextView的容量時(shí)用省略號(hào)代替
- 解析在Android中為T(mén)extView增加自定義HTML標(biāo)簽的實(shí)現(xiàn)方法
- Android TextView顯示Html類(lèi)解析的網(wǎng)頁(yè)和圖片及自定義標(biāo)簽用法示例
- Android自定義View之繼承TextView繪制背景
- Android TextView自定義數(shù)字滾動(dòng)動(dòng)畫(huà)
- 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開(kāi)發(fā)自定義TextView省略號(hào)樣式的方法
相關(guān)文章
ViewPager和SlidingPaneLayout的滑動(dòng)事件沖突解決方法
下面小編就為大家分享一篇ViewPager和SlidingPaneLayout的滑動(dòng)事件沖突解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android實(shí)現(xiàn)讀取NFC卡卡號(hào)示例
本篇文章主要介紹了Android實(shí)現(xiàn)讀取NFC卡卡號(hào)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01Afianl框架里面的FinalBitmap加載網(wǎng)絡(luò)圖片
這篇文章主要介紹了Afianl框架里面的FinalBitmap加載網(wǎng)絡(luò)圖片的相關(guān)資料,需要的朋友可以參考下2015-07-07android檢測(cè)網(wǎng)絡(luò)連接狀態(tài)示例講解
網(wǎng)絡(luò)的時(shí)候,并不是每次都能連接到網(wǎng)絡(luò),因此在程序啟動(dòng)中需要對(duì)網(wǎng)絡(luò)的狀態(tài)進(jìn)行判斷,如果沒(méi)有網(wǎng)絡(luò)則提醒用戶(hù)進(jìn)行設(shè)置2014-02-02Android Studio 1.2版安裝設(shè)置圖文教程
這篇文章主要介紹了Android Studio 1.2版安裝設(shè)置圖文教程,本文詳細(xì)講解了下載、安裝Android Studio 1.2教程,以及常用設(shè)置詳細(xì)圖文教程,需要的朋友可以參考下2015-05-05詳解Android應(yīng)用中preference首選項(xiàng)的編寫(xiě)方法
這篇文章主要介紹了Android應(yīng)用中preference首選項(xiàng)的編寫(xiě)方法,或許Apple將其翻譯為'偏好設(shè)置'更直觀(guān)些,即用戶(hù)對(duì)應(yīng)用的一些個(gè)性化調(diào)整菜單,需要的朋友可以參考下2016-04-04