【实训】旋转的文字控件

来源:互联网 发布:抽签软件在线 编辑:程序博客网 时间:2024/05/22 02:11

因为项目要求,所以需要一个可以旋转的文字控件。

事实上这个控件并不难写。

1.继承TextView

2.成员变量mDegrees表示角度

3.重写onDraw方法

4.旋转canvas即可


import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.view.Gravity;import android.widget.TextView;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);        mDegrees = DEFAULT_DEGREES;    }    @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;    }}


原创粉丝点击