Android实现消息提醒小红点效果

Android实现消息提醒小红点效果,第1张

概述本人分享一下,自己写的一个消息提醒小红点控件,支持圆、矩形、椭圆、圆角矩形、正方形五种图形样式,可带文字,支持链式 *** 作。

本人分享一下,自己写的一个消息提醒小红点控件,支持圆、矩形、椭圆、圆角矩形、正方形五种图形样式,可带文字,支持链式 *** 作。

先看一下实现效果,随便测了几个控件(TextVIEw、ImageVIEw、Radiobutton、linearLayout、relativeLayout、FrameLayout),不确定其他会不会有问题。

@R_403_5565@ androID.content.Context; @R_403_5565@ androID.graphics.Canvas; @R_403_5565@ androID.graphics.color; @R_403_5565@ androID.graphics.Paint; @R_403_5565@ androID.graphics.RectF; @R_403_5565@ androID.util.AttributeSet; @R_403_5565@ androID.util.Log; @R_403_5565@ androID.vIEw.Gravity; @R_403_5565@ androID.vIEw.VIEw; @R_403_5565@ androID.vIEw.VIEwGroup; @R_403_5565@ androID.Widget.FrameLayout; @R_403_5565@ 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,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,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,mTextPaint);   break;   case SHAPE_RECTANGLE:   canvas.drawRect(mRectF,mTextPaint);   break;   case SHAPE_SQUARE:   int sIDeLength = Math.min(getMeasuredHeight(),getMeasureDWIDth());   mRectF.set(0,sIDeLength,sIDeLength);   canvas.drawRect(mRectF,sIDeLength / 2f,sIDeLength / 2f    + (mtextH / 2f - FontMetrics.descent),mTextPaint);   break;   case SHAPTE_ROUND_RECTANGLE:   canvas.drawRoundRect(mRectF,dip2px(getContext(),getMeasureDWIDth()/2),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(),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);  }  } 

可自由定制自己喜欢的控件,为了方便使用这里还采用工厂模式封装一些基本方法,如下:

@R_403_5565@ androID.content.Context; @R_403_5565@ 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实现消息提醒小红点效果全部内容,希望文章能够帮你解决Android实现消息提醒小红点效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1143874.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存