亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實現(xiàn)EditText的富文本編輯

 更新時間:2017年08月12日 16:19:55   作者:LeoLeoHan  
這篇文章主要介紹了Android實現(xiàn)EditText的富文本編輯,具有一定的參考價值,感興趣的小伙伴們可以參考一下

前言

本文是我之前寫的這篇文章《Android圖文混排-實現(xiàn)EditText圖文混合插入上傳》的升級版,除了在EditText實現(xiàn)了圖片上傳之外,還包含了視頻上傳、云盤文件上傳、錄音上傳以及顯示上傳進度。目前應(yīng)用于蜜蜂-集結(jié)號-任務(wù)模塊。

首先介紹一下該功能的實現(xiàn)效果:

實現(xiàn)思路

實現(xiàn)思路與之前介紹的稍有不同,但是依然是使用SpannableString實現(xiàn)的。由于這里不僅僅支持圖片上傳,還支持音頻、視頻、文件上傳,為了以后方便擴展更多類型,這里不再使用標(biāo)簽實現(xiàn),而是直接以JSON實現(xiàn)。以前的實現(xiàn)思路是"<img url ="xxx.jpg">",現(xiàn)在每一個富文本元素都是"{"type":"video", "data":{ "url":"xxx.mp4", "thumb":"base64 str", "size":1024 }}" 這樣的字符串替換出來的,"type"有"video","audio","image","text","file"等類型,針對不同類型,"data"里面的字段也不同。"data"里面一般包含文件名、文件大小、文件網(wǎng)絡(luò)路徑、音視頻長度等字段。

圖片或視頻的上傳進度改變時,切回主線程不斷更新UI,所謂更新UI,其實就是不斷的去替換這個SpannableString。對于各種樣式的ImageSpan,實際上都是BitmapDrawable。

實現(xiàn)富文本元素插入到EditText

實現(xiàn)代碼如下:

 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);

  }

這里的TaskSpan繼承了ImageSpan, 將音頻播放條這個view轉(zhuǎn)換成了drawable,因此它就可以在EditText中顯示了。同理圖片、視頻、文件的實現(xiàn)方式也是如此。

實現(xiàn)富文本元素的點擊事件

要做到點擊視頻跳轉(zhuǎn)到視頻播放頁面,點擊音頻播放音頻,點擊文件跳轉(zhuǎn)到文件預(yù)覽頁面,就必須給這些富文本元素添加點擊事件。這里的通用實現(xiàn)就是自定義LinkMovementMethod:

package com.gnet.uc.activity.appcenter;

import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.MotionEvent;
import android.widget.TextView;

/**
 * 集結(jié)號富文本Span的點擊事件
 *
 * @author lei.han
 * @time 2017/6/20 下午11:02
 */
public class TaskMovementMethod extends LinkMovementMethod {

  public boolean onTouchEvent(TextView widget, Spannable buffer,
                MotionEvent event) {
    int action = event.getAction();

    if (action == MotionEvent.ACTION_UP ||
        action == MotionEvent.ACTION_DOWN) {
      int x = (int) event.getX();
      int y = (int) event.getY();

      x -= widget.getTotalPaddingLeft();
      y -= widget.getTotalPaddingTop();

      x += widget.getScrollX();
      y += widget.getScrollY();

      Layout layout = widget.getLayout();
      int line = layout.getLineForVertical(y);
      int off = layout.getOffsetForHorizontal(line, x);
      float xLeft = layout.getPrimaryHorizontal(off);
      if (xLeft < x) {
        off += 1;
      } else {
        off -= 1;
      }

      ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
      TaskSpan[] spans = buffer.getSpans(off, off, TaskSpan.class);

      if (link.length != 0) {
        if (action == MotionEvent.ACTION_UP) {
          link[0].onClick(widget);
        } else if (action == MotionEvent.ACTION_DOWN) {
          Selection.setSelection(buffer,
              buffer.getSpanStart(link[0]),
              buffer.getSpanEnd(link[0]));
        }

        return true;
      } else if (spans.length != 0) {
        if (action == MotionEvent.ACTION_UP) {
          spans[0].onClick(widget);
        } else if (action == MotionEvent.ACTION_DOWN) {
          Selection.setSelection(buffer,
              buffer.getSpanStart(spans[0]),
              buffer.getSpanEnd(spans[0]));
        }

        return true;
      } else {
        Selection.removeSelection(buffer);
      }
    }

    return false;
  }
}

editText.setMovementMethod(new TaskMovementMethod());

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論