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);

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/android-textview-text-italic/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android TextView文字倾斜
需求:TextView的文字倾斜一定的角度 自定义TextView public class RotateTextView extends ThemedTextView { private static final int DEFAULT_DEGREES……
<<上一篇
下一篇>>
文章目录
关闭
目 录