canvas.drawText text to EditText's place

98 Views Asked by At

I have movable EditText on ImageView and save button. On click save button app should draw text from EditText to the image. For now, app is working but doesn't draw exactly to EditText's place. I need to draw precisely to EditText's place, it should look like EditText pasting text.

this is a code:

public void saveButtonClicked() {
    if (!textEditText.getText().toString().equals("")) {
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mCroppedImageUris.get(mCurrentImageIndex));
            mCropImageView.setImageBitmap(textAsBitmap(bitmap, textEditText.getText().toString(), 25f, mTextColor));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private Bitmap textAsBitmap(Bitmap image, String text,
                            float textSize, int textColor) {
    Paint paint = new Paint();
    paint.setTextSize(textSize);
    paint.setColor(textColor);
    paint.setAntiAlias(true);

    Bitmap newMapBitmap = image.copy(Bitmap.Config.ARGB_8888, true);
    try {
        Canvas canvas = new Canvas(newMapBitmap);
        int[] location = new int[2];
        addTextLayout.getLocationOnScreen(location);

        float x = (float) (location[0] / 2.1);
        float y = (float) (location[1] / 3.1);
        canvas.drawText(text, x, y, paint);

    } catch (Exception e) {
        Log.e("textAsBitmap", e.getMessage());
    }

    String root = Environment.getExternalStorageDirectory().toString();
    File folder = new File(root + Constants.pdfDirectory);

    String path = mCroppedImageUris.get(mCurrentImageIndex).getPath();
    String filename = "add_text_im";
    if (path != null) {
        filename = "added_text_" + FileUtils.getFileName(path);
    }
    File file = new File(folder, filename);

    try {
        FileOutputStream out = new FileOutputStream(file);
        newMapBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        setUpdatedImage(Uri.fromFile(file));
        out.flush();
        out.close();
    } catch (Exception e) {`your text`
        e.printStackTrace();
    }
    return newMapBitmap;
}
0

There are 0 best solutions below