源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android自定义TextView实现文字倾斜效果

  • 时间:2020-06-11 08:19 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android自定义TextView实现文字倾斜效果
[b]前言[/b] 由于Android自带的TextView控件没有提供倾斜的(我暂时没有找到),我们可以自定义控件来实现,下面首先来看我们实现的效果图。 [img]http://files.jb51.net/file_images/article/201611/20161129103334199.gif?20161029103349[/img] TextView文字倾斜 [b]其实实现很简单,下面我们来看实现步骤:[/b] [b]1、新建一个类 LeanTextView继承TextView [/b]
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();
  }
}
[b]2、在values文件中新建styleable.xml[/b]
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="LeanTextView">
    <attr name="degree" format="dimension" />
  </declare-styleable>
</resources>
[b]3、页面布局,引用自定义控件[/b]
  <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" />
[b]java代码[/b]
    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 文字倾斜的自定义控件就算基本完成了,是不是很简单。 [b]项目结构图:[/b] [img]http://files.jb51.net/file_images/article/201611/20161129103515841.jpg?20161029103530[/img] TextView文字倾斜项目结构图 [b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们能有所帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部