【问题标题】:How to add the images into the edit text field and how to save it as the whole edit text image in android如何将图像添加到编辑文本字段中以及如何将其保存为android中的整个编辑文本图像
【发布时间】:2014-05-23 13:42:51
【问题描述】:

在我的应用程序中,我将出现一个屏幕,在该屏幕中,我将放置编辑文本字段,在该编辑文本字段下方,我将使用选择和保存按钮,在编辑文本字段内我将输入一些文本,然后光标指向那个地方,我必须添加图像,我想使用选择按钮从图库中获取图像,然后再次将图像放入编辑文本字段中,我想输入一些东西之后,我将单击保存按钮,将编辑文本字段作为图像保存到图库中,这是我在 Google 中搜索过的其他内容,但我找不到任何解决方案,请任何人帮助我。

【问题讨论】:

标签: android android-layout android-edittext android-button android-imagebutton


【解决方案1】:

EditText 是一种视图。将该视图转换为位图并将它们存储在您想要的任何位置。 Java 代码是

// capture bitmapt of genreated textview
int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(spec, spec);
textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
canvas.translate(-textView.getScrollX(), -textView.getScrollY());
textView.draw(canvas);
textView.setDrawingCacheEnabled(true);
Bitmap cacheBmp = textView.getDrawingCache();
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
textView.destroyDrawingCache(); // destory drawable

在这个课程中找到完整的例子https://github.com/kpbird/chips-edittext-library/blob/master/ChipsEditTextLibrary/src/com/kpbird/chipsedittextlibrary/ChipsMultiAutoCompleteTextview.java

在EditText中设置图片

SpannableStringBuilder ssb = new SpannableStringBuilder("");
    BitmapDrawable bmpDrawable = new BitmapDrawable(viewBmp);
    bmpDrawable.setBounds(0, 0,bmpDrawable.getIntrinsicWidth(),bmpDrawable.getIntrinsicHeight());
    // create and set imagespan
    ssb.setSpan(new ImageSpan(bmpDrawable),x ,x + c.length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
edtText.setText(ssb);

【讨论】:

  • 在此之前,如果可能的话,我需要将图片从图库中获取到编辑文本中。
  • 更新了我的答案。也请看一下链接。两者的代码(图像到文本和文本到图像)都可以在此处获得。
  • 抱歉,Hardik Trivedi,这段代码对我来说没有帮助,我想从图库中挑选图像。
  • 从图库中选择图片使用意图stackoverflow.com/questions/2507898/…
  • 在编辑文本字段中我如何从图库中获取图像,天气我想在图像视图中使用图像视图。
猜你喜欢
  • 1970-01-01
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多