欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android實現(xiàn)在圖標上顯示數(shù)字

 更新時間:2022年04月19日 09:45:29   作者:球球_2014  
這篇文章主要為大家詳細介紹了android實現(xiàn)在圖標上顯示數(shù)字,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

效果圖:

動態(tài)顯示當天的號數(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
?? ? * ? ? ? ? ? ?上下文對象
?? ? * @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);
?? ??? ?// 構建
?? ??? ?Canvas canvas = new Canvas(contactIcon);
?
?? ??? ?// 創(chuàng)建畫筆
?? ??? ?Paint paint = new Paint();
?? ??? ?// 設定是否使用圖像抖動處理,會使繪制出來的圖片顏色更加平滑和飽滿,圖像更加清晰
?? ??? ?paint.setDither(true);
?? ??? ?// 如果該項設置為true,則圖像在動畫進行中會濾掉對Bitmap圖像的優(yōu)化操作,加快顯示
?? ??? ?// 速度,本設置項依賴于dither和xfermode的設置
?? ??? ?paint.setFilterBitmap(true);
?
?? ??? ?// 截取整個圖片,從左上角到右下角
?? ??? ?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);
?? ??? ?// 設置文字顏色
?? ??? ?numPaint.setColor(color);
?? ??? ?// 設置文字大小
?? ??? ?numPaint.setTextSize(textSize);
?? ??? ?// 設置文字字體
?? ??? ?numPaint.setTypeface(typeface);
?
?? ??? ?// 將文字內容畫在圖片上,x和y的坐標這里直接計算了文字在圖片上的寬高偏移比例
?? ??? ?canvas.drawText(text, iconWidth * offsetX, iconHeight * offsetY,
?? ??? ??? ??? ?numPaint);
?? ??? ?return contactIcon;
?? ?}
}

數(shù)字字體大小會隨屏幕大小而不適應,可以根據(jù)屏幕寬度然后動態(tài)縮放字體比例。
調用方式:

// 動態(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));

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論