1.自定义LineTextView,使LineTextView继承TextView;
public class LineTextView extends TextView{}
2.重写够构造方法
public LineTextView(Context context, AttributeSet attrs) {
init();
}
3.初始化
private void init() {
mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mLineColor = getResources().getColor(R.color.color_999);
mLinePaint.setColor(mLineColor);
}
4.重写ondraw方法
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//斜线坐标
int startX, startY, endX, endY;
//视图宽高
int width = getWidth();
int height = getHeight();
//计算坐标
startX = (int) ((width / 2) * (1 - Math.cos(mAngle)));
startY = (int) ((height / 2) * (1 - Math.sin(mAngle)));
endX = (int) ((width / 2) * (1 + Math.cos(mAngle)));
endY = (int) ((height / 2) * (1 + Math.sin(mAngle)));
//画斜线
canvas.drawLine(startX, startY, endX, endY, mLinePaint);
}
5.布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.administrator.myline.LineTextView
android:id="@+id/dchoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D"
android:layout_centerInParent="true"
android:textSize="12sp"
android:layout_marginLeft="5dp"
android:background="@drawable/circle_choice"
android:gravity="center"/>
</RelativeLayout>
6.@drawable/circle_choice文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:color="#333"
android:width="1dp"/>
<size android:width="24dp"
android:height="24dp"/>
<solid android:color="#ffffff"/>
</shape>