Android TextView文字倾斜

需求:TextView的文字倾斜一定的角度

自定义TextView

public class RotateTextView extends ThemedTextView {
    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.getInteger(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="integer" />  
    </declare-styleable>  
</resources>

使用RotateTextView

xml

<cn.appblog.lib.ui_widget.common.RotateTextView
    android:id="@+id/text"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_marginTop="2dp"
    app:degree="-50"
    android:textColor="#F7F7F7"
    android:textSize="13sp"
    />

java

RotateTextView mTextView = (RotateTextView) findViewById (R.id.text);  
mText.setDegrees(10);
上一篇 Android Parcelable中枚举的写法
下一篇 Genymotion模拟器无法开启的解决方法
目录
文章列表
1 Prometheus使用Consul实现自动服务发现
Prometheus使用Consul实现自动服务发现
2
Spring Boot指定事务管理器
Spring Boot指定事务管理器
3
Bigcommerce订单确认页接口调试
Bigcommerce订单确认页接口调试
4
PHP中 HMAC-MD5 加密算法
PHP中 HMAC-MD5 加密算法
5
Java中使用Gradle中声明的变量
Java中使用Gradle中声明的变量
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。