圆角框TextView

本文介绍了如何在Android中创建并实现具有圆角效果的TextView。通过定义自定义属性并在代码中应用,可以轻松达成目标效果。
摘要由CSDN通过智能技术生成

要以下效果:
这里写图片描述

直接上代码:

package com.jc.statsysicons;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;


public class CircleTextView extends TextView {

    private static final boolean DEBUG = false;
    private static final String TAG = "CircleTextView";
    private Paint mCirclePaint = new Paint();
    private Paint mLinePaint = new Paint();
    private int mStokeWidth = 2;
    private int mSpace = 2;
    private int mStrokeColor = Color.BLACK;

    PaintFlagsDrawFilter mPaintFlagsDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

    public CircleTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CircleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleTextView);
        mStokeWidth = typedArray.getDimensionPixelSize(R.styleable.CircleTextView_stroke_width, 2);
        mSpace = typedArray.getDimensionPixelSize(R.styleable.CircleTextView_space, 2);
        mStrokeColor = typedArray.getColor(R.styleable.CircleTextView_stroke_color, Color.BLACK);
        typedArray.recycle();
        if (DEBUG)Log.d(TAG, "mStokeWidth: " + mStokeWidth + ", mSpace: " + mSpace + ", mStrokeColor: " + mStrokeColor);
        // Circle paint init
        mCirclePaint.setColor(mStrokeColor);
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStrokeWidth(mStokeWidth);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        // Line paint init
        mLinePaint.setColor(mStrokeColor);
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStrokeWidth(mStokeWidth);
        mLinePaint.setStyle(Paint.Style.STROKE);
        this.setGravity(Gravity.CENTER);
    }

    public CircleTextView(Context context) {
        super(context);
        // Circle paint init
        mCirclePaint.setColor(mStrokeColor);
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStrokeWidth(mStokeWidth);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        // Line paint init
        mLinePaint.setColor(mStrokeColor);
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStrokeWidth(mStokeWidth);
        mLinePaint.setStyle(Paint.Style.STROKE);
        this.setGravity(Gravity.CENTER);
    }


    @Override
    public void draw(Canvas canvas) {
        canvas.setDrawFilter(mPaintFlagsDrawFilter);
        int width = getWidth();
        int height = getHeight();
        int radius = Math.min(width, height) / 2 - mSpace; // minus 1 for show stoke fully.
        if (DEBUG) Log.d(TAG, "width: " + width + ", height: " + height + ", radius: " + radius);
        if (width <= height) {
            canvas.drawCircle(width / 2, height / 2, radius, mCirclePaint);
        } else {
            int lineWidth = width - 2 * mSpace - 2 * radius;
            RectF oval = new RectF();
            oval.left = mSpace;
            oval.top = mSpace;
            oval.right = 2 * radius + mSpace;
            oval.bottom = height - mSpace;
            canvas.drawArc(oval, 90, 180, false, mCirclePaint);
            canvas.drawLine(radius + mSpace, mSpace,  radius + mSpace + lineWidth, mSpace, mLinePaint);
            canvas.drawLine(radius + mSpace, height - mSpace, radius + mSpace + lineWidth, height - mSpace, mLinePaint);
            oval.left = width - mSpace - 2 * radius;
            oval.top = mSpace;
            oval.right = width - mSpace;
            oval.bottom = height - mSpace;
            canvas.drawArc(oval, -90, 180, false, mCirclePaint);
        }
        super.draw(canvas);
    }
}

attrs.xml 定义属性值

    <declare-styleable name="CircleTextView">
        <attr name="stroke_width" format="dimension" />
        <!-- Space between circle and boundary of TextView -->
        <attr name="space" format="dimension" />
        <attr name="stroke_color" format="color" />
    </declare-styleable>

使用方法:

        <com.jc.statsysicons.CircleTextView
            android:text="6632"
            android:id="@+id/circleView"
            android:paddingLeft="4dip"
            android:paddingRight="4dip"
            android:minWidth="20dip"
            android:minHeight="20dip"
            RippleTextView:stroke_width="1dip"
            RippleTextView:space="2dip"
            android:layout_marginTop="40dip"
            RippleTextView:stroke_color="@color/colorAccent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值