是时候做一点代码的重构了,毕竟已经是一个可运行的版本了,并且顺带做一点点美化的工作,先看效果图,重构和美化完之后的版本效果图。
重构第一部分
如果是数字版,不再使用带有数字的图片,而是使用动态创建数字图片的方式。并在图片上绘制数字,代码如下:
private void draw_text(Bitmap bitmap, String text) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG
| Paint.DEV_KERN_TEXT_FLAG);
Paint rect_paint = new Paint();
rect_paint.setColor(Color.GRAY);
rect_paint.setStyle(Paint.Style.FILL);
RectF rect = new RectF(1, 1, width - 1, height - 1);
canvas.drawRoundRect(rect, 20, 20, rect_paint);
paint.setTextSize(width * 0.5f);
paint.setColor(Color.WHITE);
paint.setFakeBoldText(false);
paint.setTextAlign(Paint.Align.CENTER);
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
float top = fontMetrics.top;
float bottom = fontMetrics.bottom;
int baseLineY = (int) (rect.centerY() - top / 2 - bottom / 2);
canvas.drawText(text, rect.centerX(), baseLineY, paint);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
}