android 文本编辑器添加图片,Android富文本编辑器,支持图文混排,支持插入和删除图片…

  • Post author:
  • Post category:其他


XRichText

一个Android富文本类库,支持编辑和预览,支持插入和删除图片。

GitHub地址:https://github.com/sendtion/XRichText

截图预览

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

使用方式

1、作为类库

把xrichtext作为一个module导入你的工程。

或者

把xrichtext中的文件拷贝到你的工程,可以在你的工程中建一个xrichtextming包名,并把文件拷贝进去。

在xml布局中添加基于EditText编辑器(可编辑):

android:id=”@+id/et_new_content”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:textSize=”@dimen/text_size_16″

android:textColor=”@color/grey_600″/>

在xml布局中添加基于TextView编辑器(不可编辑):

android:id=”@+id/tv_note_content”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:textSize=”@dimen/text_size_16″

android:textColor=”@color/grey_600″/>

我把数据保存为了html格式,生成字符串存储到了数据库。

生成数据:

String noteContent = getEditData();

private String getEditData() {

List editList = et_new_content.buildEditData();

StringBuffer content = new StringBuffer();

for (RichTextEditor.EditData itemData : editList) {

if (itemData.inputStr != null) {

content.append(itemData.inputStr);

} else if (itemData.imagePath != null) {

content.append(”
%5C%22%22).append(itemData.imagePath).append(%22%5C%22
“);

}

}

return content.toString();

}

显示数据:

et_new_content.post(new Runnable() {

@Override

public void run() {

showEditData(content);

}

});

protected void showEditData(String content) {

et_new_content.clearAllLayout();

List textList = StringUtils.cutStringByImgTag(content);

for (int i = 0; i < textList.size(); i++) {

String text = textList.get(i);

if (text.contains(”

String imagePath = StringUtils.getImgSrc(text);

int width = ScreenUtils.getScreenWidth(this);

int height = ScreenUtils.getScreenHeight(this);

et_new_content.measure(0,0);

Bitmap bitmap = ImageUtils.getSmallBitmap(imagePath, width, height);

if (bitmap != null){

et_new_content.addImageViewAtIndex(et_new_content.getLastIndex(), bitmap, imagePath);

} else {

et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), text);

}

et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), text);

}

}

}

2、gradle依赖

稍后支持。