红色小圆点消息提示之BadgeView

举报
yechaoa 发表于 2022/05/30 22:22:23 2022/05/30
【摘要】 在app中随处可见具有中国红特色的小圆点,比如电商项目的购物车、通讯项目的消息列表等等。 第三方控件BadgeView可以简单粗暴实现。 效果图: 1.布局 2.初始化布局 textView1 = (TextView) findViewById(R.id.textView1);textView2 = (TextV...

在app中随处可见具有中国红特色的小圆点,比如电商项目的购物车、通讯项目的消息列表等等。

第三方控件BadgeView可以简单粗暴实现。

效果图:


1.布局


2.初始化布局


  
  1. textView1 = (TextView) findViewById(R.id.textView1);
  2. textView2 = (TextView) findViewById(R.id.textView2);
  3. imageView = (ImageView) findViewById(R.id.imageView);


  
  1. //初始化BadgeView,1.上下文,2.显示对象
  2. final BadgeView badgeTextView1 = new BadgeView(this, textView1);
  3. BadgeView badgeTextView2 = new BadgeView(this, textView2);
  4. BadgeView badgeImageView = new BadgeView(this, imageView);



4.设置BadgeView属性


  
  1. badgeTextView1.setBadgeBackgroundColor(Color.RED);//设置背景颜色
  2. badgeTextView1.setBadgeMargin(5);//设置margin
  3. badgeTextView1.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);//设置显示的位置

5.设置显示内容


  
  1. count = 1;
  2. if (count > 0) {
  3. if (count < 99) {
  4. badgeTextView1.setText(count + "");//设置显示的内容
  5. } else {
  6. badgeTextView1.setText("99+");
  7. }
  8. badgeTextView1.setTextSize(10);//设置字体大小
  9. badgeTextView1.setTextColor(Color.WHITE);//设置字体颜色
  10. badgeTextView1.show();//显示
  11. } else {
  12. badgeTextView1.hide();//隐藏
  13. }

6.设置点击(强迫症的必须点击消失。。。)


  
  1. //设置点击
  2. badgeTextView1.setOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View v) {
  5. // 1
  6. // 设置进入的移动动画,设置了插值器,可以实现颤动的效果
  7. TranslateAnimation anim1 = new TranslateAnimation(0, 0, 0, 0);
  8. anim1.setInterpolator(new BounceInterpolator());
  9. // 设置动画的持续时间
  10. anim1.setDuration(500);
  11. // 设置退出的移动动画
  12. TranslateAnimation anim2 = new TranslateAnimation(0, 0, 0, 0);
  13. anim2.setDuration(500);
  14. badgeTextView1.toggle(anim1, anim2);
  15. // 2
  16. //badgeTextView1.toggle(true);
  17. // 3
  18. //badgeTextView1.hide();
  19. }
  20. });

7.BadgeView

