Android自定义View之随屏幕旋转的TextView

本博客介绍如何通过自定义TextView实现文字随屏幕旋转的功能,并提供了详细的代码实现步骤。旋转角度可通过设置轻松调整。

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

RotateTextView

在个别时候我们需要文字随屏幕旋转,那么该怎么实现呢?自定义TextView就可以.
接下来我们贴出RotateTextView的实现代码:
“` python
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class RotateTextView extends TextView {
private static final int DEFAULT_DEGREES = 0;
private int mDegrees;
public RotateTextView(Context context, AttributeSet attrs) {
super(context,attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.save();
    canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
    //设置绕中心旋转mDegrees度
    canvas.rotate(this.mDegrees, this.getWidth() / 2f, this.getHeight() / 2f);
    Log.i("degree>>>>>>>>>>>",this.mDegrees+"");
    super.onDraw(canvas);
    canvas.restore();
}

//设置旋转角度
public void setDegree(int degrees) {
this.mDegrees = degrees;
//设置刷新界面
this.volidate();
}

}

最新文章更新在微信公众号上,欢迎关注获取详情:
这里写图片描述

觉得有用请关注我的博客哦,本文原创,转载请注明出处.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值