效果图:
一个数字时是圆形,多个数字时是椭圆。
package com.miduo.messagedemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.RectF;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
/**
*
* @author huozhenpeng www.miduo.com 2015-9-9
*
*/
public class DemoView extends View {
private Paint paint;
private String text;
private RectF rectf;
private int radius = 10;
private TextPaint textpaint;
private float width;
private FontMetrics metrics;
private int textsize = 24;
private float bottom;
public DemoView(Context context) {
this(context, null);
}
public DemoView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.message, 0, 0);
int n = typedArray.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.message_radius:
radius = (int) typedArray.getDimension(attr, 10);
break;
case R.styleable.message_text:
text = typedArray.getString(attr);
break;
case R.styleable.message_textsize:
textsize = (int) typedArray.getDimension(attr, 24);
break;
default:
break;
}
}
init();
}
private void init() {
paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
paint.setColor(Color.RED);
textpaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
textpaint.setColor(Color.parseColor("#ffffff"));
textpaint.setTextSize(textsize);
textpaint.setTextAlign(Align.CENTER);
metrics = textpaint.getFontMetrics();
bottom = Math.abs(metrics.ascent) + metrics.descent;
if (!TextUtils.isEmpty(text)) {
width = textpaint.measureText(text);
width += bottom - (textpaint.measureText("1"));
rectf = new RectF(0, 0, width + radius * 2, bottom + 2 * radius);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int specwidth=(int) (width + radius * 2);
int spcemode=MeasureSpec.EXACTLY;
int spechight=(int) (bottom + 2 * radius);
setMeasuredDimension(MeasureSpec.makeMeasureSpec(specwidth, spcemode), MeasureSpec.makeMeasureSpec(spechight, spcemode));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(!TextUtils.isEmpty(text))
{
canvas.drawOval(rectf, paint);
canvas.drawText(text, (width + radius * 2) / 2, (bottom + 2 * radius)
/ 2 + Math.abs((metrics.ascent + metrics.descent) / 2),
textpaint);
}
}
public void setText(String text) {
this.text = text;
}
public void setTextSize(int textSize) {
this.textsize = textSize;
}
public void setRadius(int radius) {
this.radius = radius;
}
}
配置文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:hzp="http://schemas.android.com/apk/res/com.miduo.messagedemo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#ffffff"
android:orientation="vertical"
tools:context=".MainActivity" >
<com.miduo.messagedemo.DemoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
hzp:radius="10px"
hzp:text="2"
hzp:textsize="25px" />
<com.miduo.messagedemo.DemoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
hzp:radius="10px"
hzp:text="25"
hzp:textsize="25px" />
<com.miduo.messagedemo.DemoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
hzp:radius="10px"
hzp:text="250"
hzp:textsize="25px" />
<com.miduo.messagedemo.DemoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
hzp:radius="10px"
hzp:text="2"
hzp:textsize="25px" />
<com.miduo.messagedemo.DemoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
hzp:radius="15px"
hzp:text="250"
hzp:textsize="30px" />
<com.miduo.messagedemo.DemoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
hzp:radius="15px"
hzp:text=""
hzp:textsize="30px" />
</LinearLayout>
(因为用到了自定义属性)
<resources>
<attr name="text" format="string" />
<attr name="radius" format="dimension"/>
<attr name="textsize" format="dimension"/>
<declare-styleable name="message">
<attr name="text" />
<attr name="radius" />
<attr name="textsize" />
</declare-styleable>
</resources>