结合闲时整理(3)------消息提示标签 BadgeView和闲时整理(4)--圆形TextView修改整理了CircleBadgeView这个类,标签是圆形的了,以前的标签是圆角矩形。

package com.glory.room.view;import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.TabWidget;
import android.widget.TextView;public class CircleBadgeView extends TextView{public static final int POSITION_TOP_LEFT = 1;public static final int POSITION_TOP_RIGHT = 2;public static final int POSITION_BOTTOM_LEFT = 3;public static final int POSITION_BOTTOM_RIGHT = 4;private static final int DEFAULT_MARGIN_DIP = 5;private static final int DEFAULT_LR_PADDING_DIP = 5;private static final int DEFAULT_CORNER_RADIUS_DIP = 8;private static final int DEFAULT_POSITION = POSITION_TOP_RIGHT;private static final int DEFAULT_BADGE_COLOR = Color.RED;private static final int DEFAULT_TEXT_COLOR = Color.WHITE;private static Animation fadeIn;private static Animation fadeOut;private Context context;private View target;private int badgePosition;private int badgeMargin;private int badgeColor;private boolean isShown;//private ShapeDrawable badgeBg;private int targetTabIndex;private Paint mBgPaint = new Paint();PaintFlagsDrawFilter pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);public CircleBadgeView(Context context) {this(context, (AttributeSet) null, android.R.attr.textViewStyle);mBgPaint.setColor(Color.WHITE);mBgPaint.setAntiAlias(true);}public CircleBadgeView(Context context, AttributeSet attrs) {this(context, attrs, android.R.attr.textViewStyle);mBgPaint.setColor(Color.WHITE);mBgPaint.setAntiAlias(true);}/*** Constructor -* * create a new BadgeView instance attached to a target {@link android.view.View}.** @param context context for this view.* @param target the View to attach the badge to.*/public CircleBadgeView(Context context, View target) {this(context, null, android.R.attr.textViewStyle, target, 0);}/*** Constructor -* * create a new BadgeView instance attached to a target {@link android.widget.TabWidget}* tab at a given index.** @param context context for this view.* @param target the TabWidget to attach the badge to.* @param index the position of the tab within the target.*/public CircleBadgeView(Context context, TabWidget target, int index) {this(context, null, android.R.attr.textViewStyle, target, index);}public CircleBadgeView(Context context, AttributeSet attrs, int defStyle) {this(context, attrs, defStyle, null, 0);}public CircleBadgeView(Context context, AttributeSet attrs, int defStyle, View target, int tabIndex) {super(context, attrs, defStyle);init(context, target, tabIndex);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper.onMeasure(widthMeasureSpec, heightMeasureSpec);int measuredWidth = getMeasuredWidth();int measuredHeight = getMeasuredHeight();int max = Math.max(measuredWidth, measuredHeight);setMeasuredDimension(max, max);}@Overridepublic void setBackgroundColor(int color) {// TODO Auto-generated method stub//super.setBackgroundColor(Color.RED);mBgPaint.setColor(color);}@Overridepublic void draw(Canvas canvas) {// TODO Auto-generated method stubcanvas.setDrawFilter(pfd);canvas.drawCircle(getWidth()/2, getHeight()/2, Math.max(getWidth(), getHeight())/2, mBgPaint);super.draw(canvas);}private void init(Context context, View target, int tabIndex) {this.context = context;this.target = target;this.targetTabIndex = tabIndex;// apply defaultsbadgePosition = DEFAULT_POSITION;badgeMargin = dipToPixels(DEFAULT_MARGIN_DIP);badgeColor = DEFAULT_BADGE_COLOR;setTypeface(Typeface.DEFAULT_BOLD);int paddingPixels = dipToPixels(DEFAULT_LR_PADDING_DIP);setPadding(paddingPixels, 0, paddingPixels, 0);setTextColor(DEFAULT_TEXT_COLOR);fadeIn = new AlphaAnimation(0, 1);fadeIn.setInterpolator(new DecelerateInterpolator());fadeIn.setDuration(200);fadeOut = new AlphaAnimation(1, 0);fadeOut.setInterpolator(new AccelerateInterpolator());fadeOut.setDuration(200);isShown = false;if (this.target != null) {applyTo(this.target);} else {show();}}private void applyTo(View target) {LayoutParams lp = target.getLayoutParams();ViewParent parent = target.getParent();FrameLayout container = new FrameLayout(context);if (target instanceof TabWidget) {// set target to the relevant tab child containertarget = ((TabWidget) target).getChildTabViewAt(targetTabIndex);this.target = target;((ViewGroup) target).addView(container, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));this.setVisibility(View.GONE);container.addView(this);} else {// TODO verify that parent is indeed a ViewGroupViewGroup group = (ViewGroup) parent; int index = group.indexOfChild(target);group.removeView(target);group.addView(container, index, lp);container.addView(target);this.setVisibility(View.GONE);container.addView(this);group.invalidate();}}/*** Make the badge visible in the UI.* */public void show() {show(false, null);}/*** Make the badge visible in the UI.** @param animate flag to apply the default fade-in animation.*/public void show(boolean animate) {show(animate, fadeIn);}/*** Make the badge visible in the UI.** @param anim Animation to apply to the view when made visible.*/public void show(Animation anim) {show(true, anim);}/*** Make the badge non-visible in the UI.* */public void hide() {hide(false, null);}/*** Make the badge non-visible in the UI.** @param animate flag to apply the default fade-out animation.*/public void hide(boolean animate) {hide(animate, fadeOut);}/*** Make the badge non-visible in the UI.** @param anim Animation to apply to the view when made non-visible.*/public void hide(Animation anim) {hide(true, anim);}/*** Toggle the badge visibility in the UI.* */public void toggle() {toggle(false, null, null);}/*** Toggle the badge visibility in the UI.* * @param animate flag to apply the default fade-in/out animation.*/public void toggle(boolean animate) {toggle(animate, fadeIn, fadeOut);}/*** Toggle the badge visibility in the UI.** @param animIn Animation to apply to the view when made visible.* @param animOut Animation to apply to the view when made non-visible.*/public void toggle(Animation animIn, Animation animOut) {toggle(true, animIn, animOut);}private void show(boolean animate, Animation anim) {if (getBackground() == null) {
//          if (badgeBg == null) {
//              badgeBg = getDefaultBackground();
//          }
//          setBackgroundDrawable(badgeBg);}applyLayoutParams();if (animate) {this.startAnimation(anim);}this.setVisibility(View.VISIBLE);isShown = true;}private void hide(boolean animate, Animation anim) {this.setVisibility(View.GONE);if (animate) {this.startAnimation(anim);}isShown = false;}private void toggle(boolean animate, Animation animIn, Animation animOut) {if (isShown) {hide(animate && (animOut != null), animOut);  } else {show(animate && (animIn != null), animIn);}}/*** Increment the numeric badge label. If the current badge label cannot be converted to* an integer value, its label will be set to "0".* * @param offset the increment offset.*/public int increment(int offset) {CharSequence txt = getText();int i;if (txt != null) {try {i = Integer.parseInt(txt.toString());} catch (NumberFormatException e) {i = 0;}} else {i = 0;}i = i + offset;setText(String.valueOf(i));return i;}/*** Decrement the numeric badge label. If the current badge label cannot be converted to* an integer value, its label will be set to "0".* * @param offset the decrement offset.*/public int decrement(int offset) {return increment(-offset);}private ShapeDrawable getDefaultBackground() {int r = dipToPixels(DEFAULT_CORNER_RADIUS_DIP);float[] outerR = new float[] {r, r, r, r, r, r, r, r};RoundRectShape rr = new RoundRectShape(outerR, null, null);OvalShape os = new OvalShape();ShapeDrawable drawable = new ShapeDrawable(rr);drawable.getPaint().setColor(badgeColor);return drawable;}private void applyLayoutParams() {FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);switch (badgePosition) {case POSITION_TOP_LEFT:lp.gravity = Gravity.LEFT | Gravity.TOP;lp.setMargins(badgeMargin, badgeMargin, 0, 0);break;case POSITION_TOP_RIGHT:lp.gravity = Gravity.RIGHT | Gravity.TOP;lp.setMargins(0, badgeMargin, badgeMargin, 0);break;case POSITION_BOTTOM_LEFT:lp.gravity = Gravity.LEFT | Gravity.BOTTOM;lp.setMargins(badgeMargin, 0, 0, badgeMargin);break;case POSITION_BOTTOM_RIGHT:lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;lp.setMargins(0, 0, badgeMargin, badgeMargin);break;default:break;}setLayoutParams(lp);}/*** Returns the target View this badge has been attached to.* */public View getTarget() {return target;}/*** Is this badge currently visible in the UI?* */@Overridepublic boolean isShown() {return isShown;}/*** Returns the positioning of this badge.* * one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT.* */public int getBadgePosition() {return badgePosition;}/*** Set the positioning of this badge.* * @param layoutPosition one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT.* */public void setBadgePosition(int layoutPosition) {this.badgePosition = layoutPosition;}/*** Returns the horizontal/vertical margin from the target View that is applied to this badge.* */public int getBadgeMargin() {return badgeMargin;}/*** Set the horizontal/vertical margin from the target View that is applied to this badge.* * @param badgeMargin the margin in pixels.*/public void setBadgeMargin(int badgeMargin) {this.badgeMargin = badgeMargin;}/*** Returns the color value of the badge background.* */public int getBadgeBackgroundColor() {return badgeColor;}/*** Set the color value of the badge background.* * @param badgeColor the badge background color.*/public void setBadgeBackgroundColor(int badgeColor) {this.badgeColor = badgeColor;//badgeBg = getDefaultBackground();}private int dipToPixels(int dip) {Resources r = getResources();float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics());return (int) px;}
}

