Add a reminder above the Android icon (a) draw with canvas

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

When we develop some applications such as address book, social and other applications or add new function modules, we will consider adding the number of unread information to the corresponding icon, or the icon of the new function reminder, so that it does not occupy too much space and can achieve the purpose of prompting.

In the MMS analysis section, I was also asked about the implementation of this reminder. The addition of this reminder, I summed up, there are probably several ways to achieve:

1. Place the TextView or ImageView in the corresponding layout.

2. Use canvas to draw on the original icon's bitmap base

3. Easy to add with open source project Viewbadger, and relatively flexible

The first method is simply to adjust the layout below. Not here anymore, just look at the second method:

Here is a custom function to get the picture based on the resource icon ID, which is very simple, the code is as follows:

[Java]View Plaincopy print?
  1. /**
  2. * Get a picture by ID
  3. * @param Res
  4. * @param resId
  5. * @return
  6. */
  7. private Bitmap Getresicon (Resources res,int resId) {
  8. Drawable icon=res.getdrawable (resId);
  9. if (icon instanceof bitmapdrawable) {
  10. Bitmapdrawable bd= (bitmapdrawable) icon;
  11. return Bd.getbitmap ();
  12. }else{
  13. return null;
  14. }
  15. }
After you get to the icon, you have to process the icon, adding a number of overrides to the icon

[Java]View Plaincopy print?
  1. /**
  2. * Add quantity to the upper right corner of the given picture
  3. * @param image given by icon
  4. */
  5. Private Bitmap Generatorcontactcounticon (Bitmap icon) {
  6. //Initialize canvas
  7. int iconsize= (int) getresources (). Getdimension (Android.    R.dimen.app_icon_size);
  8. LOG.D (TAG, "The icon size is" +iconsize);
  9. Bitmap Contacticon=bitmap.createbitmap (iconsize, Iconsize, config.argb_8888);
  10. Canvas canvas=new canvas (Contacticon);
  11. //Copy Picture
  12. Paint iconpaint=new paint ();
  13. Iconpaint.setdither (true); Anti-jitter
  14. Iconpaint.setfilterbitmap (true); It is used to filter the bitmap so that when you choose Drawable, there is an anti-aliasing effect.
  15. Rect src=New rect (0, 0, Icon.getwidth (), Icon.getheight ());
  16. Rect dst=New rect (0, 0, Iconsize, iconsize);
  17. Canvas.drawbitmap (icon, SRC, DST, iconpaint);
  18. //enable antialiasing and use device text kerning
  19. Paint countpaint=New paint (paint.anti_alias_flag|    Paint.dev_kern_text_flag);
  20. Countpaint.setcolor (color.red);
  21. Countpaint.settextsize (20f);
  22. Countpaint.settypeface (Typeface.default_bold);
  23. Canvas.drawtext (String.valueof (count), iconsize-, countpaint);
  24. return Contacticon;
  25. }
if reproduced, please declare the source: The Sands of Time: http://blog.csdn.net/t12x3456

Add a reminder above the Android icon (a) draw with canvas

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.