Android 4.0 adds a unified background topic for all application icons on the main interface of the Launcher.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.