BadgeView新提示开源工具类
2024-10-10 17:29:07
BadgeView是使用某个图标作为新功能的提醒,类似于收到短息后短信图标的右上方有信息数目或者其他的显示性提示。BadgeView很好的实现了这个功能,而且进行了拓展,可自定义位置和提示图标。
工具类源码如下:
package cn.car273.widget; /*
* BadgeView.java
* BadgeView
*
* Copyright (c) 2012 Stefan Jauker.
* https://github.com/kodex83/BadgeView
*
* 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.
*/ import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TabWidget;
import android.widget.TextView; public class BadgeView extends TextView { private boolean mHideOnNull = true; public BadgeView(Context context) {
this(context, null);
} public BadgeView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
} public BadgeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); init();
} private void init() {
if (!(getLayoutParams() instanceof LayoutParams)) {
LayoutParams layoutParams =
new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.RIGHT | Gravity.TOP);
setLayoutParams(layoutParams);
} // set default font
setTextColor(Color.WHITE);
setTypeface(Typeface.DEFAULT_BOLD);
setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
setPadding(dip2Px(5), dip2Px(1), dip2Px(5), dip2Px(1)); // set default background
setBackground(9, Color.parseColor("#d3321b")); setGravity(Gravity.CENTER); // default values
setHideOnNull(true);
setBadgeCount(0);
} @SuppressWarnings("deprecation")
public void setBackground(int dipRadius, int badgeColor) {
int radius = dip2Px(dipRadius);
float[] radiusArray = new float[] { radius, radius, radius, radius, radius, radius, radius, radius }; RoundRectShape roundRect = new RoundRectShape(radiusArray, null, null);
ShapeDrawable bgDrawable = new ShapeDrawable(roundRect);
bgDrawable.getPaint().setColor(badgeColor);
setBackgroundDrawable(bgDrawable);
} /**
* @return Returns true if view is hidden on badge value 0 or null;
*/
public boolean isHideOnNull() {
return mHideOnNull;
} /**
* @param hideOnNull the hideOnNull to set
*/
public void setHideOnNull(boolean hideOnNull) {
mHideOnNull = hideOnNull;
setText(getText());
} /*
* (non-Javadoc)
*
* @see android.widget.TextView#setText(java.lang.CharSequence, android.widget.TextView.BufferType)
*/
@Override
public void setText(CharSequence text, BufferType type) {
if (isHideOnNull() && (text == null || text.toString().equalsIgnoreCase("0"))) {
setVisibility(View.GONE);
} else {
setVisibility(View.VISIBLE);
}
super.setText(text, type);
} public void setBadgeCount(int count) {
setText(String.valueOf(count));
} public Integer getBadgeCount() {
if (getText() == null) {
return null;
} String text = getText().toString();
try {
return Integer.parseInt(text);
} catch (NumberFormatException e) {
return null;
}
} /** 显示的位置,相对于依附对象 */
public void setBadgeGravity(int gravity) {
FrameLayout.LayoutParams params = (LayoutParams) getLayoutParams();
params.gravity = gravity;
setLayoutParams(params);
} public int getBadgeGravity() {
FrameLayout.LayoutParams params = (LayoutParams) getLayoutParams();
return params.gravity;
} public void setBadgeMargin(int dipMargin) {
setBadgeMargin(dipMargin, dipMargin, dipMargin, dipMargin);
} public void setBadgeMargin(int leftDipMargin, int topDipMargin, int rightDipMargin, int bottomDipMargin) {
FrameLayout.LayoutParams params = (LayoutParams) getLayoutParams();
params.leftMargin = dip2Px(leftDipMargin);
params.topMargin = dip2Px(topDipMargin);
params.rightMargin = dip2Px(rightDipMargin);
params.bottomMargin = dip2Px(bottomDipMargin);
setLayoutParams(params);
} public int[] getBadgeMargin() {
FrameLayout.LayoutParams params = (LayoutParams) getLayoutParams();
return new int[] { params.leftMargin, params.topMargin, params.rightMargin, params.bottomMargin };
} public void incrementBadgeCount(int increment) {
Integer count = getBadgeCount();
if (count == null) {
setBadgeCount(increment);
} else {
setBadgeCount(increment + count);
}
} public void decrementBadgeCount(int decrement) {
incrementBadgeCount(-decrement);
} /*
* Attach the BadgeView to the TabWidget
*
* @param target the TabWidget to attach the BadgeView
*
* @param tabIndex index of the tab
*/
public void setTargetView(TabWidget target, int tabIndex) {
View tabView = target.getChildTabViewAt(tabIndex);
setTargetView(tabView);
} /*
* Attach the BadgeView to the target view
*
* @param target the view to attach the BadgeView
*/
public void setTargetView(View target) {
if (getParent() != null) {
((ViewGroup) getParent()).removeView(this);
} if (target == null) {
return;
} if (target.getParent() instanceof FrameLayout) {
((FrameLayout) target.getParent()).addView(this); } else if (target.getParent() instanceof ViewGroup) {
// use a new Framelayout container for adding badge
ViewGroup parentContainer = (ViewGroup) target.getParent();
int groupIndex = parentContainer.indexOfChild(target);
parentContainer.removeView(target); FrameLayout badgeContainer = new FrameLayout(getContext());
ViewGroup.LayoutParams parentlayoutParams = target.getLayoutParams(); parentContainer.addView(badgeContainer, groupIndex, parentlayoutParams);
badgeContainer.addView(target); badgeContainer.addView(this);
} else if (target.getParent() == null) {
Log.e(getClass().getSimpleName(), "ParentView is needed");
} } /*
* converts dip to px
*/
private int dip2Px(float dip) {
return (int) (dip * getContext().getResources().getDisplayMetrics().density + 0.5f);
}
}
具体实现代码如下:
private void initBadgeView(Context context){
badgeView = new BadgeView(context);
badgeView.setBackgroundResource(R.drawable.subscription_num_bg);
badgeView.setIncludeFontPadding(false);
badgeView.setGravity(Gravity.CENTER);
badgeView.setTextSize(8f);
badgeView.setTextColor(Color.WHITE);
//设置提示信息
badgeView.setBadgeCount(23);
//添加依附的对象View
badgeView.setTargetView(imgIv); }
最新文章
- 用C语言编写生成小学四则运算程序
- 学习总结relative和absolute
- java设计模式(二)---工厂方法模式
- 强连通 HDU 1827
- shiro添加注解@RequiresPermissions不起作用
- Jquery easyui treegrid实现树形表格的行拖拽
- 学习日记之模板方法模式和 Effective C++
- HDU5086Revenge of Segment Tree(数论)
- icon的使用
- Unbutu14.04 启用 root 并禁用guest
- Asp.Net下,基于Jquery的Ajax二级联动
- app 下载更新 file-downloader 文件下载库的简单介绍和使用
- 距离度量以及python实现(二)
- HTTPD三种工作模型
- 详细分析LoadRunner参数化
- OninitDialog与OnCreate两个消息有何区别
- win10版office365激活序列码
- 我的G++编译选项
- (转)Springboot日志配置(超详细,推荐)
- JQuery基本知识(3)
热门文章
- [转]C语言的那些秘密之---函数返回局部变量
- undefined reference to `_sbrk', `_write', `_lseek', `_read'
- VS2012常用快捷建(必备)
- Nginx 教程的连载计划
- Geoserver基本使用、WMS服务发布与OpenLayers测试
- [IOS]UIWebView 请求网络页面或者加载本地资源页面
- linux共享内存简析
- MS SQL 当记录不存在时插入insert INTO not exists
- python基础教程_学习笔记14:标准库:一些最爱——re
- session劫持以及预防