Android If the custom bitmap is drawn and then returned to ImageView

Source: Internet
Author: User

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

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.