您好,登錄后才能下訂單哦!
這篇文章主要介紹了android怎么實(shí)現(xiàn)在圖標(biāo)上顯示數(shù)字的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇android怎么實(shí)現(xiàn)在圖標(biāo)上顯示數(shù)字文章都會(huì)有所收獲,下面我們一起來看看吧。
效果圖:
動(dòng)態(tài)顯示當(dāng)天的號(hào)數(shù)。
主要代碼如下:
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 2014年3月5日 * */ public class QNotifacationIcon { /** * 在給定的圖片上添加文字 * * @param context * 上下文對(duì)象 * @param resId * 圖片資源ID * @param text * 需要顯示的文字 * @param textSize * 文字大小 * @param color * 文字顏色 * @param typeface * 文字字體 * @param offsetX * 文字x的偏移量 * @param offsetY * 文字y的偏移量 * @return 帶文字的圖片 */ public static Bitmap generatorContactIcon(Context context, int resId, String text, float textSize, int color, Typeface typeface, float offsetX, float offsetY) { // 根據(jù)id獲取需要處理的圖片 Bitmap icon = ((BitmapDrawable) (context.getResources() .getDrawable(resId))).getBitmap(); int iconWidth = icon.getWidth(); int iconHeight = icon.getHeight(); // 初始化畫布 Bitmap contactIcon = Bitmap.createBitmap(iconWidth, iconHeight, Config.ARGB_8888); // 構(gòu)建 Canvas canvas = new Canvas(contactIcon); // 創(chuàng)建畫筆 Paint paint = new Paint(); // 設(shè)定是否使用圖像抖動(dòng)處理,會(huì)使繪制出來的圖片顏色更加平滑和飽滿,圖像更加清晰 paint.setDither(true); // 如果該項(xiàng)設(shè)置為true,則圖像在動(dòng)畫進(jìn)行中會(huì)濾掉對(duì)Bitmap圖像的優(yōu)化操作,加快顯示 // 速度,本設(shè)置項(xiàng)依賴于dither和xfermode的設(shè)置 paint.setFilterBitmap(true); // 截取整個(gè)圖片,從左上角到右下角 Rect src = new Rect(0, 0, iconWidth, iconHeight); // 截取的圖片放在畫布上的位置 Rect dst = new Rect(0, 0, iconWidth, iconHeight); canvas.drawBitmap(icon, src, dst, paint); // 抗鋸齒和使用本身的文本字距 Paint numPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG); // 設(shè)置文字顏色 numPaint.setColor(color); // 設(shè)置文字大小 numPaint.setTextSize(textSize); // 設(shè)置文字字體 numPaint.setTypeface(typeface); // 將文字內(nèi)容畫在圖片上,x和y的坐標(biāo)這里直接計(jì)算了文字在圖片上的寬高偏移比例 canvas.drawText(text, iconWidth * offsetX, iconHeight * offsetY, numPaint); return contactIcon; } }
數(shù)字字體大小會(huì)隨屏幕大小而不適應(yīng),可以根據(jù)屏幕寬度然后動(dòng)態(tài)縮放字體比例。
調(diào)用方式:
// 動(dòng)態(tài)在圖片上畫日期數(shù)字 ((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));
關(guān)于“android怎么實(shí)現(xiàn)在圖標(biāo)上顯示數(shù)字”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“android怎么實(shí)現(xiàn)在圖標(biāo)上顯示數(shù)字”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。