1. Setting Text to Image On Android
2. Adjudt the text font size based on the android resolution
3. 文字居中: paint.setTextAlign(Align.CENTER);然后drawText的x坐标设置为width / 2即可
private float toPixel(Resources res, int dip)
{
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dip, res.getDisplayMetrics());
return px;
}
public Bitmap createBitmap(Bitmap src, String str)
{
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int w = src.getWidth();
int h = src.getHeight();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int height = (int)960*68/screenHeight;
Bitmap bmpTemp = Bitmap.createBitmap(w, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bmpTemp);
Paint p = new Paint();
Typeface font = Typeface.create(Typeface.DEFAULT,Typeface.BOLD);
p.setColor(Color.GRAY);
p.setTypeface(font);
int fontSize = (int)toPixel(getResources(),11);
p.setTextSize(fontSize);
p.setTextAlign(Align.CENTER);
canvas.drawBitmap(src, 0, 0, p);
int y = (int)960 *63/screenHeight;
int x = (int)screenWidth/10;
int adjustWidth = (int)(screenWidth/10 - (screenWidth - 5 *w)/10);
if (adjustWidth < x
&& adjustWidth > 0)
{
x = adjustWidth;
}
canvas.drawText(str,x , y, p);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return bmpTemp;
}
{
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dip, res.getDisplayMetrics());
return px;
}
public Bitmap createBitmap(Bitmap src, String str)
{
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int w = src.getWidth();
int h = src.getHeight();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int height = (int)960*68/screenHeight;
Bitmap bmpTemp = Bitmap.createBitmap(w, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bmpTemp);
Paint p = new Paint();
Typeface font = Typeface.create(Typeface.DEFAULT,Typeface.BOLD);
p.setColor(Color.GRAY);
p.setTypeface(font);
int fontSize = (int)toPixel(getResources(),11);
p.setTextSize(fontSize);
p.setTextAlign(Align.CENTER);
canvas.drawBitmap(src, 0, 0, p);
int y = (int)960 *63/screenHeight;
int x = (int)screenWidth/10;
int adjustWidth = (int)(screenWidth/10 - (screenWidth - 5 *w)/10);
if (adjustWidth < x
&& adjustWidth > 0)
{
x = adjustWidth;
}
canvas.drawText(str,x , y, p);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return bmpTemp;
}