在Graphic Paint Android中有多种字体

时间:2014-06-08 08:20:14

标签: java android canvas paint

我正在尝试使用以下代码创建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.我怎样才能实现这个目标?

谢谢,

阿曼

1 个答案:

答案 0 :(得分:0)

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

绘制的字符串的大小