简体   繁体   English

在画布上加载图片并在图片上覆盖文字并保存

[英]Load a picture on canvas and Overlay text on the picture and save it

I want to create an application which will simply load the selected the image to a canvas and the user will have freedon to overlay text on the image anywhere on the image. 我想创建一个应用程序,将选定的图像简单地加载到画布上,用户将可以随意在图像上的任何位置覆盖文本。 ie is after he have overlayed text on the image he should be able to move the text, edit the text. 也就是说,在他将文字覆盖在图像上之后,他应该能够移动文字,编辑文字。 How can this be achieved. 如何做到这一点。 Is it that i have to load a frame layout and then on that i have to dynamically place text view to get the text overlay or anything else can be done. 是我必须加载框架布局,然后必须动态放置文本视图以获取文本覆盖图,还是可以执行其他任何操作? I want to achieve something like the picture given below 我想实现如下图所示的效果 在此处输入图片说明 When a "Add text" is clicked from action bar I want to add the text to the Picture loaded by user. 从操作栏中单击“添加文本”时,我想将文本添加到用户加载的图片中。 Searched a lot but could not get any answers. 搜索了很多,但没有得到任何答案。 I just want how to get this text on the image and move it anywhere on the picture. 我只想知道如何在图像上获取此文本并将其移动到图片上的任何位置。 Also the final image that will be saved on the device will be just with the text overlayed on the image. 同样,将要保存在设备上的最终图像将与文本重叠在图像上。 the box appearing in picture should not be present in the image. 图片中出现的框不应出现在图像中。 All the experts out their please guide me. 所有专家请他们指导我。

You can add ImageView and EditText to FrameLayout, then EditText will overlay your image. 您可以将ImageView和EditText添加到FrameLayout,然后EditText将覆盖您的图像。 To drag view you should set OnTouchListner: 要拖动视图,您应该设置OnTouchListner:

     mEditText.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    final int action = event.getAction();

    mLastTouchX = event.getRawX();
    mLastTouchY = event.getRawY();

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) getLayoutParams();
    mDeltaX = mLastTouchX - lParams.leftMargin;
    mDeltaY = mLastTouchY - lParams.topMargin;

    break;
    }
    case MotionEvent.ACTION_MOVE: {
    mLastTouchX = event.getRawX();
    mLastTouchY = event.getRawY();

    final RelativeLayout.LayoutParams params = (LayoutParams) getLayoutParams();
    params.leftMargin = (int) (mLastTouchX - mDeltaX);
    params.topMargin = (int) (mLastTouchY - mDeltaY);
    setLayoutParams(params);

    break;
    }
    }
    invalidate();

    return true;
    }
});

To create final image you can draw on canvas your original image and text. 要创建最终图像,您可以在画布上绘制原始图像和文本。

UPDATED: You can add view with addView: 更新:您可以使用addView添加视图:

final EditText et  = new EditText(getContext());
et.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mFramLayout.addView(et);

Also it's possible to adjust position with margins. 也可以用边距调整位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM