自定义view实现未读消息提示(小红点)

转载:http://blog.csdn.net/qq_28268507/article/details/70314844

自定义view继承RadioButton

public class NotifyRadioButton extends RadioButton {

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
float radius;
boolean notify;

public NotifyRadioButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    paint.setColor(Color.RED);
    radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 12.0f, context.getResources().getDisplayMetrics());
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (notify) {
      //获取到DrawableTop,  0 drawableleft 1 drawabletop 2 drawableright 3 drawablebottom
        Drawable drawable = getCompoundDrawables()[1];
         //获取到Drawable的left right top bottom的值
        Rect bounds = drawable.getBounds();
        //float cx, float cy, float radius, @NonNull Paint paint
        float cx = getMeasuredWidth() / 2 + bounds.width() / 2 - radius / 2;
        float cy = getPaddingTop() + bounds.height() / 4;
        canvas.drawCircle(cx, cy, radius, paint);
    }
}

/**
 * 新消息提醒
 *
 * @param notify
 */
public void notify(boolean notify) {
    this.notify = notify;
    invalidate();
}

}

在代码中,通过id找到控件,调用notify(true),就可看到小红点的显示

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值