Android TextView 背景斜线

图片示例

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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值