Android 徽章控件

Last update: Jun 18, 2022

💗 BGABadgeView-Android 💗

Android Arsenal License Download

English Document

react-native-bga-badge-view

demo中演示了:

  • 微博底部tab显示最新微博条数
  • 微博列表用户头像显示显示右下角vip徽章
  • 微信消息界面用户新消息
  • 微信消息界面订阅号新消息
  • 使用v4包中的RoundedBitmapDrawable制作圆角头像
  • 拖拽删除徽章

爆炸效果参考的 ExplosionField 改成了只有一个View的情况,只刷新徽章附近的区域

效果图

badgeview

点击下载 Apk 或扫描下方二维码安装体验

Demo

添加 Gradle 依赖

  • maven { url 'https://jitpack.io' } 加入到 repositories 中
  • 添加如下依赖,末尾的「latestVersion」指的是徽章Download里的版本名称,请自行替换。
dependencies {
    implementation 'com.github.bingoogolapple.BGABadgeView-Android:api:latestVersion'
    annotationProcessor 'com.github.bingoogolapple.BGABadgeView-Android:compiler:latestVersion'
}

初始化徽章控件

  1. 在项目任意一个类上面添加 BGABadge 注解,例如新建一个类 BGABadgeInit 专门用于初始化徽章控件
  2. 需要哪些类具有徽章功能,就把那些类的 Class 作为 BGABadge 注解的参数「下面的代码块给出了例子,不需要的可以删掉对应的行」
@BGABadge({
        View.class, // 对应 cn.bingoogolapple.badgeview.BGABadgeView,不想用这个类的话就删了这一行
        ImageView.class, // 对应 cn.bingoogolapple.badgeview.BGABadgeImageView,不想用这个类的话就删了这一行
        TextView.class, // 对应 cn.bingoogolapple.badgeview.BGABadgeFloatingTextView,不想用这个类的话就删了这一行
        RadioButton.class, // 对应 cn.bingoogolapple.badgeview.BGABadgeRadioButton,不想用这个类的话就删了这一行
        LinearLayout.class, // 对应 cn.bingoogolapple.badgeview.BGABadgeLinearLayout,不想用这个类的话就删了这一行
        FrameLayout.class, // 对应 cn.bingoogolapple.badgeview.BGABadgeFrameLayout,不想用这个类的话就删了这一行
        RelativeLayout.class, // 对应 cn.bingoogolapple.badgeview.BGABadgeRelativeLayout,不想用这个类的话就删了这一行
        FloatingActionButton.class, // 对应 cn.bingoogolapple.badgeview.BGABadgeFloatingActionButton,不想用这个类的话就删了这一行
        ...
        ...
        ...
})
public class BGABadgeInit {
}
  1. 再 AS 中执行 Build => Rebuild Project
  2. 经过前面三个步骤后就可以通过「cn.bingoogolapple.badgeview.BGABadge原始类名」来使用徽章控件了

接口说明

/**
 * 显示圆点徽章
 */
void showCirclePointBadge();

/**
 * 显示文字徽章
 *
 * @param badgeText
 */
void showTextBadge(String badgeText);

/**
 * 隐藏徽章
 */
void hiddenBadge();

/**
 * 显示图像徽章
 *
 * @param bitmap
 */
void showDrawableBadge(Bitmap bitmap);

/**
 * 设置拖动删除徽章的代理
 *
 * @param delegate
 */
void setDragDismissDelegage(BGADragDismissDelegate delegate);

/**
 * 是否显示徽章
 *
 * @return
 */
boolean isShowBadge();

/**
 * 是否可拖动
 *
 * @return
 */
boolean isDraggable();

/**
 * 是否正在拖动
 *
 * @return
 */
boolean isDragging();

自定义属性说明

属性名 说明 默认值
badge_bgColor 徽章背景色 Color.RED
badge_textColor 徽章文本的颜色 Color.WHITE
badge_textSize 徽章文本字体大小 10sp
badge_verticalMargin 徽章背景与宿主控件上下边缘间距离 4dp
badge_horizontalMargin 徽章背景与宿主控件左右边缘间距离 4dp
badge_padding 徽章文本边缘与徽章背景边缘间的距离 4dp
badge_gravity 徽章在宿主控件中的位置 BGABadgeImageView和BGABadgeRadioButton是右上方,其他控件是右边垂直居中
badge_draggable 是否开启拖拽删除徽章 false
badge_isResumeTravel 拖拽徽章超出轨迹范围后,再次放回到轨迹范围时,是否恢复轨迹 false
badge_borderWidth 徽章描边宽度 0dp
badge_borderColor 徽章描边颜色 Color.WHITE
badge_dragExtra 触发开始拖拽徽章事件的扩展触摸距离 4dp

