When developing applications such as short messages and app stores, we will consider adding the number of unread text messages to the icon of the short message, and adding the number of apps that can be upgraded to the app store, in this way, the goal of prompting can be achieved without occupying too much space.
This section uses an example of showing the number of contacts in a mobile phone to show how to add a number to an icon, that is, a number overlay .. I have done Google map development and know about overlay.
I. You can learn
- View contacts based on Uris on your phone
- Add Permissions
- Retrieve the app icons of any app on your phone
- Image processing, such as copying, adds a layer-number to the image.
- Use remoteview to customize notification
Ii. Start action
- Create an android project named icationicationiconcount. Project with final version included in the attachment
- First, modify androidmanifest. xml and add the permission <uses-Permission Android: Name = "android. Permission. read_contacts"> </uses-Permission> because we want to read the contact.
- Modify main. XML as follows. Here we define an imageview. We have the courage to preview the processed icon with numbers.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="center" android:adjustViewBounds="true"/> </LinearLayout>
4. First, retrieve the address book icon in the mobile phone. If not, use the icon of the app.
Super. oncreate (savedinstancestate); setcontentview (R. layout. main); mimageview = (imageview) findviewbyid (R. id. icon); // The contacts icon is used first. If it does not exist, the application icon drawable contacticon is used. Try {contacticon = getpackagemanager (). getapplicationicon ("com. android. contacts ");} catch (namenotfoundexception e) {contacticon = NULL;} bitmap icon; If (contacticon instanceof bitmapdrawable) {icon = (bitmapdrawable) contacticon ). getbitmap ();} else {icon = getresicon (getresources (), R. id. icon );}
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 ;}}
5.
After obtaining the icon, you need to process it. to overwrite the number of contacts for the icon, first let's look at the function for getting the number of contacts.
/*** Get the number of contacts * @ return the number of contacts in the address book */private int getcontactcount () {cursor c = getcontentresolver (). query (contactscontract. contacts. content_uri, new string [] {contactscontract. contacts. _ count}, null); try {C. movetofirst (); Return C. getint (0);} catch (exception e) {return 0;} finally {C. close ();}}
Here we use the URI method to obtain the contact's cursor and then obtain the number.
6. the icon with the number of contacts and the number of contacts can be generated. Let's look at the generated function.
/*** Add the number of contacts in the upper-right corner of the specified image. The number is represented in red * @ Param icon. The specified image * @ return contains the number of contacts */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); // create an overwrite Contact Count int contacycount = getcontactcount () on the image (); // enable anti-aliasing and use the device's text offset 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 (contacycount), iconSize-18, 25, countpaint); Return contacticon ;}
The comments are very detailed and will not be explained. It is nothing more than defining a canvas, and then drawing icons and numbers on it.
7. Then we put the processed bitmap in the imageview defined in main. XML to see the effect.
Mimageview. setimagebitmap (contactcounticon );
8. Start the application to see the effect.
We can see that red 1 in the upper right corner indicates that there is a contact in my mobile phone.
Http://blog.csdn.net/listening_music/article/details/7172438