首页 > 代码库 > Android 文字倾斜
Android 文字倾斜
有时候Android自带的控件无法满足我们的某些要求,这时就需要我们自定义控件来实现这些功能。比如需要一个TextView里的字倾斜一定的角度,就需要自定义TextView。
package com.leigo.ratatetextview; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.Gravity; import android.widget.TextView; /** * Created by Administrator on 2014/9/9. */ public class RotateTextView extends TextView { private static final int DEFAULT_DEGREES = 0; private int mDegrees; public RotateTextView(Context context) { super(context, null); } public RotateTextView(Context context, AttributeSet attrs) { super(context, attrs, android.R.attr.textViewStyle); this.setGravity(Gravity.CENTER); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RotateTextView); mDegrees = a.getDimensionPixelSize(R.styleable.RotateTextView_degree, DEFAULT_DEGREES); 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(); } public void setDegrees(int degrees) { mDegrees = degrees; } }
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="RotateTextView"> <attr name="degree" format="dimension" /> </declare-styleable> </resources>
用法:
1.xml
<com.leigo.ratatetextview.RotateTextView xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/text" android:text="@string/hello_world" app:degree="10dp" android:padding="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /></span>
2.java
RotateTextView mText = (RotateTextView) findViewById (R.id.text); mText.setDegrees(10);</span>
项目地址:
https://coding.net/u/leigo/p/RotateTextView/git
Android 文字倾斜
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。