android實(shí)現(xiàn)在圖標(biāo)上顯示數(shù)字
本文實(shí)例為大家分享了android實(shí)現(xiàn)在圖標(biāo)上顯示數(shù)字的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:

動(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));
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android UI控件之Gallery實(shí)現(xiàn)拖動(dòng)式圖片瀏覽效果
這篇文章主要為大家詳細(xì)介紹了Android UI控件之Gallery實(shí)現(xiàn)拖動(dòng)式圖片瀏覽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
android自定義進(jìn)度條漸變色View的實(shí)例代碼
這篇文章主要介紹了android自定義進(jìn)度條漸變色View的實(shí)例代碼,有需要的朋友可以參考一下2014-01-01
Android實(shí)現(xiàn)簡(jiǎn)單實(shí)用的搜索框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單實(shí)用的搜索框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android打開系統(tǒng)相機(jī)并拍照的2種顯示方法
這篇文章主要介紹了Android打開系統(tǒng)相機(jī)并拍照的2種顯示方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
手把手教你實(shí)現(xiàn)Android編譯期注解
今天給大家介紹Android編譯期注解sdk的步驟以及注意事項(xiàng),并簡(jiǎn)要分析了運(yùn)行時(shí)注解以及字節(jié)碼技術(shù)在生成代碼上與編譯期注解的不同與優(yōu)劣,感興趣的朋友一起看看吧2021-07-07