使用案例:

 private void initView() {login = (Button) findViewById(R.id.login);badge1 = new CircleBadgeView(this, login);badge1.setText("10");badge1.setBackgroundColor(Color.RED);//设置背景颜色badge1.setTextSize(15);//设置文字大小badge1.setGravity(Gravity.CENTER);//设置文字居中badge1.show();}

闲时整理(5)--圆形标签相关推荐

  1. HTML笔记整理2 -- HTML标签

    HTML笔记整理2 -- HTML标签 HTML注释 关于标签(标记) 标记的分类 标记的属性 重要的标记 ``标签 ``标签 meta标签中的 --- 字符编码问题 常用标记 常用标记-1---We ...

  2. 百度地图鼠标经过图层时高亮显示图标及标签内容

    百度地图开发常用网站 1.百度地图开放平台 http://lbsyun.baidu.com/ 2.百度地图 Javascript API JavaScript API v3.0 http://lbsy ...

  3. angularjs2 html转义,详解Angular.js数据绑定时自动转义html标签及内容

    angularJS在进行数据绑定时默认是以字符串的形式数据,也就是对你数据中的html标签不进行转义照单全收,这样提高了安全性,防止html标签的注入攻击,但有时候需要,特别是从数据库读取带格式的文本 ...

  4. Python使用matplotlib保存图像时发生自动裁剪丢了部分标签信息解决方案(plt.savefig保存时丢失了部分标签字符)

    Python使用matplotlib保存图像时发生自动裁剪丢了部分标签信息解决方案(plt.savefig保存时丢失了部分标签字符) 目录

  5. java——自己实现基础的线程池及带有任务数过多拒绝策略、线程池销毁、自动扩充线程数量及闲时自动回收线程等操作的改进版线程池

    1. 实现第一版基础的线程池 1.1 首先我们定义一个线程池类ThreadPool,然后线程池有一个容器存放我们创建的线程,另一个容器则是存放当前线程池需要处理的任务队列,线程容器用ArrayList ...

  6. html中dfn标签,怎么时用html dfn标签

    怎么时用html dfn标签 发布时间:2020-07-10 13:53:57 来源:亿速云 阅读:116 作者:Leah 本篇文章给大家分享的是有关怎么时用html dfn标签,小编觉得挺实用的,因 ...

  7. golang for嵌套循环中break 的注意事项和使用细节: break 语句出现在多层嵌套的语句块中时,可以通过标签指明要终止的是哪一层语句块

    break 的注意事项和使用细节 break 语句出现在多层嵌套的语句块中时,可以通过标签指明要终止的是哪一层语句块 看一个案例 对上面案例的说明 (1) break 默认会跳出最近的 for 循环 ...

  8. 尚硅谷周阳老师2020年 SpringCloud(H版和Alibaba) 视频教程学习时整理的笔记记录和代码

    尚硅谷周阳老师2020年 SpringCloud(H版和Alibaba)视频教程学习时整理的笔记记录和代码 尚硅谷周阳老师SpringCloud(H版和Alibaba)学习.代码摘录,下面是各个mod ...

  9. [HTML]关于html禁止图片拖动,以及禁止拖动图片时打开新的标签页

    [HTML]关于html禁止图片拖动,以及禁止拖动图片时打开新的标签页. //禁止浏览器拖动图片打开新标签页的默认事件document.ondragover = function (e) { e.pr ...

  10. 个人整理 HTML全部标签总集,及用法

    个人整理,资料来自HTML参考手册,应该是最全的的了 1.<!--...--> 定义注释 注释标签用于在源代码中插入注释.注释不会显示在浏览器中. 您可使用注释对您的代码进行解释,这样做有 ...

最新文章

  1. 一行代码实现数据类型判断
  2. 二叉树c语言程序插入某个成员,关于C ++:二叉树:插入节点算法
  3. web开发下的各种下载方法
  4. 简单计算机病毒黑屏,教大家一个黑屏小程序
  5. 鸟哥的Linux私房菜(服务器)- 架站文件習題解答篇
  6. 关于cookie 跨页面处理
  7. KBMMW 4.81.00 发布
  8. 加密方式(包括MD5 base64 对称加密 非对称加密简介)
  9. ArcGIS修改矢量边界(土地利用图图斑)
  10. 爬取百度贴吧发帖信息并保存到scv文件中
  11. 8255矩阵键盘C语言,8255驱动矩阵键盘和数码管程序
  12. 读书寄语:有一种感动叫守口如瓶
  13. ubuntu18.04安装五笔输入法
  14. 【MFiX】记录一些有参考价值的legacy-tut/test算例
  15. Cubase10.5稳定版安装包+安装教程
  16. Maven多模块打包成war+vue打包
  17. 永磁同步电机驱动器保护算法专题
  18. 《CSS实战案例汇总》涟漪
  19. 学习html的心得总结
  20. iview表格中,鼠标滑过单元格展示提示信息

热门文章

  1. 财务内部收益率用计算机怎么算,财务内部收益率计算公式
  2. PageHelper获取数据总条数
  3. 勤于奋:国外LEAD跟联盟经理沟通聊天软件,Skype注册教程
  4. 桥架算量用什么软件_鹏业安装算量软件识别桥架
  5. Windows7计算机的程序文件名,Win7怎么显示文件后缀名_Win7显示文件的扩展名-192路由网...
  6. Ionic for Angular 环境搭建
  7. 亚马逊运营实用教程 上线前三个月如何做
  8. 台湾地区HITCON队长发威夺冠,XCTF联赛西安站国际赛顺利落幕
  9. 百度 95 后程序员删库跑路被判刑,动机为工作内容变动及对领导不满,删库会给互联网公司带来哪些影响?
  10. 利用Python制作证件照