Create an Android Project named icationicationiconcount.
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 Code
<? 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>
First, retrieve the icon of the address book on your phone. If no icon is obtained, use the icon of the application.
Java code
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MImageView = (ImageView) findViewById (R. id. icon );
// Use the contact icon first. If the contact icon does not exist, use the application icon.
Drawable contactIcon;
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:
Java code
/**
* 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. to overwrite the number of contacts for the icon, first let's look at the function for getting the number of contacts.
Java code
/**
* Obtain the number of contacts.
* @ Return refers to 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.
With the icon and contact number, you can generate an icon with the number of contacts. Let's look at the generated function.
Java code
/**
* Add the number of contacts in the upper-right corner of the specified image. The quantity is marked in red.
* @ Param icon specifies the image
* @ Return refers to an image with 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 canvas = new Canvas (contactIcon );
// Copy the image
Paint iconPaint = new Paint ();
IconPaint. setDither (true); // anti-Jitter
IconPaint. setFilterBitmap (true); // It is used to filter Bitmap. In this way, when you select Drawable, it will have 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 );
// Create an overwrite number of contacts on the Image
Int contacyCount = getContactCount ();
// Enable anti-aliasing and use the text field of the device
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.
Then we put the processed Bitmap in the ImageView defined in main. xml to see the effect.
Java code
MImageView. setImageBitmap (contactCountIcon );
Let's 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.
Author "TryLoveCatch"