将EditText保存为位图

4

我正在将包含 ImageView 和 EditText 的布局保存为位图。

我正在使用以下代码:

public void saveToImage(RelativeLayout content){

    Bitmap bitmap = Bitmap.createBitmap(content.getWidth(), content.getHeight(), Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(bitmap);
    content.layout(0, 0, content.getLayoutParams().width, content.getLayoutParams().height);
    content.draw(c);


    try{
        File file,f = null;                    
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
            {  
                 file =new File(android.os.Environment.getExternalStorageDirectory(),"TTImages_cache");
                 if(!file.exists())
                {
                  file.mkdirs();

                 } 
                 f = new File(file.getAbsolutePath()+file.separator+ "filename"+".png");
            }
          FileOutputStream ostream = new FileOutputStream(f);                                   
          bitmap.compress(CompressFormat.PNG, 10, ostream);
          ostream.close();

         } 


         catch (Exception e){
         e.printStackTrace();
        }
}

然而,我保存的图像看起来是这样的:

Layout-to-bitmap

我希望在保存位图时删除EditText中的下划线文本和文本光标。是否可能?

在你的saveToImage()方法中添加这一行代码:this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); - Haresh Chhelana
不幸的是,当键盘不存在时,闪烁的光标也会显示 :( - deimos1988
@deimos1988 你解决了你的问题吗?! - Dr.jacky
要禁用闪烁的光标,您可以在捕获位图时使用editText.setCursorVisible(false)。 - pankaj khedekar
2个回答

0

为了在保存位图之前移除闪烁的光标,您可以执行以下操作:

editText.setCursorVisible(false);

然后在之后将其设置回true


0

当您开始捕获布局时,只需删除下划线光标即可。您可以通过以下方式删除下划线:

yourEditText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

通过以下方式控制光标:

yourEditText.setCursorVisible(false);

如果你在saveToImage方法中禁用光标,会更好:

public void saveToImage(RelativeLayout content){
    yourEditText.setCursorVisible(false);
       ....
       ....
    //your code for saving the layout
}

然后在布局保存在内存中之后,只需重置yourEditText以显示光标。

public void saveToImage(RelativeLayout content){
    //your code for saving the layout
       ....
       ....
    yourEditText.setCursorVisible(true);
}

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