Android:从模板+表单数据创建图像

时间:2016-09-02 03:39:41

标签: android image canvas

在我的项目中,我使用的是一个简单的表单字段,例如用户输入的图像,名称,公司,地点,日期时间。但在输入详细信息之前,我向用户提供了模板选择部分,其中包含三个预定义的邀请卡模板(图像)。 我希望填充的用户的所有详细信息都嵌入到他选择的邀请卡模板(图像)中,最后我可以显示包含所有输入详细信息的最终图像以及邀请卡模板上的详细信息。

1 个答案:

答案 0 :(得分:0)

//template is the card template, strings is the details, x and y are the locations of the strings.
//The top left corner of the image is (0, 0), and the bottom right is (template.width, template.height)
private static Bitmap makeDetailedBitmap(final Bitmap template, final String[] strings, final float[] x, final float[] y)
{
    Bitmap result = Bitmap.createBitmap(template.getWidth(), template.getHeight(), template.getConfig());   //Create the base image
    Canvas canvas = new Canvas(result);   //Create a canvas so we can draw onto the base image
    canvas.drawBitmap(template, 0, 0, null);   //Draw the template

    //EDIT: Forgot to set the text paint
    Paint p = new Paint();
    //p.setTextAlign(Paint.Align.CENTER);   //Uncomment if you want centered text
    p.setTextSize(12);   //Change as you please
    p.setColor(Color.BLACK);   //Change as you please

    final int len = strings.length; //Assumes that the length of x and y are >= the length of strings
    for (int i = 0; i < len; i++)
    {
        canvas.drawText(strings[i], x[i], y[i], p);  //Draws text onto image
    }
    return result;   //Returns image that was modified using the canvas (aka image with details)
}

所以只需为你想要的每个模板调用它。