Git Product home page Git Product logo

tipradiobutton's Introduction

TipRadioButton

RadioButton添加消息提醒(小红点)

效果图

废话很多!!!直接上图好吧

Demo.gif

代码实现

首先,需求很简单,只需要能多显示一个小红点就 OK 了,所以我们只需要在原本的 RadioButton 控件上绘制一个圆型的、红色的一个图案就可以了。

自定义一个 TipRadioButton 直接继承 RadioButton ,然后再 onDraw() 方法中绘制一个我们需要的小红点,并为其设置一个绘制开关。

  • 定义小红点对象,设置圆角和红点显示的位置
private class Dot {
    int color;
    int radius;
    int marginTop;
    int marginRight;

    Dot() {
        float density = getContext().getResources().getDisplayMetrics().density;
        radius = (int) (5 * density);
        marginTop = (int) (3 * density);
        marginRight = (int) (3 * density);
        color = getContext().getResources().getColor(R.color.red);
    }

}
  • 绘制 RadioButton
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (isTipOn) {
        float cx = getWidth() - mDot.marginRight - mDot.radius;
        float cy = mDot.marginTop + mDot.radius;

        Drawable drawableTop = getCompoundDrawables()[1];
        if (drawableTop != null) {
            int drawableTopWidth = drawableTop.getIntrinsicWidth();
            if (drawableTopWidth > 0) {
                int dotLeft = getWidth() / 2 + drawableTopWidth / 2;
                cx = dotLeft + mDot.radius;
            }
        }

        Paint paint = getPaint();
        //save
        int tempColor = paint.getColor();

        paint.setColor(mDot.color);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(cx, cy, mDot.radius, paint);

        //restore
        paint.setColor(tempColor);
    }
}

打开方式

使用方法很简单,简单到你不需要看

<com.dreamfisn.tipdemo.TipRadioButton
        android:id="@+id/tip_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        />

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.