资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android实现EditText的富文本编辑-创新互联

前言

成都创新互联公司是一家专注于网站设计制作、网站建设与策划设计,滦平网站建设哪家好?成都创新互联公司做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:滦平等地区。滦平做网站价格咨询:18982081108

本文是我之前写的这篇文章《Android图文混排-实现EditText图文混合插入上传》的升级版,除了在EditText实现了图片上传之外,还包含了视频上传、云盘文件上传、录音上传以及显示上传进度。目前应用于蜜蜂-集结号-任务模块。

首先介绍一下该功能的实现效果:


实现思路

实现思路与之前介绍的稍有不同,但是依然是使用SpannableString实现的。由于这里不仅仅支持图片上传,还支持音频、视频、文件上传,为了以后方便扩展更多类型,这里不再使用标签实现,而是直接以JSON实现。以前的实现思路是"",现在每一个富文本元素都是"{"type":"video", "data":{ "url":"xxx.mp4", "thumb":"base64 str", "size":1024 }}" 这样的字符串替换出来的,"type"有"video","audio","image","text","file"等类型,针对不同类型,"data"里面的字段也不同。"data"里面一般包含文件名、文件大小、文件网络路径、音视频长度等字段。

图片或视频的上传进度改变时,切回主线程不断更新UI,所谓更新UI,其实就是不断的去替换这个SpannableString。对于各种样式的ImageSpan,实际上都是BitmapDrawable。

实现富文本元素插入到EditText

实现代码如下:

 public static TaskSpan getAudioSpan(Context context, int type, String json, String time, int progress) {
    View spanView = View.inflate(context, R.layout.bbs_audio_bar_tag, null);
    LinearLayout llBg = (LinearLayout) spanView.findViewById(R.id.ll_bg);
    ImageView icPlay = (ImageView) spanView.findViewById(R.id.iv_play);
    ImageView icStop = (ImageView) spanView.findViewById(R.id.iv_stop);
    TextView tvTime = (TextView) spanView.findViewById(R.id.tv_time);
    ProgressBar proBar = (ProgressBar) spanView.findViewById(R.id.progress_bar);

    switch (type) {
      case AUDIO_PLAY_NONE:
        try {
          final String[] split = json.split(BBSConstants.SPLIT_TAG);
          JSONObject obj = new JSONObject(split[1]);
          final JSONObject data = obj.optJSONObject(Constants.RETURN_DATA);
          int duration = data.optInt(BBSConstants.LONG_DATA_DURATION);

          tvTime.setText(DateUtil.getDurationTime(duration / 1000, false));
          proBar.setProgress(0);
          icPlay.setVisibility(View.VISIBLE);
          icStop.setVisibility(View.GONE);
          llBg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.grey_bg_50dp_corner_no_border));
        } catch (JSONException e) {
          e.printStackTrace();
        }
        break;

      case AUDIO_PLAY_ING:
        proBar.setProgress(progress);
        icPlay.setVisibility(View.GONE);
        icStop.setVisibility(View.VISIBLE);
        llBg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.blue_bg_50dp_corner_no_border));
        tvTime.setText(time);
        break;
    }

    BitmapDrawable drawable = (BitmapDrawable) ViewUtil.convertViewToDrawable(spanView);
    drawable.setTargetDensity(MyApplication.getInstance().getResources().getDisplayMetrics());
    final float scale = 1.0f / 6.0f;
    final int width = DeviceUtil.getScreenWidth((Activity) context) - DeviceUtil.dip2px(context, LENGTH);
    float height = (float) width * scale;
    drawable.setBounds(0, 0, width, (int) height);
    return new TaskSpan(drawable, type, json);

  }

分享名称:Android实现EditText的富文本编辑-创新互联
网页网址:http://cdkjz.cn/article/jpije.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220