最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義TextView實(shí)現(xiàn)文字傾斜效果

 更新時(shí)間:2016年11月29日 10:35:08   作者:愛開發(fā)  
有時(shí)候Android自帶的控件無法滿足我們的某些要求,這時(shí)就需要我們自定義控件來實(shí)現(xiàn)這些功能。比如在實(shí)際開發(fā)應(yīng)用中,我們有時(shí)需要將TextView的文字傾斜一定的角度,就需要自定義TextView。下面這篇文章就給大家介紹了利用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ā)者們能有所幫助,如果有疑問大家可以留言交流。

相關(guān)文章

最新評(píng)論

台北县| 阳朔县| 昌黎县| 临沭县| 四会市| 桃江县| 宣汉县| 突泉县| 易门县| 潢川县| 华亭县| 邹城市| 和政县| 平遥县| 长寿区| 梨树县| 蕲春县| 长汀县| 土默特左旗| 南平市| 东安县| 长武县| 霍林郭勒市| 富蕴县| 泾川县| 开化县| 闽侯县| 汝南县| 东乡县| 巴中市| 武汉市| 康平县| 通城县| 明水县| 彩票| 大冶市| 临泉县| 长治县| 淮滨县| 桦川县| 宜兰县|