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