有多种字体在图形绘制Android



我正在尝试使用下面的代码创建一个jpeg图像:

Bitmap src = BitmapFactory.decodeResource(activity.getResources(), R.drawable.background_splash); // the original file yourimage.jpg i added in resources
           Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
            String yourText = "My custom Text adding to Image";
            Canvas cs = new Canvas(dest);
            Paint tPaint = new Paint();
            tPaint.setTextSize(35);
            tPaint.setColor(Color.BLUE);
        tPaint.setTypeface(Typeface.createFromAsset(getAssets(),
                    PATH_FONT_PUNJABI));
            cs.drawBitmap(src, 0f, 0f, null);
            float height = tPaint.measureText("yY");
            float width = tPaint.measureText(yourText);
            float x_coord = (src.getWidth() - width)/2;
            cs.drawText(yourText, x_coord, height+15f, tPaint); // 15f is to put space between top edge and the text, if you want to change it, you can
            try {
                dest.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/ImageAfterAddingText.jpg")));
                // dest is Bitmap, if you want to preview the final image, you can display it on screen also before saving
               System.out.println("Image Created");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   

现在我想要一些文本使用字体A和一些使用字体b,我怎么能做到这一点?

谢谢,阿曼

最简单的方法是使用2个单独的绘制文本命令。设置第一种字体,绘制字符串1,设置第二种字体,绘制字符串2。如果一个字符串出现在另一个字符串之后你可以通过Paint对象的measureText或getTextBounds

获取字符串的大小

最新更新