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
- /**
- * 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
- /**
- * 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); //used to filter the bitmap so that when you select 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;
- }