88import android .graphics .Typeface ;
99
1010public class BitmapUtil {
11+ private static int height = 60 ;
12+ private static int baseLine = 48 ;
1113
1214 public static Bitmap getTextBitmap (String name , Typeface typeface , float fontSize , int color ) {
1315 Paint paint = new Paint ();
@@ -24,9 +26,9 @@ public static Bitmap getTextBitmap(String name, Typeface typeface, float fontSiz
2426 paint .getTextBounds (name , 0 , name .length (), rect );
2527 // 获取字符串在屏幕上的长度
2628 int width = (int ) (paint .measureText (name ));
27- final Bitmap bmp = Bitmap .createBitmap (width , rect . height () , Bitmap .Config .ARGB_8888 );
29+ final Bitmap bmp = Bitmap .createBitmap (width , height , Bitmap .Config .ARGB_8888 );
2830 Canvas canvas = new Canvas (bmp );
29- canvas .drawText (name , rect .left , rect . height () - rect . bottom , paint );
31+ canvas .drawText (name , rect .left , baseLine , paint );
3032 return bmp ;
3133 }
3234
@@ -39,4 +41,14 @@ public static Bitmap setBitmapSize(Bitmap bitmap, float size) {
3941 matrix .postScale (scaleWidth , scaleHeight );
4042 return Bitmap .createBitmap (bitmap , 0 , 0 , width , height , matrix , true );
4143 }
44+
45+ // private static int getBaseLine(Paint paint) {
46+ // if (baseLine == 0) {
47+ // Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
48+ // int dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
49+ // baseLine = dy;
50+ // return height / 2 + dy;
51+ // }
52+ // return baseLine;
53+ // }
4254}
0 commit comments