Android自訂TextView實現文字傾斜效果_Android

來源:互聯網
上載者:User

前言

由於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開發人員們能有所協助,如果有疑問大家可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.