直播源码网站,消息图标在收到消息时展示消息条数

直播源码网站,消息图标在收到消息时展示消息条数实现的相关代码

使用

 

1
    <YourPackgaeName.NumberImageView<br>        android:layout_width="wrap_content"<br>        android:layout_height="45dp"<br>        android:paddingHorizontal="20dp"<br>        android:layout_marginVertical="10dp"<br>        android:src="@mipmap/ic_shop_sign"<br>        app:number="15"<br>        app:numberMarginRight="18dp"<br>        app:numberTextSize="12sp" />

源码

1.attrs.xml

 

1
<br>    <attr name="numbersTextSize" format="dimension|reference" /><br>    <attr name="numberMarginTop" format="dimension|reference" /><br>    <attr name="numberMarginRight" format="dimension|reference" /><br>    <attr name="number" format="string|reference" /><br>    <attr name="numberTextColor" format="color|reference" /><br>    <attr name="numberTextSize" format="dimension|reference" /><br>    <attr name="numberSolidColor" format="color|reference" /><br>    <declare-styleable name="NumberImageView"><br>        <attr name="numberMarginTop" /><br>        <attr name="numberMarginRight" /><br>        <attr name="numberSolidColor" /><br>        <attr name="numberTextColor" /><br>        <attr name="numberTextSize" /><br>        <attr name="number" /><br>    </declare-styleable>

 

 

2.代码

 

1
<br>import android.content.Context;<br>import android.content.res.TypedArray;<br>import android.graphics.Canvas;<br>import android.graphics.Color;<br>import android.graphics.Paint;<br>import android.graphics.Rect;<br>import android.graphics.RectF;<br>import android.support.v7.widget.AppCompatImageView;<br>import android.support.v7.widget.AppCompatTextView;<br>import android.text.TextUtils;<br>import android.util.AttributeSet;<br>/**<br> * 圆点数字 + 文字<br> */<br>public class NumberTextView extends AppCompatTextView {<br>    private Paint paint;<br>    //圆点文字<br>    private String text = "0";<br>    //圆点文字大小<br>    private int textSize = 16;<br>    //圆点文字颜色<br>    private int textColor = Color.WHITE;<br>    //圆点颜色<br>    private int solidColor = Color.RED;<br>    //圆点半径大小<br>    private int radius = 18;<br>    //圆点文字上间距<br>    private int textMarginTop = 10;<br>    //圆点文字下间距<br>    private int textMarginRight = 10;<br>    public NumberTextView(Context context) {<br>        super(context);<br>        initAttributeSet(context, null);<br>    }<br>    public NumberTextView(Context context, AttributeSet attrs) {<br>        super(context, attrs);<br>        initAttributeSet(context, attrs);<br>    }<br>    public NumberTextView(Context context, AttributeSet attrs, int defStyleAttr) {<br>        super(context, attrs, defStyleAttr);<br>        initAttributeSet(context, attrs);<br>    }<br>    private void initAttributeSet(Context context, AttributeSet attrs) {<br>        if (attrs != null) {<br>            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.NumberTextView);<br>            text = array.getString(R.styleable.NumberTextView_number);<br>            text = TextUtils.isEmpty(text) ? "0" : text;<br>            textSize = array.getDimensionPixelSize(R.styleable.NumberTextView_numberTextSize, textSize);<br>            textColor = array.getColor(R.styleable.NumberTextView_numberTextColor, textColor);<br>            solidColor = array.getColor(R.styleable.NumberTextView_numberSolidColor, solidColor);<br>            textMarginTop = array.getDimensionPixelSize(R.styleable.NumberTextView_numberMarginTop, textMarginTop);<br>            textMarginRight = array.getDimensionPixelSize(R.styleable.NumberTextView_numberMarginRight, textMarginRight);<br>        }<br>    }<br>    @Override<br>    protected void onDraw(Canvas canvas) {<br>        super.onDraw(canvas);<br>        if (text.equals("0")) {<br>            return;<br>        }<br>        paint = new Paint();<br>        paint.setAntiAlias(true);<br>        //圆点背景<br>        paint.setColor(solidColor);<br>        int textSpace = 0;<br>        if (text.length() > 2) {<br>            textSpace = (text.length() - 2) * textSize / 2;<br>        }<br>        int cx = getMeasuredWidth() - radius - textSpace - textMarginRight;<br>        int cy = radius + textMarginTop;<br>        paint.setStyle(Paint.Style.FILL);<br>        RectF rectF = new RectF(cx - radius - textSpace, cy - radius, cx + radius + textSpace, cy + radius);<br>        canvas.drawRoundRect(rectF, radius, radius, paint);<br>        //文字<br>        paint = new Paint();<br>        paint.setAntiAlias(true);<br>        paint.setColor(textColor);<br>        paint.setTextSize(textSize);<br>        Rect rect = new Rect();<br>        paint.getTextBounds(text, 0, text.length(), rect);<br>        canvas.drawText(text, cx - rect.width() / 2, cy + rect.height() / 2, paint);<br>    }<br>    /**<br>     * 设置文字<br>     *<br>     * @param text<br>     */<br>    public void setText(String text) {<br>        this.text = text;<br>        invalidate();<br>    }<br>    /**<br>     * 设置文字大小<br>     *<br>     * @param textSize<br>     */<br>    public void setTextSize(int textSize) {<br>        this.textSize = textSize;<br>        invalidate();<br>    }<br>    /**<br>     * 设置颜色<br>     *<br>     * @param solidColor<br>     */<br>    public void setSolidColor(int solidColor) {<br>        this.solidColor = solidColor;<br>        invalidate();<br>    }<br>    /**<br>     * 设置圆点半径<br>     *<br>     * @param radius<br>     */<br>    public void setRadius(int radius) {<br>        this.radius = radius;<br>        invalidate();<br>    }<br>}

 

以上就是直播源码网站,消息图标在收到消息时展示消息条数实现的相关代码, 更多内容欢迎关注之后的文章

 

posted @   云豹科技-苏凌霄  阅读(45)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示