您好,登錄后才能下訂單哦!
前言
本文是我之前寫的這篇文章《Android圖文混排-實(shí)現(xiàn)EditText圖文混合插入上傳》的升級(jí)版,除了在EditText實(shí)現(xiàn)了圖片上傳之外,還包含了視頻上傳、云盤文件上傳、錄音上傳以及顯示上傳進(jìn)度。目前應(yīng)用于蜜蜂-集結(jié)號(hào)-任務(wù)模塊。
首先介紹一下該功能的實(shí)現(xiàn)效果:
實(shí)現(xiàn)思路
實(shí)現(xiàn)思路與之前介紹的稍有不同,但是依然是使用SpannableString實(shí)現(xiàn)的。由于這里不僅僅支持圖片上傳,還支持音頻、視頻、文件上傳,為了以后方便擴(kuò)展更多類型,這里不再使用標(biāo)簽實(shí)現(xiàn),而是直接以JSON實(shí)現(xiàn)。以前的實(shí)現(xiàn)思路是"<img url ="xxx.jpg">",現(xiàn)在每一個(gè)富文本元素都是"{"type":"video", "data":{ "url":"xxx.mp4", "thumb":"base64 str", "size":1024 }}" 這樣的字符串替換出來(lái)的,"type"有"video","audio","image","text","file"等類型,針對(duì)不同類型,"data"里面的字段也不同。"data"里面一般包含文件名、文件大小、文件網(wǎng)絡(luò)路徑、音視頻長(zhǎng)度等字段。
圖片或視頻的上傳進(jìn)度改變時(shí),切回主線程不斷更新UI,所謂更新UI,其實(shí)就是不斷的去替換這個(gè)SpannableString。對(duì)于各種樣式的ImageSpan,實(shí)際上都是BitmapDrawable。
實(shí)現(xiàn)富文本元素插入到EditText中
實(shí)現(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, 將音頻播放條這個(gè)view轉(zhuǎn)換成了drawable,因此它就可以在EditText中顯示了。同理圖片、視頻、文件的實(shí)現(xiàn)方式也是如此。
實(shí)現(xiàn)富文本元素的點(diǎn)擊事件
要做到點(diǎn)擊視頻跳轉(zhuǎn)到視頻播放頁(yè)面,點(diǎn)擊音頻播放音頻,點(diǎn)擊文件跳轉(zhuǎn)到文件預(yù)覽頁(yè)面,就必須給這些富文本元素添加點(diǎn)擊事件。這里的通用實(shí)現(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é)號(hào)富文本Span的點(diǎn)擊事件 * * @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());
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。