Android 自定义倾斜字体

本文介绍了一种自定义的TextView组件——RotateTextView,该组件允许文本以任意角度旋转显示。文章详细展示了如何通过继承AppCompatTextView并重写onMeasure和onDraw方法来实现这一功能,同时提供了设置旋转角度的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class RotateTextView extends AppCompatTextView {
    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(-27,25);
        canvas.rotate(mDegrees);
        super.onDraw(canvas);
        canvas.restore();
    }

    public void setDegrees(int degrees) {
        mDegrees = degrees;
    }
}

<com.h3c.classboard.teacher.RotateTextView
    android:id="@+id/info"
    android:layout_width="72dp"
    android:layout_height="72dp"
    android:gravity="center"
    android:text="哈 哈"
    android:textColor="@color/colorWhite"
    android:textSize="@dimen/font14"
    app:degree="-46dp" />

  

转载于:https://www.cnblogs.com/candyzhmm/p/10382917.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值