Android实现消息提醒小红点效果
程序员文章站
2023-12-04 17:41:46
本人分享一下,自己写的一个消息提醒小红点控件,支持圆、矩形、椭圆、圆角矩形、正方形五种图形样式,可带文字,支持链式操作。
先看一下实现效果,随便测了几个控件(text...
本人分享一下,自己写的一个消息提醒小红点控件,支持圆、矩形、椭圆、圆角矩形、正方形五种图形样式,可带文字,支持链式操作。
先看一下实现效果,随便测了几个控件(textview、imageview、radiobutton、linearlayout、relativelayout、framelayout),不确定其他会不会有问题。
import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.rectf; import android.util.attributeset; import android.util.log; import android.view.gravity; import android.view.view; import android.view.viewgroup; import android.widget.framelayout; import android.widget.tabwidget; public class badgeview extends view { protected static final string log_tag = "badgeview"; // 该控件的背景图形类型 public static final int shape_circle = 1; public static final int shape_rectangle = 2; public static final int shape_oval = 3; public static final int shapte_round_rectangle = 4; public static final int shape_square = 5; // 该框架内容的文本画笔 private paint mtextpaint; // 该控件的背景画笔 private paint mbgpaint; private int mheight = 0; private int mwidth = 0; private int mbackgroundshape = shape_circle; private int mtextcolor = color.white; private int mtextsize; private int mbgcolor = color.red; private string mtext = ""; private int mgravity = gravity.right | gravity.top; private rectf mrectf; private float mtexth; private boolean misshow = false; public badgeview(context context) { this(context, null); } public badgeview(context context, attributeset attrs) { this(context, attrs, 0); } public badgeview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); mrectf = new rectf(); mtextsize = dip2px(context, 1); mtextpaint = new paint(paint.anti_alias_flag); mtextpaint.setcolor(mtextcolor); mtextpaint.setstyle(paint.style.fill); mtextpaint.settextsize(mtextsize); mtextpaint.settextalign(paint.align.center); mbgpaint = new paint(paint.anti_alias_flag); mbgpaint.setcolor(mbgcolor); mbgpaint.setstyle(paint.style.fill); framelayout.layoutparams params = new framelayout.layoutparams( framelayout.layoutparams.wrap_content, framelayout.layoutparams.wrap_content); params.gravity = mgravity; setlayoutparams(params); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); mrectf.set(0, 0, getmeasuredwidth(), getmeasuredheight()); paint.fontmetrics fontmetrics = mtextpaint.getfontmetrics(); mtexth = fontmetrics.descent - fontmetrics.ascent; switch (mbackgroundshape) { case shape_circle: canvas.drawcircle(getmeasuredwidth() / 2f, getmeasuredheight() / 2f, getmeasuredwidth() / 2, mbgpaint); canvas.drawtext(mtext, getmeasuredwidth() / 2f, getmeasuredheight() / 2f + (mtexth / 2f - fontmetrics.descent), mtextpaint); break; case shape_oval: canvas.drawoval(mrectf, mbgpaint); canvas.drawtext(mtext, getmeasuredwidth() / 2f, getmeasuredheight() / 2f + (mtexth / 2f - fontmetrics.descent), mtextpaint); break; case shape_rectangle: canvas.drawrect(mrectf, mbgpaint); canvas.drawtext(mtext, getmeasuredwidth() / 2f, getmeasuredheight() / 2f + (mtexth / 2f - fontmetrics.descent), mtextpaint); break; case shape_square: int sidelength = math.min(getmeasuredheight(), getmeasuredwidth()); mrectf.set(0, 0, sidelength, sidelength); canvas.drawrect(mrectf, mbgpaint); canvas.drawtext(mtext, sidelength / 2f, sidelength / 2f + (mtexth / 2f - fontmetrics.descent), mtextpaint); break; case shapte_round_rectangle: canvas.drawroundrect(mrectf, dip2px(getcontext(), getmeasuredwidth()/2), dip2px(getcontext(), getmeasuredwidth()/2), mbgpaint); canvas.drawtext(mtext, getmeasuredwidth() / 2f, getmeasuredheight() / 2f + (mtexth / 2f - fontmetrics.descent), mtextpaint); break; } } /** * 设置该控件的背景颜色 * * @param color * 背景颜色 * @return badgeview */ public badgeview setbadgebackgroundcolor(int color) { mbgcolor = color; mbgpaint.setcolor(color); invalidate(); return this; } /** * 设置该控件的背景图形 * * @param shape * 图形 * @return */ public badgeview setbackgroundshape(int shape) { mbackgroundshape = shape; invalidate(); return this; } /** * 设置该控件的宽 * * @param width * 宽 * @return badgeview */ public badgeview setwidth(int width) { this.mwidth = width; this.setbadgelayoutparams(width, mheight); return this; } /** * 设置该控件的高 * * @param height * 高 * @return badgeview */ public badgeview setheight(int height) { this.mheight = height; this.setbadgelayoutparams(mwidth, height); return this; } /** * 设置该控件的高和宽 * * @param width * 宽 * @param height * 高 * @return */ public badgeview setbadgelayoutparams(int width, int height) { this.mwidth = width; this.mheight = height; framelayout.layoutparams params = (framelayout.layoutparams) getlayoutparams(); params.width = dip2px(getcontext(), width); params.height = dip2px(getcontext(), height); setlayoutparams(params); return this; } /** * 设置该控件的位置 * * @param gravity * 位置 * @return badgeview */ public badgeview setbadgegravity(int gravity) { mgravity = gravity; framelayout.layoutparams params = (framelayout.layoutparams) getlayoutparams(); params.gravity = gravity; setlayoutparams(params); return this; } /** * 设置该控件的高和宽、位置 * * @param width * 宽 * @param height * 高 * @param gravity * 位置 * @return badgeview */ public badgeview setbadgelayoutparams(int width, int height, int gravity) { framelayout.layoutparams params = (framelayout.layoutparams) getlayoutparams(); params.width = dip2px(getcontext(), width); params.height = dip2px(getcontext(), height); setlayoutparams(params); setbadgegravity(gravity); return this; } /** * 设置该控件的文本大小 * * @param size * 文本大小(sp) * @return */ public badgeview settextsize(int size) { mtextsize = sp2px(getcontext(), size); mtextpaint.settextsize(sp2px(getcontext(), size)); invalidate(); return this; } /** * 设置该控件的文本颜色 * * @param color * 文本颜色 * @return badgeview */ public badgeview settextcolor(int color) { mtextcolor = color; mtextpaint.setcolor(color); invalidate(); return this; } /** * 设置该控件的文本是否为粗体 * * @param flag */ public void setbadgeboldtext(boolean flag) { mtextpaint.setfakeboldtext(flag); invalidate(); } /** * 设置该控件要显示的整数文本 * * @param count * 要显示的整数文本 * @return badgeview */ public badgeview setbadgetext(int count) { mtext = string.valueof(count); invalidate(); return this; } /** * 设置该控件要显示的整数文本数字,超过指定上限显示为指定的上限内容 * * @param count * 要显示的整数文本 * @param maxcount * 数字上限 * @param text * 超过上限要显示的字符串文本 * @return badgeview */ public badgeview setbadgetext(int count, int maxcount, string text) { if (count <= maxcount) { mtext = string.valueof(count); } else { mtext = text; } invalidate(); return this; } /** * 设置该控件要显示的字符串文本 * * @param text * 要显示的字符串文本 * @return badgeview */ public badgeview setbadgetext(string text) { mtext = text; invalidate(); return this; } /** * 设置绑定的控件 * * @param view * 要绑定的控件 * @return badgeview */ public badgeview setbindview(view view) { misshow = true; if (getparent() != null) ((viewgroup) getparent()).removeview(this); if (view == null) return this; if (view.getparent() instanceof framelayout) { ((framelayout) view.getparent()).addview(this); } else if (view.getparent() instanceof viewgroup) { viewgroup parentcontainer = (viewgroup) view.getparent(); int viewindex = ((viewgroup) view.getparent()).indexofchild(view); ((viewgroup) view.getparent()).removeview(view); framelayout container = new framelayout(getcontext()); viewgroup.layoutparams containerparams = view.getlayoutparams(); container.setlayoutparams(containerparams); container.setid(view.getid()); view.setlayoutparams(new viewgroup.layoutparams( viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent)); container.addview(view); container.addview(this); parentcontainer.addview(container, viewindex); } else if (view.getparent() == null) { log.e(log_tag, "view must have a parent"); } return this; } /** * 设置绑定的控件 * * @param view 要绑定的控件 * @param tabindex 要绑定的控件的子项 */ public void setbindview(tabwidget view, int tabindex) { view tabview = view .getchildtabviewat(tabindex); this.setbindview(tabview); } /** * 移除绑定的控件 * * @return badgeview */ public boolean removebindview() { if (getparent() != null) { misshow = false; ((viewgroup) getparent()).removeview(this); return true; } return false; } /** * @return 改控件的显示状态 */ public boolean isshow() { return misshow; } /** * @return 控件的字符串文本 */ public string getbadgetext() { return mtext; } private int dip2px(context context, int dip) { return (int) (dip * getcontext().getresources().getdisplaymetrics().density + 0.5f); } private int sp2px(context context, float spvalue) { final float fontscale = context.getresources().getdisplaymetrics().scaleddensity; return (int) (spvalue * fontscale + 0.5f); } }
可*定制自己喜欢的控件,为了方便使用这里还采用工厂模式封装一些基本方法,如下:
import android.content.context; import android.view.gravity; public class badgefactory { public static badgeview create(context context) { return new badgeview(context); } public static badgeview createdot(context context) { return new badgeview(context).setbadgelayoutparams(10, 10) .settextsize(0) .setbadgegravity(gravity.right | gravity.top) .setbackgroundshape(badgeview.shape_circle); } public static badgeview createcircle(context context) { return new badgeview(context).setbadgelayoutparams(16, 16) .settextsize(12) .setbadgegravity(gravity.right | gravity.top) .setbackgroundshape(badgeview.shape_circle); } public static badgeview createrectangle(context context) { return new badgeview(context).setbadgelayoutparams(2, 20) .settextsize(12) .setbadgegravity(gravity.right | gravity.top) .setbackgroundshape(badgeview.shape_rectangle); } public static badgeview createoval(context context) { return new badgeview(context).setbadgelayoutparams(25, 20) .settextsize(12) .setbadgegravity(gravity.right | gravity.top) .setbackgroundshape(badgeview.shape_oval); } public static badgeview createsquare(context context) { return new badgeview(context).setbadgelayoutparams(20, 20) .settextsize(12) .setbadgegravity(gravity.right | gravity.top) .setbackgroundshape(badgeview.shape_square); } public static badgeview createroundrect(context context) { return new badgeview(context).setbadgelayoutparams(25, 20) .settextsize(12) .setbadgegravity(gravity.right | gravity.top) .setbackgroundshape(badgeview.shapte_round_rectangle); } }
源码下载:android实现消息提醒小红点
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Android仿微信底部菜单栏效果