需求:TextView的文字倾斜一定的角度
自定义TextView
public class RotateTextView extends ThemedTextView {
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.getInteger(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;
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RotateTextView">
<attr name="degree" format="integer" />
</declare-styleable>
</resources>
使用RotateTextView
xml
<cn.appblog.lib.ui_widget.common.RotateTextView
android:id="@+id/text"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="2dp"
app:degree="-50"
android:textColor="#F7F7F7"
android:textSize="13sp"
/>
java
RotateTextView mTextView = (RotateTextView) findViewById (R.id.text);
mText.setDegrees(10);