Android Launcher icon box-added to optimize display effect, androidlauncher
Read the code and explain it. Mainly used for image processing
PorterDuff. Mode. SRC_ATOPSRC_ATOP
The source pixel and target pixel are mixed to make the upper-layer graph display in the center to achieve the framed effect.
Package com. ferris. launchericonutils; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. paint; import android. graphics. porterDuff; import android. graphics. porterduxfermode; import android. graphics. rect; import android. OS. bundle; import android. widget. imageView; public class MainActivity extends Activity {private ImageView imageView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageView = (ImageView) findViewById (R. id. imageView1); // obtain the image Bitmap lay1 = BitmapFactory set in imageview. decodeResource (getResources (), R. drawable. icon_folder); Bitmap lay2 = BitmapFactory. decodeResource (getResources (), R. drawable. mobileqq); // obtain the image width and create the result bitmapint width = lay1.getWidth (); Bitmap resultBmp = Bitmap. createBitmap (width, width, Bitmap. config. ARGB_8888); Paint paint = new Paint (); Canvas canvas = new Canvas (resultBmp); canvas. drawBitmap (lay1, 0, 0, paint); // draw the lower layer // display int top = 0 in the center of the upper layer icon; if (lay1.getHeight ()> lay2.getHeight ()) {top = (lay1.getHeight ()-lay2.getHeight ()/2;} else {// you can determine whether the image is greater than the box, and then continue to get the value greater than the box, zooming the upper-layer image, and then performing intersection operations, the method will not write} int left = 0; if (lay1.getWidth ()> lay2.getWidth () {left = (lay1.getWidth () -lay2.getWidth ()/2;} else {// here, you can determine whether the image is greater than the box, and then continue to obtain the greater value, scale the upper-layer image, and then perform intersection, method will not be written} paint. setXfermode (new porterduxfermode (PorterDuff. mode. SRC_ATOP); // select the intersection to go to the upper-layer image canvas. drawBitmap (lay2, left, top, paint); imageView. setImageBitmap (resultBmp );}}