关于我

个人主页 邮箱 BGA 系列开源库 QQ 群 GitHub 喵(专注于 GitHub 等一切与程序员有关的内容)
bingoogolapple.cn [email protected] BGA_CODE_CLUB GitHub喵

打赏支持

如果您觉得 BGA 系列开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 🐵 就太 👍 了。您的支持将鼓励我继续创作:octocat:

如果您目前正打算购买通往墙外的梯子,可以使用我的邀请码「YFQ9Q3B」购买 Lantern,双方都赠送三个月的专业版使用时间 🍻

License

Copyright 2015 bingoogolapple

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

GitHub

https://github.com/bingoogolapple/BGABadgeView-Android
Comments
  • 1. 最新

    android studio 3.0中,26 版本编译出现以下错误。

    Unable to resolve dependency for ':[email protected]/compileClasspath': Could not resolve cn.bingoogolapple:bga-badgeview:1.1.3.

    Could not resolve cn.bingoogolapple:bga-badgeview:1.1.3. Required by: project :app

    Could not resolve cn.bingoogolapple:bga-badgeview:1.1.3. > Could not get resource 'https://www.jitpack.io/cn/bingoogolapple/bga-badgeview/1.1.3/bga-badgeview-1.1.3.pom'. > Could not HEAD 'https://www.jitpack.io/cn/bingoogolapple/bga-badgeview/1.1.3/bga-badgeview-1.1.3.pom'. Received status code 401 from server: Unauthorized

    Reviewed by yigepang at 2017-10-27 09:00
  • 2. 重新放回时曲线不能绘制

    拖拽徽章,当线断裂,再次放回到半径之内,曲线不能重新绘制

    // 处理断开事件 if (BGABadgeViewUtil.getDistanceBetween2Points(mDragCenter, mStickCenter) > mDismissThreshold) { mDismissAble = true; postInvalidate(); } else { mDismissAble = false; }

    在这里加上else体验会更好

    Reviewed by yhsj0919 at 2015-12-08 08:37
  • 3. 为什么RadioGroup的监听没效果?

    代码: main_bottom.setOnCheckedChangeListener(tabCheckedListener); RadioGroup.OnCheckedChangeListener tabCheckedListener = new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { ((BGABadgeRadioButton) main_bottom.getChildAt(i)).setChecked(true); viewPager.setCurrentItem(i,false); } }; 里面的BGABadgeRadioButton 点击没反应

    Reviewed by merhong2012 at 2015-09-14 09:04
  • 4. Exception when drag BGABadge

    it's just disappear when i drag the bgabadgeview, i found an exception in:

    @Override
     public boolean onTouchEvent(MotionEvent event) {
         try {
             switch (event.getAction()) {
                 case MotionEvent.ACTION_DOWN:
                     handleActionDown(event);
                     break;
                 case MotionEvent.ACTION_MOVE:
                     handleActionMove(event);
                     break;
                 case MotionEvent.ACTION_UP:
                 case MotionEvent.ACTION_CANCEL:
                     handleActionUp(event);
                     break;
             }
         } catch (Exception e) {
             // 确保自己能被移除
             removeSelfWithException();
         }
         return true;
     }
    
     private void handleActionDown(MotionEvent event) {
         if (mExplosionAnimator == null && getParent() == null) {
             mDragRadius = Math.min(mBadgeViewHelper.getBadgeRectF().width() / 2, mMaxDragRadius);
             mStickRadius = mDragRadius - mDragStickRadiusDifference;
             mDismissThreshold = (int) (mStickRadius * 10);
    
             mDismissAble = false;
             mIsDragDisappear = false;
    
             mWindowManager.addView(this, mLayoutParams);
    
             updateDragPosition(event.getRawX(), event.getRawY());
         }
     }
    

    image

    p.s. the context is activity

    Reviewed by t894924815 at 2018-06-05 10:29
  • 5. getNavigationBarHeight 的疑问

    BGABadgeViewUtil 的 getNavigationBarHeight 里怎么写的是resources.getIdentifier("status_bar_height", "dimen", "android");,而内部调用的checkDeviceHasNavigationBar 里却检查的是 rs.getIdentifier("config_showNavigationBar", "bool", "android")。 第一个应该是navigation_bar_height吧?

    getNewStartY 里为什么不直接使用 getHeight,如果要手动计算可绘制高度,除了要减去状态栏的高度外,还要减去标题栏的高度(toolbar、custom toolbar...),标题栏高度这个不好处理。

    Reviewed by TomeOkin at 2016-03-17 06:07
  • 6. Wonderful Badge View

    1,showCriclePointBadge 应该是笔误 showCirclePointBadge. 2,请教一下为什么要使用AppCompatCheckedTextView、AppCompatTextView、AppCompatRadioButton而不用CheckedTextView、TextView、RadioButton减少依赖?

    Reviewed by cc1rr at 2015-07-08 08:20
  • 7. 三步编译完成,引入xml文件中,运行报错

    Caused by: android.view.InflateException: Binary XML file line #27: Error inflating class cn.bingoogolapple.badgeview.BGADragBadgeView Caused by: java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet]

    Reviewed by xyZangGit at 2019-02-19 09:11
  • 8. BGABadgeRadioGroup 是无法正常构建的

    错误: 对于RadioGroup(Context,AttributeSet,int), 找不到合适的构造器 构造器 RadioGroup.RadioGroup(Context)不适用 (实际参数列表和形式参数列表长度不同) 构造器 RadioGroup.RadioGroup(Context,AttributeSet)不适用 (实际参数列表和形式参数列表长度不同)

    1549957479 1

    Reviewed by ShowMeThe at 2019-02-12 07:45
  • 9. 注解生成的BGABadgeRadioButton无法点击选中

    无法点击BGABadgeRadioButton来触发以下事件 mTabRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.brb_main_home: ToastUtil.show("首页"); break; case R.id.brb_main_message: ToastUtil.show("消息"); break; case R.id.brb_main_discover: ToastUtil.show("发现"); break; case R.id.brb_main_me: ToastUtil.show("我"); break; } } });

    Reviewed by yhyzgn at 2018-03-09 06:24
  • 10. 依赖包完全导入不了,不能使用

    implementation 'com.github.bingoogolapple.BGABadgeView-Android:api:1.2.0' kapt 'com.github.bingoogolapple.BGABadgeView-Android:compiler:1.2.0' 依赖包完全导入不了,不能使用

    Reviewed by jyjiawohao at 2021-08-17 06:59
  • 11. 添加增量编译支持

    as 3.5 之后,as引入了注释处理器增量编译api支持,可以增加编译速度 BGABadge在这个版本下编译出现了以下警告

    w: [kapt] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: cn.bingoogolapple.badgeview.compiler.BGABadgeProcessor (NON_INCREMENTAL).

    Reviewed by chenfei0928 at 2019-08-29 10:41