可copy直接用,再加一个attrs.xml文件


  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="CircleImageView">
  4. <attr name="border_width" format="dimension"/>
  5. <attr name="border_color" format="color"/>
  6. </declare-styleable>
  7. </resources>


  
  1. package com.yechaoa.badgeviewdemo;
  2. import android.content.Context;
  3. import android.content.res.Resources;
  4. import android.graphics.Color;
  5. import android.graphics.Typeface;
  6. import android.graphics.drawable.ShapeDrawable;
  7. import android.graphics.drawable.shapes.RoundRectShape;
  8. import android.util.AttributeSet;
  9. import android.util.TypedValue;
  10. import android.view.Gravity;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.view.ViewGroup.LayoutParams;
  14. import android.view.ViewParent;
  15. import android.view.animation.AccelerateInterpolator;
  16. import android.view.animation.AlphaAnimation;
  17. import android.view.animation.Animation;
  18. import android.view.animation.DecelerateInterpolator;
  19. import android.widget.FrameLayout;
  20. import android.widget.TabWidget;
  21. import android.widget.TextView;
  22. /**
  23. * A simple text label view that can be applied as a "badge" to any given {@link View}.
  24. * This class is intended to be instantiated at runtime rather than included in XML layouts.
  25. *
  26. * @author Jeff Gilfelt
  27. */
  28. public class BadgeView extends TextView {
  29. public static final int POSITION_TOP_LEFT = 1;
  30. public static final int POSITION_TOP_RIGHT = 2;
  31. public static final int POSITION_BOTTOM_LEFT = 3;
  32. public static final int POSITION_BOTTOM_RIGHT = 4;
  33. public static final int POSITION_CENTER = 5;
  34. private static final int DEFAULT_MARGIN_DIP = 5;
  35. private static final int DEFAULT_LR_PADDING_DIP = 5;
  36. private static final int DEFAULT_CORNER_RADIUS_DIP = 8;
  37. private static final int DEFAULT_POSITION = POSITION_TOP_RIGHT;
  38. private static final int DEFAULT_BADGE_COLOR = Color.parseColor("#CCFF0000"); //Color.RED;
  39. private static final int DEFAULT_TEXT_COLOR = Color.WHITE;
  40. private static Animation fadeIn;
  41. private static Animation fadeOut;
  42. private Context context;
  43. private View target;
  44. private int badgePosition;
  45. private int badgeMarginH;
  46. private int badgeMarginV;
  47. private int badgeColor;
  48. private boolean isShown;
  49. private ShapeDrawable badgeBg;
  50. private int targetTabIndex;
  51. public BadgeView(Context context) {
  52. this(context, (AttributeSet) null, android.R.attr.textViewStyle);
  53. }
  54. public BadgeView(Context context, AttributeSet attrs) {
  55. this(context, attrs, android.R.attr.textViewStyle);
  56. }
  57. /**
  58. * Constructor -
  59. *
  60. * create a new BadgeView instance attached to a target {@link View}.
  61. *
  62. * @param context context for this view.
  63. * @param target the View to attach the badge to.
  64. */
  65. public BadgeView(Context context, View target) {
  66. this(context, null, android.R.attr.textViewStyle, target, 0);
  67. }
  68. /**
  69. * Constructor -
  70. *
  71. * create a new BadgeView instance attached to a target {@link TabWidget}
  72. * tab at a given index.
  73. *
  74. * @param context context for this view.
  75. * @param target the TabWidget to attach the badge to.
  76. * @param index the position of the tab within the target.
  77. */
  78. public BadgeView(Context context, TabWidget target, int index) {
  79. this(context, null, android.R.attr.textViewStyle, target, index);
  80. }
  81. public BadgeView(Context context, AttributeSet attrs, int defStyle) {
  82. this(context, attrs, defStyle, null, 0);
  83. }
  84. public BadgeView(Context context, AttributeSet attrs, int defStyle, View target, int tabIndex) {
  85. super(context, attrs, defStyle);
  86. init(context, target, tabIndex);
  87. }
  88. private void init(Context context, View target, int tabIndex) {
  89. this.context = context;
  90. this.target = target;
  91. this.targetTabIndex = tabIndex;
  92. // apply defaults
  93. badgePosition = DEFAULT_POSITION;
  94. badgeMarginH = dipToPixels(DEFAULT_MARGIN_DIP);
  95. badgeMarginV = badgeMarginH;
  96. badgeColor = DEFAULT_BADGE_COLOR;
  97. setTypeface(Typeface.DEFAULT_BOLD);
  98. int paddingPixels = dipToPixels(DEFAULT_LR_PADDING_DIP);
  99. setPadding(paddingPixels, 0, paddingPixels, 0);
  100. setTextColor(DEFAULT_TEXT_COLOR);
  101. fadeIn = new AlphaAnimation(0, 1);
  102. fadeIn.setInterpolator(new DecelerateInterpolator());
  103. fadeIn.setDuration(200);
  104. fadeOut = new AlphaAnimation(1, 0);
  105. fadeOut.setInterpolator(new AccelerateInterpolator());
  106. fadeOut.setDuration(200);
  107. isShown = false;
  108. if (this.target != null) {
  109. applyTo(this.target);
  110. } else {
  111. show();
  112. }
  113. }
  114. private void applyTo(View target) {
  115. LayoutParams lp = target.getLayoutParams();
  116. ViewParent parent = target.getParent();
  117. FrameLayout container = new FrameLayout(context);
  118. if (target instanceof TabWidget) {
  119. // set target to the relevant tab child container
  120. target = ((TabWidget) target).getChildTabViewAt(targetTabIndex);
  121. this.target = target;
  122. ((ViewGroup) target).addView(container,
  123. new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  124. this.setVisibility(View.GONE);
  125. container.addView(this);
  126. } else {
  127. // TODO verify that parent is indeed a ViewGroup
  128. ViewGroup group = (ViewGroup) parent;
  129. int index = group.indexOfChild(target);
  130. group.removeView(target);
  131. group.addView(container, index, lp);
  132. container.addView(target);
  133. this.setVisibility(View.GONE);
  134. container.addView(this);
  135. group.invalidate();
  136. }
  137. }
  138. /**
  139. * Make the badge visible in the UI.
  140. *
  141. */
  142. public void show() {
  143. show(false, null);
  144. }
  145. /**
  146. * Make the badge visible in the UI.
  147. *
  148. * @param animate flag to apply the default fade-in animation.
  149. */
  150. public void show(boolean animate) {
  151. show(animate, fadeIn);
  152. }
  153. /**
  154. * Make the badge visible in the UI.
  155. *
  156. * @param anim Animation to apply to the view when made visible.
  157. */
  158. public void show(Animation anim) {
  159. show(true, anim);
  160. }
  161. /**
  162. * Make the badge non-visible in the UI.
  163. *
  164. */
  165. public void hide() {
  166. hide(false, null);
  167. }
  168. /**
  169. * Make the badge non-visible in the UI.
  170. *
  171. * @param animate flag to apply the default fade-out animation.
  172. */
  173. public void hide(boolean animate) {
  174. hide(animate, fadeOut);
  175. }
  176. /**
  177. * Make the badge non-visible in the UI.
  178. *
  179. * @param anim Animation to apply to the view when made non-visible.
  180. */
  181. public void hide(Animation anim) {
  182. hide(true, anim);
  183. }
  184. /**
  185. * Toggle the badge visibility in the UI.
  186. *
  187. */
  188. public void toggle() {
  189. toggle(false, null, null);
  190. }
  191. /**
  192. * Toggle the badge visibility in the UI.
  193. *
  194. * @param animate flag to apply the default fade-in/out animation.
  195. */
  196. public void toggle(boolean animate) {
  197. toggle(animate, fadeIn, fadeOut);
  198. }
  199. /**
  200. * Toggle the badge visibility in the UI.
  201. *
  202. * @param animIn Animation to apply to the view when made visible.
  203. * @param animOut Animation to apply to the view when made non-visible.
  204. */
  205. public void toggle(Animation animIn, Animation animOut) {
  206. toggle(true, animIn, animOut);
  207. }
  208. private void show(boolean animate, Animation anim) {
  209. if (getBackground() == null) {
  210. if (badgeBg == null) {
  211. badgeBg = getDefaultBackground();
  212. }
  213. setBackgroundDrawable(badgeBg);
  214. }
  215. applyLayoutParams();
  216. if (animate) {
  217. this.startAnimation(anim);
  218. }
  219. this.setVisibility(View.VISIBLE);
  220. isShown = true;
  221. }
  222. private void hide(boolean animate, Animation anim) {
  223. this.setVisibility(View.GONE);
  224. if (animate) {
  225. this.startAnimation(anim);
  226. }
  227. isShown = false;
  228. }
  229. private void toggle(boolean animate, Animation animIn, Animation animOut) {
  230. if (isShown) {
  231. hide(animate && (animOut != null), animOut);
  232. } else {
  233. show(animate && (animIn != null), animIn);
  234. }
  235. }
  236. /**
  237. * Increment the numeric badge label. If the current badge label cannot be converted to
  238. * an integer value, its label will be set to "0".
  239. *
  240. * @param offset the increment offset.
  241. */
  242. public int increment(int offset) {
  243. CharSequence txt = getText();
  244. int i;
  245. if (txt != null) {
  246. try {
  247. i = Integer.parseInt(txt.toString());
  248. } catch (NumberFormatException e) {
  249. i = 0;
  250. }
  251. } else {
  252. i = 0;
  253. }
  254. i = i + offset;
  255. setText(String.valueOf(i));
  256. return i;
  257. }
  258. /**
  259. * Decrement the numeric badge label. If the current badge label cannot be converted to
  260. * an integer value, its label will be set to "0".
  261. *
  262. * @param offset the decrement offset.
  263. */
  264. public int decrement(int offset) {
  265. return increment(-offset);
  266. }
  267. private ShapeDrawable getDefaultBackground() {
  268. int r = dipToPixels(DEFAULT_CORNER_RADIUS_DIP);
  269. float[] outerR = new float[] {r, r, r, r, r, r, r, r};
  270. RoundRectShape rr = new RoundRectShape(outerR, null, null);
  271. ShapeDrawable drawable = new ShapeDrawable(rr);
  272. drawable.getPaint().setColor(badgeColor);
  273. return drawable;
  274. }
  275. private void applyLayoutParams() {
  276. FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  277. switch (badgePosition) {
  278. case POSITION_TOP_LEFT:
  279. lp.gravity = Gravity.LEFT | Gravity.TOP;
  280. lp.setMargins(badgeMarginH, badgeMarginV, 0, 0);
  281. break;
  282. case POSITION_TOP_RIGHT:
  283. lp.gravity = Gravity.RIGHT | Gravity.TOP;
  284. lp.setMargins(0, badgeMarginV, badgeMarginH, 0);
  285. break;
  286. case POSITION_BOTTOM_LEFT:
  287. lp.gravity = Gravity.LEFT | Gravity.BOTTOM;
  288. lp.setMargins(badgeMarginH, 0, 0, badgeMarginV);
  289. break;
  290. case POSITION_BOTTOM_RIGHT:
  291. lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
  292. lp.setMargins(0, 0, badgeMarginH, badgeMarginV);
  293. break;
  294. case POSITION_CENTER:
  295. lp.gravity = Gravity.CENTER;
  296. lp.setMargins(0, 0, 0, 0);
  297. break;
  298. default:
  299. break;
  300. }
  301. setLayoutParams(lp);
  302. }
  303. /**
  304. * Returns the target View this badge has been attached to.
  305. *
  306. */
  307. public View getTarget() {
  308. return target;
  309. }
  310. /**
  311. * Is this badge currently visible in the UI?
  312. *
  313. */
  314. @Override
  315. public boolean isShown() {
  316. return isShown;
  317. }
  318. /**
  319. * Returns the positioning of this badge.
  320. *
  321. * one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT, POSTION_CENTER.
  322. *
  323. */
  324. public int getBadgePosition() {
  325. return badgePosition;
  326. }
  327. /**
  328. * Set the positioning of this badge.
  329. *
  330. * @param layoutPosition one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT, POSTION_CENTER.
  331. *
  332. */
  333. public void setBadgePosition(int layoutPosition) {
  334. this.badgePosition = layoutPosition;
  335. }
  336. /**
  337. * Returns the horizontal margin from the target View that is applied to this badge.
  338. *
  339. */
  340. public int getHorizontalBadgeMargin() {
  341. return badgeMarginH;
  342. }
  343. /**
  344. * Returns the vertical margin from the target View that is applied to this badge.
  345. *
  346. */
  347. public int getVerticalBadgeMargin() {
  348. return badgeMarginV;
  349. }
  350. /**
  351. * Set the horizontal/vertical margin from the target View that is applied to this badge.
  352. *
  353. * @param badgeMargin the margin in pixels.
  354. */
  355. public void setBadgeMargin(int badgeMargin) {
  356. this.badgeMarginH = badgeMargin;
  357. this.badgeMarginV = badgeMargin;
  358. }
  359. /**
  360. * Set the horizontal/vertical margin from the target View that is applied to this badge.
  361. *
  362. * @param horizontal margin in pixels.
  363. * @param vertical margin in pixels.
  364. */
  365. public void setBadgeMargin(int horizontal, int vertical) {
  366. this.badgeMarginH = horizontal;
  367. this.badgeMarginV = vertical;
  368. }
  369. /**
  370. * Returns the color value of the badge background.
  371. *
  372. */
  373. public int getBadgeBackgroundColor() {
  374. return badgeColor;
  375. }
  376. /**
  377. * Set the color value of the badge background.
  378. *
  379. * @param badgeColor the badge background color.
  380. */
  381. public void setBadgeBackgroundColor(int badgeColor) {
  382. this.badgeColor = badgeColor;
  383. badgeBg = getDefaultBackground();
  384. }
  385. private int dipToPixels(int dip) {
  386. Resources r = getResources();
  387. float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics());
  388. return (int) px;
  389. }
  390. }


Demo下载:https://github.com/yechaoa/BadgeViewDemo


BadgeView下载:https://github.com/stefanjauker/BadgeView



文章来源: blog.csdn.net,作者:yechaoa,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/yechaoa/article/details/53236418

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。