Add a reminder on the android icon (1) Use canvas

Source: Internet
Author: User

When we develop some applications or applications such as address book and social networking to add new function modules, we will consider adding the number of unread information to the corresponding icons or new function reminder icons, in this way, the goal of prompting can be achieved without occupying too much space.

Some people have asked me about the implementation of this kind of reminder in the MMS Analysis Section. I have summarized how to add this kind of reminder. There are several methods to achieve this:

1. Place textview or imageview in the corresponding layout.

2. Use canvas to draw on the bitmap of the original icon

3. It is convenient and flexible to add viewbadger, an open-source project.

The first method is to adjust the layout. The second method is not described here:

:

Here we use a custom function to retrieve images based on the resource icon ID. The Code is as follows:

/*** Obtain an image by ID * @ Param res * @ Param resid * @ return */private bitmap getresicon (Resources res, int resid) {drawable icon = res. getdrawable (resid); If (icon instanceof bitmapdrawable) {bitmapdrawable BD = (bitmapdrawable) icon; return BD. getbitmap () ;}else {return NULL ;}}

After obtaining the icon, you need to process it. You need to overwrite the number of the icon.

/*** Add the number in the upper-right corner of the given image * @ Param icon */private bitmap generatorcontactcounticon (Bitmap icon) {// initialize the canvas int iconsize = (INT) getresources (). getdimension (Android. r. dimen. app_icon_size); log. D (TAG, "the Icon size is" + iconsize); bitmap contacticon = bitmap. createbitmap (iconsize, iconsize, config. argb_8888); canvas = new canvas (contacticon); // copy the image paint iconpaint = new paint (); iconpaint. setdither (true); // anti-jitter iconpaint. setfilterbitmap (true); // used to filter bitmap. In this way, when you select drawable, rect src = new rect (0, 0, icon. getwidth (), icon. getheight (); rect DST = new rect (0, 0, iconsize, iconsize); canvas. drawbitmap (icon, SRC, DST, iconpaint); // enable anti-aliasing and use the text field of the device to paint countpaint = new paint (paint. anti_alias_flag | paint. dev_kern_text_flag); countpaint. setcolor (color. red); countpaint. settextsize (20f); countpaint. settypeface (typeface. default_bold); canvas. drawtext (string. valueof (count), iconSize-18, 25, countpaint); Return contacticon ;}

If any reprint is available, please declare the Source: Shi zhisha:

Http://blog.csdn.net/t12x3456

Related Article

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.