Related tags
TourGuide is an Android library that aims to provide an easy way to add pointers with animations over a desired Android View

TourGuide TourGuide is an Android library. It lets you add pointer, overlay and tooltip easily, guiding users on how to use your app. Refer to the exa

Jun 15, 2022
Bubbles for Android is an Android library to provide chat heads capabilities on your apps. With a fast way to integrate with your development.
Bubbles for Android is an Android library to provide chat heads capabilities on your apps. With a fast way to integrate with your development.

Bubbles for Android Bubbles for Android is an Android library to provide chat heads capabilities on your apps. With a fast way to integrate with your

Jun 15, 2022
View that imitates Ripple Effect on click which was introduced in Android L (for Android 2.3+)
View that imitates Ripple Effect on click which was introduced in Android L  (for Android 2.3+)

RippleView View that imitates Ripple Effect on click which was introduced in Android L. Usage For a working implementation, Have a look at the Sample

May 8, 2022
A new canvas drawing library for Android. Aims to be the Fabric.js for Android. Supports text, images, and hand/stylus drawing input. The library has a website and API docs, check it out

FabricView - A new canvas drawing library for Android. The library was born as part of a project in SD Hacks (www.sdhacks.io) on October 3rd. It is cu

Jun 18, 2022
MarkdownView is an Android webview with the capablity of loading Markdown text or file and display it as HTML, it uses MarkdownJ and extends Android webview.
MarkdownView is an Android webview with the capablity of loading Markdown text or file and display it as HTML, it uses MarkdownJ and extends Android webview.

