The app icons on the Android main interface have different styles. To make the user look more unified, you have to set a unified background for the icons.
In ApplicationPackageManager. java, there is a public Drawable getDrawable (String packageName, int resid, ApplicationInfo appInfo) method used to return the application icon. Therefore, we can process the icons after parsing and put them in the cache to make them the desired effect.
// Icon: application icon idImg: id of the background view to be added
Private Drawable buildTrayForIcon (Drawable icon, int idImg ){
Resources res = this. getResources ();
BitmapDrawable bd = new BitmapDrawable (buildTrayForIcon (icon, BitmapFactory. decodeResource (res, idImg )));
BD. settargetdensity (res. getdisplaymetrics ());
Return BD;
}
Private bitmap buildtrayforicon (drawable icon, bitmap background ){
If (icon = NULL ){
Return NULL;
}
Final int backgroundwith = background. getwidth ();
Final int backgrouncHeight = background. getHeight ();
Int sourceWidth = icon. getIntrinsicWidth ();
Int sourceHeight = icon. getIntrinsicHeight ();
/**
* The work to be done here is: how to ensure that the source image will be added in the middle
* If the source image is larger than the background image, the source image cannot be drawn. So this time
* A fixed size value is given. right-left is smaller than the source image width,
* Or the bottom-top is smaller than the height of the source image to scale the source image.
*/
Int left = (backgroundWith-sourceWidth)/2;
Int top = (backgrouncHeight-sourceHeight)/2;
Int right = left + sourceWidth;
Int bottom = top + sourceHeight;
If (left <0 ){
Left = 0;
Left + = 15; // you can set these values by yourself. I am only a test version.
Right = backgroundWith-15;
}
If (top <0 ){
Top = 0;
Top + = 15;
Bottom = backgrouncHeight-15;
}
Bitmap compoundBitmap = null;
CompoundBitmap = Bitmap. createBitmap (backgroundWith, backgrouncHeight, Config. ARGB_8888 );
// Drawable d = new Drawable (compoundBitmap );
// Drawable drawable = new BitmapDrawable (compoundBitmap );
Canvas canvas = new Canvas (compoundBitmap );
Canvas. drawBitmap (background, 0, 0, null );
// Rect r = drawable. getBounds ();
// SOldBounds. set (icon. getBounds (). left ,);
// Icon. setBounds (left, top, left + width, top + height );
Icon. setBounds (left, top, right, bottom );
Icon. draw (canvas );
// Icon. setBounds (sOldBounds );
Return compoundBitmap;
}
In this way, our results can be achieved.