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<declare-styleable name="RotateTextView"> <attr name="degree" format="dimension" /> </declare-styleable>
//布局文件<RotateTextView 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旋转文字
最新推荐文章于 2024-08-26 03:45:44 发布