Android TextView文字倾斜

需求:TextView的文字倾斜一定的角度

自定义TextView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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;
}
}
1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>  
<resources>
<declare-styleable name="RotateTextView">
<attr name="degree" format="integer" />
</declare-styleable>
</resources>

使用RotateTextView

xml

1
2
3
4
5
6
7
8
9
<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

1
2
RotateTextView mTextView = (RotateTextView) findViewById (R.id.text);  
mText.setDegrees(10);

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :