android强迫症头像BadgeView

绝地灬酷狼 2022-08-03 08:45 224阅读 0赞

先看图片吧,需要用到的自己下载Demo,不需要的点右上角。

大多数的红点就是这两种,BadgeView是一个类文件,没有打成jar包,方便各位拓展。

点我下载Demo

Center

第一种效果与第二种效果。

先复制 BadgeView类到你的包下,然后在需要使用的地方 new 出来,并设置相关属性。

  1. BadgeView maintabbadgeView = new BadgeView(getApplicationContext(),
  2. tvBadgeView);
  3. maintabbadgeView.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
  4. maintabbadgeView.setTextSize(1);
  5. maintabbadgeView.setBackgroundResource(R.drawable.notificaitonpoint);
  6. maintabbadgeView.show();
  7. BadgeView maintabbadgeView1 = new BadgeView(getApplicationContext(),
  8. tvBadgeView1);
  9. maintabbadgeView1.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
  10. maintabbadgeView1.setText("4");
  11. maintabbadgeView1.show();

BadgeView类:

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

发表评论

表情:
评论列表 (有 0 条评论,224人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android圆形头像实现

    ![这里写图片描述][SouthEast] 如图所示,实现一个QQ圆形头像,话不多说,直接上代码,继承ImageView类,重写里面的方法: package com