Generate a image with custom text in Android

后端 未结 2 1175
迷失自我
迷失自我 2020-12-24 00:03

I\'m trying to make an app for create custom cards. I\'d like to add some text over a custom background (a jpg image).

What is the best way of doing it? I\'d need to

相关标签:
2条回答
  • 2020-12-24 00:15

    I am not sure that this is the best solution but it can help you.

    Step1: Create a relative layout(on any other) and set your image as its background.

    Step2: Now add a textview with width and height as fill_parent or match_parent and also gravity as top|center_horizontal.

    Step3: Now add an another button or any other layout control which will trigger the user confirmation. (You should place this control outside the Relative layout).

    Step4: If user has confirmed the image then you can take screenshot of your relative layout via following code:

    v1.setDrawingCacheEnabled(true);//v1 is the object of your Relative layout
                Bitmap bm = v1.getDrawingCache();
                if (bm != null) {
    
                    //TODO:write the code for saving the image.
    
                    Toast toast = Toast.makeText(YourActivity.this, "image saved",
                            Toast.LENGTH_LONG);
                    toast.show();
                } else {
                    Toast toast = Toast.makeText(YourActivity.this,
                            "No image saved.", Toast.LENGTH_LONG);
                    toast.show();
                }
    
    0 讨论(0)
  • 2020-12-24 00:28

    Use below code to achieve your requirement

        Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage); // 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.setStyle(Style.FILL);
        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
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

    You have to use below permission in manifest file.

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    For my device the path is /sdcard to access external SD card, it may vary for other devices. Some devices may have /mnt/sdcard may be it is for internal sd cards. Just check it while before using this code.

    Actually I wrote the above code for some other question, which required time stamp on photo after captured from camera. I gave you the same solution with a little modifications for your specific requirement.

    I hope you can understand this. If you have any doubts regarding code feel free to ask.

    0 讨论(0)
提交回复
热议问题