Let's start with the FontMetrics class, which is a class about character attributes and measurements.
Graphs can be used to more precisely understand the meaning of each attribute:
We have a imageview in layout that we can pass:
<span style= "White-space:pre" ></span>imageview item_image = (ImageView) Findviewbyid (R.id.item_image); Item_image.setimagebitmap (Makemessagecentericon ());
This way to fill the bitmap into the ImageView.
Here's an effect, especially for the icon with a message notification:
The code can be written like this:
Protected Bitmap Makemessagecentericon () {int mmsgcnt = 4; Bitmap icon = Bitmapfactory.decoderesource (Getresources (), R.drawable.desktop_messageicon), if (icon = = null) {return icon;} Make Notice iconif (mmsgcnt > 0) {Bitmap Noticeicon = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.NOTICE_COUNT_BG); if (Noticeicon! = null) {int h = noticeicon.getheight (); int w = Noticeicon.getwidth (); Bitmap noticeiconbmp = Bitmap.createbitmap (W, H, config.argb_8888)//noticeiconbmp is the stooped bitmapcanvas that is ultimately needed = new Canvas (noticeiconbmp); Paint Noticecntpaint = new paint (); Noticecntpaint.setantialias (true); Noticecanvas.drawbitmap (Noticeicon, 0, 0, Noticecntpaint); noticecntpaint.settextalign (Align.center); Noticecntpaint.setcolor (GetResources (). GetColor ( R.drawable.white)); FontMetrics FontMetrics = Noticecntpaint.getfontmetrics (); float x = noticeicon.getwidth ()/2;float y = (noticeicon.getheight ()-fontmetrics.ascent)/2-2; FontMetrics is the highest position of the character above the baseline Noticecanvas.drawtext (String.vaLueof (MMSGCNT), x, Y, noticecntpaint); On the canvas (x, y), write Mmsgcnt//draw notice icon onto message Iconh = Icon.getheight (); w = Icon.getwidth (); Bitmap iconbmp = Bitmap.createbitmap (W, H, config.argb_8888); Canvas canvas = new canvas (iconbmp); Paint paint = new paint ();p Aint.setantialias (true); Canvas.drawbitmap (icon, 0, 0, paint); float left = Icon.getwidth ()-Not Iceicon.getwidth (); Float top = 0;canvas.drawbitmap (Noticeiconbmp, left, top, paint); return iconbmp;}} return icon;}
Android If the custom bitmap is drawn and then returned to ImageView