未读消息

效果图:





一个数字时是圆形,多个数字时是椭圆。


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>



attrs.xml文件

(因为用到了自定义属性)

<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>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值