|
47 | 47 |
|
48 | 48 | import androidx.core.content.ContextCompat; |
49 | 49 |
|
| 50 | +import java.io.InputStream; |
50 | 51 | import java.lang.reflect.Field; |
| 52 | +import java.net.HttpURLConnection; |
| 53 | +import java.net.URL; |
51 | 54 |
|
52 | 55 | public class VariableTextInput extends LinearLayout { |
53 | 56 | private VariableEditText editText; |
54 | 57 | private ScrollView scrollView; |
55 | 58 | private Boolean ignoreNextLocalTextChange = false; |
56 | | - |
| 59 | + private Bitmap imgBitmap = null; |
57 | 60 | private Context mContext; |
58 | 61 | private SpannableString mSpannableString; |
59 | 62 | private Editable mEditable; |
@@ -478,22 +481,71 @@ private RichTextBean handleParams(ReadableMap map) { |
478 | 481 | if (map.hasKey(ActivityConst.TEXT)) { |
479 | 482 | richTextBean.text = map.getString(ActivityConst.TEXT); |
480 | 483 | } |
| 484 | + if (map.hasKey(ActivityConst.EMOJI_URI)){ |
| 485 | + richTextBean.emojiUri = map.getString(ActivityConst.EMOJI_URI); |
| 486 | + } |
481 | 487 | return richTextBean; |
482 | 488 | } |
483 | | - |
| 489 | + /** |
| 490 | + * 通过 网络图片 url 获取图片BitMap |
| 491 | + * @param photoUrl 网络图片 url |
| 492 | + */ |
| 493 | + private void requestWebPhotoBitMap(String photoUrl){ |
| 494 | + new Thread(()->{ |
| 495 | + HttpURLConnection connection = null; |
| 496 | + try { |
| 497 | + URL bitmapUrl = new URL(photoUrl); |
| 498 | + connection = (HttpURLConnection) bitmapUrl.openConnection(); |
| 499 | + connection.setRequestMethod("GET"); |
| 500 | + connection.setConnectTimeout(5000); |
| 501 | + connection.setReadTimeout(5000); |
| 502 | + // 判断是否请求成功 |
| 503 | + if (connection.getResponseCode() == 200) { |
| 504 | + |
| 505 | + InputStream inputStream = connection.getInputStream(); |
| 506 | + imgBitmap = BitmapFactory.decodeStream(inputStream); |
| 507 | + |
| 508 | + } else { |
| 509 | + //todo |
| 510 | + } |
| 511 | + }catch (Exception e){ |
| 512 | + //todo |
| 513 | + } |
| 514 | + }).start(); |
| 515 | + } |
484 | 516 | public void insertEmoji(RichTextBean richTextBean) { |
485 | 517 | int startIndex = editText.getSelectionStart(); |
486 | 518 | int endIndex = startIndex + richTextBean.tag.length(); |
487 | | - if (editText.getText() != null) { |
488 | | - editText.getText().insert(startIndex, richTextBean.tag); |
489 | | - } |
490 | | - Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.kuxiao); |
491 | | - TextSpan imageSpan = new TextSpan(mContext, BitmapUtil.setBitmapSize(bitmap, editText.getTextSize()), richTextBean); |
492 | | - mSpannableString = SpannableString.valueOf(editText.getText()); |
493 | | - mSpannableString.setSpan(imageSpan, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
494 | | - editText.setText(mSpannableString); |
495 | | - editText.setSelection(endIndex); |
496 | | - editText.getText().replace(startIndex, endIndex, richTextBean.content); |
| 519 | +// Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.kuxiao); |
| 520 | + new Thread(()->{ |
| 521 | + try { |
| 522 | + Bitmap iBitMap; |
| 523 | + if (richTextBean.emojiUri.startsWith("http")){ |
| 524 | + String imgUrl = richTextBean.emojiUri; |
| 525 | + URL url = new URL(imgUrl); |
| 526 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 527 | + connection.setDoInput(true); |
| 528 | + connection.connect(); |
| 529 | + InputStream input = connection.getInputStream(); |
| 530 | + iBitMap = BitmapFactory.decodeStream(input); |
| 531 | + }else { |
| 532 | + iBitMap = BitmapFactory.decodeFile(richTextBean.emojiUri); |
| 533 | + } |
| 534 | + if (editText.getText() != null) { |
| 535 | + editText.getText().insert(startIndex, richTextBean.tag); |
| 536 | + } |
| 537 | + TextSpan imageSpan = new TextSpan(mContext, BitmapUtil.setBitmapSize(iBitMap, editText.getTextSize()), richTextBean); |
| 538 | + mSpannableString = SpannableString.valueOf(editText.getText()); |
| 539 | + mSpannableString.setSpan(imageSpan, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 540 | + editText.setText(mSpannableString); |
| 541 | + editText.setSelection(endIndex); |
| 542 | + editText.getText().replace(startIndex, endIndex, richTextBean.content); |
| 543 | + }catch (Exception e){ |
| 544 | + //todo |
| 545 | + Log.d("错误", "insertEmoji: "+e); |
| 546 | + } |
| 547 | + }).start(); |
| 548 | + |
497 | 549 | } |
498 | 550 |
|
499 | 551 | private void insertMentions(RichTextBean richTextBean) { |
|
0 commit comments