仿微信消息数目提示的自定义view

public class TipNumberView extends TextView {
    private Paint mBgPaint;
    //抗锯齿
    PaintFlagsDrawFilter pfd;

    public TipNumberView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //初始化画笔
        mBgPaint = new Paint();
        mBgPaint.setColor(Color.RED);
        mBgPaint.setAntiAlias(true);
        pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    }

    public TipNumberView(Context context) {
        this(context, null);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = getMeasuredWidth();//获取view的宽度
        int measuredHeight = getMeasuredHeight();//获取view的高度
        //取高度和宽度中的最大值,然后在onDraw();方法中根据较大的那个值作为直径画圆。
        int max = Math.max(measuredWidth, measuredHeight);
        setMeasuredDimension(max, max);
    }

    @Override
    public void setBackgroundColor(int color) {
        mBgPaint.setColor(color);
    }
    //设置通知个数显示
    public void setNotifiText(int text) {
        setText(text + "");//继承了TextView,所以有setText()方法
    }

    public void setNotifiText(String text) {
        setText(text);
    }

    //draw方法比onDraw方法绘制 的东西更多一些。在这里用draw方法来绘制背景。
    @Override
    public void draw(Canvas canvas) {
        //设置绘图无锯齿
        canvas.setDrawFilter(pfd);
        //下面三个参数:1.参数一是中心点的x轴,参数二是中心点的y轴,参数三是半径,参数四是paint对象;
        //画圆
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, Math.max(getWidth(), getHeight()) / 2, mBgPaint);
        super.draw(canvas);
    }
}
content_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.tipnumberview.MainActivity" >

    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        >
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@mipmap/ic_launcher"
            />
        <com.mrl.fangqqmessagenumber.TipNumberView
            android:id="@+id/tip"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:textSize="11sp"
            android:gravity="center"
            android:text="13"
            />
    </RelativeLayout>

</RelativeLayout>

MainActivity.class 
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        TipNumberView tv = (TipNumberView) findViewById(R.id.tip);
        //设置消息数目19条
        tv.setNotifiText(19);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值