要以下效果:
直接上代码:
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"/>