支持富文本插入图片,文本的自定义View
来源:互联网 发布:软件测试工程师教学 编辑:程序博客网 时间:2024/05/20 07:33
在比较潮流的App中,我们经常看到富文本的身影,能支持图文混排,其实图文混排不是那么难,这段时间由于项目的需要,我自己实现了图文混排,即能支持插入图排,又可以编辑文本。其实原理很简单,把ImageView跟EditText动态加入一个layout容器里面,然后根据最近的焦点来添加view,具体请看我的view,里面已经注释很清楚了。
public class RichTextView extends ScrollView { private static final int EDIT_PADDING = 10; // edittext常规padding是10dp private int viewTagIndex = 1; // 新生的view都会打一个tag,对每个view来说,这个tag是唯一的。 private LinearLayout allLayout; // 这个是所有子view的容器,scrollView内部的唯一一个ViewGroup private LayoutInflater inflater; private int editNormalPadding = 0; // public RichTextView(Context context) { this(context, null); } public RichTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RichTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); inflater = LayoutInflater.from(context); // 1. 初始化allLayout allLayout = new LinearLayout(context); allLayout.setOrientation(LinearLayout.VERTICAL); //allLayout.setBackgroundColor(Color.WHITE);//去掉背景 LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); allLayout.setPadding(50,15,50,15);//设置间距,防止生成图片时文字太靠边 addView(allLayout, layoutParams); LinearLayout.LayoutParams firstEditParam = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); //editNormalPadding = dip2px(EDIT_PADDING); TextView firstText = createTextView("没有内容", dip2px(context, EDIT_PADDING)); allLayout.addView(firstText, firstEditParam); } public int dip2px(Context context, float dipValue) { float m = context.getResources().getDisplayMetrics().density; return (int) (dipValue * m + 0.5f); } /** * 清除所有的view */ public void clearAllLayout(){ allLayout.removeAllViews(); } /** * 获得最后一个子view的位置 * @return */ public int getLastIndex(){ int lastEditIndex = allLayout.getChildCount(); return lastEditIndex; } /** * 生成文本输入框 */ public TextView createTextView(String hint, int paddingTop) { TextView textView = (TextView) inflater.inflate(R.layout.rich_textview, null); textView.setTag(viewTagIndex++); textView.setPadding(editNormalPadding, paddingTop, editNormalPadding, paddingTop); textView.setHint(hint); return textView; } /** * 生成图片View */ private RelativeLayout createImageLayout() { RelativeLayout layout = (RelativeLayout) inflater.inflate( R.layout.edit_imageview, null); layout.setTag(viewTagIndex++); View closeView = layout.findViewById(R.id.image_close); closeView.setVisibility(GONE); return layout; } /** * 在特定位置插入EditText * * @param index * 位置 * @param editStr * EditText显示的文字 */ public void addTextViewAtIndex(final int index, CharSequence editStr) { TextView textView = createTextView("", EDIT_PADDING); textView.setText(editStr); allLayout.addView(textView, index); } /** * 在特定位置添加ImageView */ public void addImageViewAtIndex(final int index, String imagePath) { Bitmap bmp = BitmapFactory.decodeFile(imagePath); final RelativeLayout imageLayout = createImageLayout(); DataImageView imageView = (DataImageView) imageLayout.findViewById(R.id.edit_imageView); Glide.with(getContext()).load(imagePath).crossFade().centerCrop().into(imageView); //imageView.setImageBitmap(bmp);//这里改用Glide加载图片 //imageView.setBitmap(bmp);//这句去掉,保留下面的图片地址即可,优化图片占用 imageView.setAbsolutePath(imagePath); // 调整imageView的高度 int imageHeight = allLayout.getWidth() * bmp.getHeight() / bmp.getWidth(); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, imageHeight); lp.bottomMargin = 10; imageView.setLayoutParams(lp); allLayout.addView(imageLayout, index); } /** * 根据view的宽度,动态缩放bitmap尺寸 * * @param width * view的宽度 */ public Bitmap getScaledBitmap(String filePath, int width) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); int sampleSize = options.outWidth > width ? options.outWidth / width + 1 : 1; options.inJustDecodeBounds = false; options.inSampleSize = sampleSize; return BitmapFactory.decodeFile(filePath, options); }}
0 0
- 支持富文本插入图片,文本的自定义View
- UITextView富文本、插入图片
- unity 富文本 插入图片
- sharepoint富文本里插入图片、视频
- Android富文本编辑器,支持图文混排,支持插入和删除图片
- Android 富文本编辑器,支持图文混排,支持插入和删除图片
- Android 富文本编辑器,支持图文混排,支持插入和删除图片
- 富文本图片上传
- Label 的富文本 与 自定义Label
- 富文本编辑器上传图片的问题
- 文字+图片的混编(富文本)
- iOS富文本设置样式,插入图片,点击事件
- wangEditor富文本插入图片文本框高度适配
- 用富文本summernote插入图片放到七牛云
- React中使用富文本编辑器Quill,支持粘贴图片
- 百度富文本编辑器UEditor自定义上传图片接口
- Ueditor富文本编辑器--上传图片自定义上传操作
- 富文本(TYAttributedLabel 简单,强大的属性文本控件(无需了解CoreText),支持图文混排显示,支持添加链接,image和UIView控件,支持自定义排版显示)
- 算法:RSA算法原理(一)
- 个人站长才能提高付费文章的转换收入率呢
- 关于如何在php7中使用mysqli扩展
- JS远程监测-Flume设置
- 彻底理解webservice SOAP WSDL
- 支持富文本插入图片,文本的自定义View
- 【安卓开发艺术探索】第2章 进程线程通信与同步 笔记
- 适配器模式
- mysql数据库的主从复制
- 国内首家中高端自由职业者共享平台——易分之一,即将上线运营
- 数据结构——链表(JavaScript)
- IOS 时间定时器
- 低成本多串口ARM9工控主板解决方案
- 使用onChange ,获取input标签的值