如何将EditText的字符串转换为位图?

3
我想将EditText的字符串转换为位图。 我有一个像这样的字符串:
String str=edtext.getText().toString(); 

如何将该字符串转换为位图?

这就像将iPhone转换为Android一样。 - ingsaurabh
你的意思是想要文本框中所显示内容的图像吗?例如,如果文本框中显示的是“2”,那么你想要“2”的图像吗? - Lucifer
亲爱的,这是不可能的。字符串是一种不同的数据类型,与位图不同。尝试在文本查看器中打开任何位图并检查是否能够读取内容。 - ingsaurabh
可能是重复的问题:String to Bitmap java/android - Mudassir
可能是从字符串创建位图的重复问题。 - Paresh Mayani
显示剩余2条评论
3个回答

7

我不知道如何制作string的图像,但是下面是从EditText中创建Bitmap的代码:

因此,您将获得整个EditText的位图图像,而不仅仅是字符串。

mEditText.setCursorVisible(false);
mEditText.buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap(mEditText.getDrawingCache());

这段代码真的很有帮助。我将生成的图像分配给一个图像视图,但每次在编辑文本框中修改文本时,我都无法获得更新后的图像。 - Jogendra Gouda
1
@JogendraGouda 每次都需要创建新的图像并将其设置为ImageView,为此,您可以将该代码移动到EditText的onTextChange方法中,并注意回收未使用的位图,否则您将耗尽内存。 - MKJParekh
@MKJParekh 感谢您的评论。 您能否向我展示一些您的代码部分。 - Jogendra Gouda
我的代码?你想看哪一段代码?创建位图的代码在上面,你也在你的应用程序中完成了这个。@JogendraGouda - MKJParekh
EditText et = (EditText) findViewById(R.id.editText1);et.setCursorVisible(false);et.buildDrawingCache();Bitmap bmp = Bitmap.createBitmap(et.getDrawingCache());ImageView img = (ImageView) findViewById(R.id.imageView1);img.setImageBitmap(bmp);这是我的代码,但它仍然无法工作。 - Jogendra Gouda
显示剩余8条评论

2

我使用了以下解决方案来解决我的问题,这对我有用。

Bitmap bmp = Bitmap.createBitmap(edtext.getDrawingCache());
System.out.println("ashish"+edtext.getText().toString());
Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.balloon_overlay_focused);
Bitmap bmw=combineImages( bm,bmp);
CompositeImageViewText.setImageBitmap(bmw);

//合并图像的代码(combineimages())

public Bitmap combineImages(Bitmap c, Bitmap s) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom 
        Bitmap cs = null; 

        int width, height = 0; 

        if(c.getWidth() > s.getWidth()) { 
          width = c.getWidth(); 
          height = s.getHeight()+30 ; 
        } else { 
          width = s.getWidth(); 
          height = s.getHeight()+30 ; 
        } 

        cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

        Canvas comboImage = new Canvas(cs); 

        comboImage.drawBitmap(c, 0f, 0f, null); 
        comboImage.drawBitmap(s, 0f, 0f, null); 

        // this is an extra bit I added, just incase you want to save the new image somewhere and then return the location 
        /*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png"; 

        OutputStream os = null; 
        try { 
          os = new FileOutputStream(loc + tmpImg); 
          cs.compress(CompressFormat.PNG, 100, os); 
        } catch(IOException e) { 
          Log.e("combineImages", "problem combining images", e); 
        }*/ 

        return cs; 
      } 

希望它能帮到其他人!

0

"位图"是组成图像的像素集。

"字符串"是由字符组成的单词。

你能做的最好的事情就是根据位图的文件名来读取位图。这就是Ankit Awasthi上面所阐述的。

希望这正是您要寻找的内容...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接