public class MarkedIconProducer { private static final int MARK_CIRCLE_RADIU = 10; public static Bitmap createMarkedIcon(Bitmap bitmap, int number) { BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap copyBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(copyBitmap); if (!bitmap.isRecycled()){ bitmap.recycle(); } Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); Paint numPaint = new Paint(Paint.ANTI_ALIAS_FLAG); numPaint.setColor(Color.WHITE); paint.setColor(Color.RED); canvas.drawCircle(options.outWidth - MARK_CIRCLE_RADIU, MARK_CIRCLE_RADIU, MARK_CIRCLE_RADIU, paint); canvas.drawText("" + number, options.outWidth - MARK_CIRCLE_RADIU / 2, options.outHeight / 2, numPaint); return copyBitmap; } }
主要思路就是利用了canvus画布功能,注意的是
Bitmap copyBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);因为很多时候我们的bitmap参数来源于解析的资源文件,而直接修改资源文件会报异常,这一句是为了防止这种情况出现