自定义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);
        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(getCompoundPaddingLeft(), getExtendedPaddingTop());
        canvas.rotate(mDegrees, this.getWidth() / 2f, this.getHeight() / 2f);
        super.onDraw(canvas);
        canvas.restore();
    }

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


// style

name="RotateTextView">
    name="degree" format="dimension" />



//布局文件

    android:id="@+id/tv_tag2"
    android:text="haha "
    android:autoLink="all"
    android:layout_marginLeft="5dp"
    android:textSize="15sp"
    android:paddingBottom="19dp"
    android:paddingRight="2dp"
    android:textColor="@color/white"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@mipmap/pb_ic_lable"/>

//代码
tv .setDegrees( 315) ;


你可能感兴趣的:(自定义textview旋转文字)