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?
- /**
- * Get a picture 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 you get to the icon, you have to process the icon, adding a number of overrides to the icon
[Java]View Plaincopy print?
- /**
- * Add quantity to the upper right corner of the given picture
- * @param image given by icon
- */
- Private Bitmap Generatorcontactcounticon (Bitmap icon) {
- //Initialize 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 canvas=new canvas (Contacticon);
- //Copy Picture
- Paint iconpaint=new paint ();
- Iconpaint.setdither (true); Anti-jitter
- Iconpaint.setfilterbitmap (true); It is used to filter the bitmap so that when you choose Drawable, there is an anti-aliasing effect.
- 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 antialiasing and use device text kerning
- 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-, countpaint);
- return Contacticon;
- }
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