国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義TextView實現文字傾斜效果

2019-12-12 04:29:17
字體:
來源:轉載
供稿:網友

前言

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


TextView文字傾斜

其實實現很簡單,下面我們來看實現步驟:

1、新建一個類 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、頁面布局,引用自定義控件

  <com.aikaifa.LeanTextView    android:id="@+id/lean"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:padding="10dp"    android:text="愛開發" />

這里我們用TextView記錄傾斜的角度,用SeekBar動態改變角度

  <com.aikaifa.LeanTextView    android:id="@+id/lean"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:padding="10dp"    android:text="愛開發" />  <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) {      }    });

這樣關于TextView 文字傾斜的自定義控件就算基本完成了,是不是很簡單。

項目結構圖:


TextView文字傾斜項目結構圖

總結

以上就是這篇文章的全部內容了,希望本文的內容對各位Android開發者們能有所幫助,如果有疑問大家可以留言交流。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 资源县| 颍上县| 乐山市| 建始县| 钟山县| 华坪县| 兴山县| 郴州市| 建阳市| 宿松县| 木里| 从化市| 罗城| 平和县| 洛浦县| 灵宝市| 平邑县| 博罗县| 普宁市| 罗甸县| 横峰县| 抚远县| 双江| 安福县| 永康市| 南澳县| 蓬安县| 凤山市| 枞阳县| 平阴县| 齐河县| 亳州市| 金溪县| 壤塘县| 宁蒗| 始兴县| 山丹县| 寻乌县| 轮台县| 绥芬河市| 肇州县|