About MarkdownView (Markdown For Android) is an Android library that helps you display Markdown text or files (local/remote) as formatted HTML, and st

Jun 6, 2022
SwipeBack for Android Activities to do pretty the same as the android "back-button" will do, but in a really intuitive way by using a swipe gesture
SwipeBack for Android Activities to do pretty the same as the android

SwipeBack SwipeBack is for Android Activities to do pretty the same as the android "back-button" will do, but in a really intuitive way by using a swi

May 11, 2022
A backport of the SwitchPreference component that was introduced on Android 4 (ICS / level 14). This port works on Android 2.1+ (Eclair MR1 / level 7).

Android Switch Preference Backport A backport of the SwitchPreference component that was introduced on Android 4 (ICS / level 14). This port works on

May 11, 2022
Wizard Pager is a library that provides an example implementation of a Wizard UI on Android, it's based of Roman Nurik's wizard pager (https://github.com/romannurik/android-wizardpager)
Wizard Pager is a library that provides an example implementation of a Wizard UI on Android, it's based of Roman Nurik's wizard pager (https://github.com/romannurik/android-wizardpager)

Wizard Pager Wizard Pager is a library that provides an example implementation of a Wizard UI on Android, it's based of Roman Nurik's wizard pager (ht

Jun 21, 2022
Make your native android Toasts Fancy. A library that takes the standard Android toast to the next level with a variety of styling options. Style your toast from code.
Make your native android Toasts Fancy. A library that takes the standard Android toast to the next level with a variety of styling options. Style your toast from code.

FancyToast-Android Prerequisites Add this in your root build.gradle file (not your module build.gradle file): allprojects { repositories { ... ma

Jun 17, 2022
Make your native android Dialog Fancy. A library that takes the standard Android Dialog to the next level with a variety of styling options. Style your dialog from code.
Make your native android Dialog Fancy. A library that takes the standard Android Dialog to the next level with a variety of styling options. Style your dialog from code.

FancyAlertDialog-Android Prerequisites Add this in your root build.gradle file (not your module build.gradle file): allprojects { repositories { ..

Apr 18, 2022
Xamarin.Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
Xamarin.Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#

Xamarin.Android Xamarin.Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#. Build Status Platform

Jun 14, 2022
A Tinder-like Android library to create the swipe cards effect. You can swipe left or right to like or dislike the content.

Swipecards Travis master: A Tinder-like cards effect as of August 2014. You can swipe left or right to like or dislike the content. The library create

Jun 8, 2022
Bootstrap style widgets for Android, with Glyph Icons
Bootstrap style widgets for Android, with Glyph Icons

Android-Bootstrap Android Bootstrap is an Android library which provides custom views styled according to the Twitter Bootstrap Specification. This al

Jun 15, 2022
[] A fast PDF reader component for Android development
[] A fast PDF reader component for Android development

This project is no longer maintained. You can find a good replacement here, which is a fork relying on Pdfium instead of Vudroid/MuPDF for decoding PD

Jun 16, 2022
[] Define and render UI specs on top of your Android UI
[] Define and render UI specs on top of your Android UI

dspec A simple way to define and render UI specs on top of your Android UI. Usage Enclose the target UI with a DesignSpecFrameLayout, usually the root

Feb 15, 2022
A Material design Android pincode library. Supports Fingerprint.
A Material design Android pincode library. Supports Fingerprint.

LolliPin A Lollipop material design styled android pincode library (API 14+) To include in your project, add this to your build.gradle file: //Loll

Jun 18, 2022
Android Library to implement simple touch/tap/swipe gestures
Android Library to implement simple touch/tap/swipe gestures

SimpleFingerGestures An android library to implement simple 1 or 2 finger gestures easily Example Library The library is inside the libSFG folder Samp

Jun 13, 2022
Android layout decorators : Injecting custom attributes in layout files, Using decorators to get rid of unnecessary class explosion with custom views
Android layout decorators : Injecting custom attributes in layout files, Using decorators to get rid of unnecessary class explosion with custom views

Decor Decor is a library that applies decorators to Android layout with additional attributes without the need to extend and create a custom View for

May 7, 2022