Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to display numbers on icons with android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces android how to achieve the icon to display numbers on the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this android how to achieve digital display on the icon articles will have a harvest, let's take a look.

Effect picture:

Dynamically display the number of the day.

The main code is as follows:

Import android.content.Context;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.Typeface;import android.graphics.drawable.BitmapDrawable / * author qhg * @ date March 5, 2014 * * / public class QNotifacationIcon {/ * add text to a given picture * * @ param context * context object * @ param resId * picture resource ID * @ param text * Text to be displayed * @ param textSize * text size * @ param color * text color * @ param typeface * text font * @ param offsetX * offset of text x * @ param offsetY * text y Offset * @ return Picture with text * / public static Bitmap generatorContactIcon (Context context Int resId, String text, float textSize, int color, Typeface typeface, float offsetX, float offsetY) {/ / get the image to be processed according to id Bitmap icon = ((BitmapDrawable) (context.getResources () .getDrawable (resId) .getBitmap () Int iconWidth = icon.getWidth (); int iconHeight = icon.getHeight (); / / initialize canvas Bitmap contactIcon = Bitmap.createBitmap (iconWidth, iconHeight, Config.ARGB_8888); / / build Canvas canvas = new Canvas (contactIcon); / / create brush Paint paint = new Paint () / / setting whether or not to use image dithering will make the color of the drawn image smoother and fuller, and the image clearer paint.setDither (true); / / if this item is set to true, the image will filter out the optimization operation of the Bitmap image in the course of animation and speed up the display / / speed. This setting depends on the setting of dither and xfermode paint.setFilterBitmap (true) / / capture the whole picture, from the upper left corner to the lower right corner Rect src = new Rect (0,0, iconWidth, iconHeight); / / the position of the intercepted image on the canvas Rect dst = new Rect (0,0, iconWidth, iconHeight); canvas.drawBitmap (icon, src, dst, paint) / / Anti-aliasing and use your own text kerning Paint numPaint = new Paint (Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG); / / set text color numPaint.setColor (color); / / set text size numPaint.setTextSize (textSize); / / set text font numPaint.setTypeface (typeface) / / draw the text content on the picture. The coordinates of x and y directly calculate the width-height offset ratio of the text on the picture canvas.drawText (text, iconWidth * offsetX, iconHeight * offsetY, numPaint); return contactIcon;}}

The digital font size does not fit with the screen size, and you can scale the font dynamically according to the width of the screen.

Call method:

/ dynamically draw date numbers on the picture ((ImageView) convertView.findViewById (R.id.iv_leftImage)) .setImageBitmap (QNotifacationIcon.generatorContactIcon (context, list_left_iamge_array [position], String.valueOf (new Date (). GetDate ()), 30f, Color.GRAY, Typeface.DEFAULT_BOLD, 0.35f, 0.75f)) This is the end of the article on "how to display numbers on the icon by android". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to display numbers on the icon by android". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report