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)文章
Android用SharedPreferences實現(xiàn)登錄注冊注銷功能
這篇文章主要為大家詳細介紹了Android用SharedPreferences實現(xiàn)登錄注冊注銷功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04Android點擊事件之多點觸摸與手勢識別的實現(xiàn)
這篇文章主要介紹了Android點擊事件之多點觸摸與手勢識別的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Android中ViewPager實現(xiàn)滑動指示條及與Fragment的配合
這篇文章主要介紹了Android中ViewPager實現(xiàn)滑動指示條及與Fragment的配合,使用Fragment實現(xiàn)ViewPager的滑動是一種比較推薦的做法,需要的朋友可以參考下2016-03-03Android開發(fā)Jetpack組件DataBinding用例詳解
這篇文章主要為大家介紹了Android開發(fā)Jetpack組件DataBinding的使案用例詳解說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02基于android中讀取assets目錄下a.txt文件并進行解析的深入分析
本篇文章是對在android需要中讀取assets目錄下a.txt文件進行了詳細的分析介紹,需要的朋友參考下2013-05-05Android實現(xiàn)原生側(cè)滑菜單的超簡單方式
網(wǎng)上關(guān)于Android實現(xiàn)側(cè)滑菜單的文章有很多,可是我們這篇文章是給大家分享一種超簡單的方式,對大家開發(fā)Android具有一定的參考借鑒價值,有需要的朋友們可以一起來看看。2016-09-09