BadgeView新提示开源工具类
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);
}
BadgeView新提示开源工具类的更多相关文章
- java8时间类API安全问题(赠送新的时间工具类哟)
LocalDateTime等新出的日期类全是final修饰的类,不能被继承,且对应的日期变量都是final修饰的,也就是不可变类.赋值一次后就不可变,不存在多线程数据问题. simpleDateFor ...
- 【Android工具类】用户输入非法内容时的震动与动画提示——EditTextShakeHelper工具类介绍
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 当用户在EditText中输入为空或者是数据异常的时候,我们能够使用Toast来提醒用户,除此之外,我们还能 ...
- UUID与System.currentTimeMillis()产生一个新文件名的工具类
1.FileUtils.java package Utils.GenerateNewFileName; import java.util.UUID; public class FileUtils { ...
- JS 日期工具类-基于yDate
源码下载 前言:最近在用到JS日期操作时,发现有一些不方便,于是搜素了一些网上的资料,基于一个开源工具类-yDate 进行了个性化定制,封装成了一个日期工具类工具函数大概介绍:1.普通的日期操作2. ...
- DBUtils-对JDBC简单封装的开源工具类库
DBUtils 是对JDBC简单封装的开源工具类 详细介绍地址: https://baike.baidu.com/item/Dbutils/10655914?fr=aladdin 在使用DBUtil ...
- laravel封装返回json信息工具类
1.工具类可以一次写入多方多方调用,很大程度的节约开发时间得到想要的信息 这里演示一个json接口的工具类(文件定义在App\Http\Controllers\Tools)中 <?php /** ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- Android开源项目大全 - 工具类
主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(2)-2个单实例工具类
从本篇开始,讲解net.micode.fileexplorer.util工具包中的类.这个包下的类,功能也比较单一和独立.很多代码的思想和实现,可以用于JavaWeb和Android等多种环境中. 一 ...
随机推荐
- Router和History (路由控制)-backbone
Router和History (路由控制) Backbone.Router担任了一部分Controller(控制器)的工作,它一般运行在单页应用中,能将特定的URL或锚点规则绑定到一个指定的方法(后文 ...
- Thinking in Java——集合(Collection)
一.ArrayList的使用(略) 二.容器的基本概念 (一).Collection是集合类的基本接口 主要方法: public interface Collection<E>{ bool ...
- VMware下设置CentOS虚拟机与主机同一网段
由于在开发中经常使用到自己的电脑搭建虚拟机器进行个人开发,而虚拟机器每次登录所使用的命令行界面比较小,看起来也不舒服.以下主要对centos虚拟机器下配置与主机共享同一网段IP,通过第三方软件(put ...
- 图片异步加载 ,KVO
图片异步下载类目: .h #import <UIKit/UIKit.h> typedef void (^ImageBlock)(UIImage *img); @interface UIIm ...
- Oracle 修改密码 解锁
1.怎么修改oracle用户密码 在以SYSDBA身份登陆时可以修改其他用户的密码,比如: SQL> alter user 用户名 identified by 新密码; 用户已更改. 这个是把U ...
- 浅谈 qmake 之 shadow build(就是将源码路径和构建路径分开)
shadow build shadow build 是什么东西?就是将源码路径和构建路径分开(也就是生成的makefile文件和其他产物都不放到源码路径),以此来保证源码路径的清洁. 这不是qmake ...
- C# 如何获取当前应用程序的父目录
//获取当前运行路径的上级目录(父目录) System.IO.DirectoryInfo topDir = System.IO.Directory.GetParent(System.Environme ...
- wikioi1450 xth的旅行
题目描述 Description 毕业了,Xth很高兴,因为他要和他的 rabbit 去双人旅行了.他们来到了水城威尼 斯.众所周知(⊙﹏⊙b汗),这里的水路交通很发达,所以 xth 和 rabbit ...
- LInux 下挂在Windows共享文件夹
挂载WIndow共享文件夹 //192.168.0.103/software mount -t smbfs -o username=administrator,password=“de123” / ...
- linux之SQL语句简明教程---表格连接
现在我们介绍连接 (Join) 的概念.要了解连接,我们需要用到许多我们之前已介绍过的指令.我们先假设我们有以下的两个表格, Store_Information 表格 Store_Name Sales ...