Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自定義 TextView 讓文字「歪」起來:Android 文字傾斜實作

編輯:關於 Android 程式設計

系統內建的 TextView 並沒有直接提供文字傾斜的屬性(至少我在原生 API 裡沒找到現成的),但只要繼承 TextView 並在繪製時動一點手腳,就能自己做出效果。

步驟一:新增 LeanTextView 類

關鍵在 onDraw:先 save 畫布狀態,以控件中心為軸心旋轉指定角度,畫完再 restore 還原,避免影響到後續繪製。

public class LeanTextView extends TextView {
    private int mDegrees;

    public int getmDegrees() { return mDegrees; }

    public void setmDegrees(int mDegrees) {
        this.mDegrees = mDegrees;
        invalidate();
    }

    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 onDraw(Canvas canvas) {
        canvas.save();
        canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
        canvas.rotate(mDegrees, this.getWidth() / 2f, this.getHeight() / 2f);
        super.onDraw(canvas);
        canvas.restore();
    }
}

步驟二:宣告自定義屬性

在 values 目錄下新增 styleable.xml,讓 XML 裡可以直接設定角度:

<resources>
    <declare-styleable name="LeanTextView">
        <attr name="degree" format="dimension" />
    </declare-styleable>
</resources>

步驟三:在版面配置中引用

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

步驟四:用 SeekBar 即時調角度

再加一個 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_height="wrap_content"
    android:layout_marginTop="20dp"
    android:max="100"
    android:progress="30" />
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) { }
});

專案結構

到這裡,一個可調角度的傾斜文字控件就完成了。這種「繼承原生 View + 改寫 onDraw」的思路幾乎可以套用到任何自定義繪圖需求上,值得多練幾次。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved