/** * 圖片內加數字 * * @param resourcesId . * @param count . */ private Bitmap addNumbersToPicture(Context context,int resourcesId, int count) { Drawable mCounterDrawable = null; Paint mPaint = null; if (mCounterDrawable == null) { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mCounterDrawable = context.getResources().getDrawable(resourcesId); mPaint.setTextAlign(Paint.Align.CENTER); mPaint.setTypeface(Typeface.DEFAULT_BOLD); mPaint.setColor(Color.WHITE); } if (count < 10) { mPaint.setTextSize(Utils.sp2px(context,16)); } else { mPaint.setTextSize(Utils.sp2px(context,12)); } Paint.FontMetrics fontMetrics = mPaint.getFontMetrics(); Bitmap bitmapDrawable = ((BitmapDrawable) mCounterDrawable).getBitmap().copy(Bitmap.Config.ARGB_8888, true); int bitmapX = bitmapDrawable.getWidth(); int bitmapY = bitmapDrawable.getHeight(); Canvas canvas = new Canvas(bitmapDrawable); canvas.drawText(String.valueOf(count), bitmapX / 2, bitmapY / 2 + (fontMetrics.bottom - fontMetrics.top) / 4, mPaint); canvas.save(); return bitmapDrawable; }