文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2023-05-30 18:13

关注

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

先看一下实现效果,随便测了几个控件(TextView、ImageView、RadioButton、LinearLayout、RelativeLayout、FrameLayout),不确定其他会不会有问题。

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

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;  }   }     public BadgeView setBadgeBackgroundColor(int color) {  mBgColor = color;  mBgPaint.setColor(color);  invalidate();  return this;  }     public BadgeView setBackgroundShape(int shape) {  mBackgroundShape = shape;  invalidate();  return this;  }     public BadgeView setWidth(int width) {  this.mWidth = width;  this.setBadgeLayoutParams(width, mHeight);  return this;   }     public BadgeView setHeight(int height) {  this.mHeight = height;  this.setBadgeLayoutParams(mWidth, height);  return this;  }     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;  }     public BadgeView setBadgeGravity(int gravity) {  mGravity = gravity;  FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();  params.gravity = gravity;  setLayoutParams(params);  return this;  }     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;  }     public BadgeView setTextSize(int size) {  mTextSize = sp2px(getContext(), size);  mTextPaint.setTextSize(sp2px(getContext(), size));  invalidate();  return this;  }     public BadgeView setTextColor(int color) {  mTextColor = color;  mTextPaint.setColor(color);  invalidate();  return this;  }     public void setBadgeBoldText(boolean flag) {  mTextPaint.setFakeBoldText(flag);  invalidate();  }     public BadgeView setBadgeText(int count) {  mText = String.valueOf(count);  invalidate();  return this;  }     public BadgeView setBadgeText(int count, int maxCount, String text) {  if (count <= maxCount) {   mText = String.valueOf(count);  } else {   mText = text;  }  invalidate();  return this;  }     public BadgeView setBadgeText(String text) {  mText = text;  invalidate();  return this;  }     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;  }     public void setBindView(TabWidget view, int tabIndex) {  View tabView = view   .getChildTabViewAt(tabIndex);  this.setBindView(tabView);  }     public boolean removebindView() {  if (getParent() != null) {   mIsShow = false;   ((ViewGroup) getParent()).removeView(this);   return true;  }  return false;  }     public boolean isShow() {  return mIsShow;  }     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);  }  } 

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 资料下载
  • 历年真题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