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

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

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

前言

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


TextView文字傾斜

其實(shí)實(shí)現(xiàn)很簡單,下面我們來看實(shí)現(xiàn)步驟:

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="愛開發(fā)" />

這里我們用TextView記錄傾斜的角度,用SeekBar動態(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 文字傾斜的自定義控件就算基本完成了,是不是很簡單。

項(xiàng)目結(jié)構(gòu)圖:


TextView文字傾斜項(xiàng)目結(jié)構(gòu)圖

總結(jié)

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

相關(guān)文章

最新評論

沭阳县| 绥芬河市| 木里| 洪洞县| 冷水江市| 乳山市| 武邑县| 抚州市| 云和县| 封丘县| 湘潭市| 辉南县| 集安市| 武邑县| 尖扎县| 腾冲县| 惠州市| 本溪市| 仁寿县| 张家川| 兴文县| 兴隆县| 乃东县| 新邵县| 无为县| 伊宁县| 安泽县| 枣阳市| 乐都县| 丰城市| 拉萨市| 隆尧县| 博爱县| 鹤峰县| 南投市| 馆陶县| 红桥区| 黄骅市| 临西县| 油尖旺区| 周宁县|