例如簡訊或者新聞的條數經常做在圖片之上,實現方式如下,可以根據不同的圖片大小進行調節
private Bitmap generateNumberIcon(int resId, int newsCount){ Bitmap icon = BitmapFactory.decodeResource(getResources(), resId); int iconSize = icon.getWidth(); Bitmap mBitmap = Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888); Canvas canvas = new Canvas(mBitmap); Paint paint = new Paint(); paint.setDither(true); paint.setFilterBitmap(true); Rect src = new Rect(0, 0, iconSize, iconSize); Rect dst = new Rect(0, 0,iconSize, iconSize); canvas.drawBitmap(icon, src, dst, paint); Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DEV_KERN_TEXT_FLAG); countPaint.setColor(getResources().getColor(R.color.red)); countPaint.setStyle(Paint.Style.STROKE); countPaint.setTypeface(Typeface.DEFAULT_BOLD); countPaint.setTextSize(iconSize*2/3f); canvas.drawText(String.valueOf(newsCount),iconSize/4 , iconSize*5/6, countPaint); return mBitmap; }
參考效果: