转eoe:http://www.eoeandroid.com/thread-4496-1-1.html
import android.text.Html; TextView t3 = (TextView) findViewById(R.id.text3); t3.setText( Html.fromHtml( "<b>text3:</b> Text with a " + "<a href=\"http://www.google.com\">link</a> " + "created in the Java source code using HTML."));
Spanned text = Html.fromHtml(source); tv.setText(text);来实现,这个用起来简单方便。
fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
ImageGetter imgGetter = new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { Drawable drawable = null; drawable = Drawable.createFromPath(source); // Or fetch it from the URL // Important drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable .getIntrinsicHeight()); return drawable; } };
ImageGetter imageGetter = new ImageGetter() { @Override public Drawable getDrawable(String source) { int id = Integer.parseInt(source); //根据id从资源文件中获取图片对象 Drawable d = getResources().getDrawable(id); d.setBounds(0, 0, d.getIntrinsicWidth(),d.getIntrinsicHeight()); return d; } };
inputLable.append(Html.fromHtml("<img src='"+clickedImageId+"'/>", imageGetter, null));其中 Html.fromHtml("<img src='"+clickedImageId+"'/>"就是HTML的图片标记,在Android中支持了部分HTML标记的使用(这方面我还在继续研究),HTML标记必须被Html.fromHtml修饰。imageGetter即为之前创建的ImageGetter类型的对象。
在此感谢网上共享资料和经验的朋友们,虽然是比较簡単的功能,但网上的文献对我的帮助还是有很大的。
四、android 短信字符转表情显示过程(转)
转:http://dianzisu.javaeye.com/blog/734327
android 的短信实现方式普通用户适应的话需要长时间的使用才能习惯,将andorid的短信模式设置成我们常用的(一般人用户)的习惯。在查看字符转图片的过程中可以猜测出腾讯的QQ表情的原理应该是一样的
只是在传送非常用的表情时是将byte数据转换为image.
转载请注明:QQ:273733055 [email protected]
/*** * * 此方法描述的是: 注意此方法在做表情转换的准备了 * @author:[email protected],[email protected] * @version: 2010-5-13 下午03:31:13 */ private void bindCommonMessage(final MessageItem msgItem) { if (mDownloadButton != null) { mDownloadButton.setVisibility(View.GONE); mDownloadingLabel.setVisibility(View.GONE); } // Since the message text should be concatenated with the sender's // address(or name), I have to display it here instead of // displaying it by the Presenter. mBodyTextView.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); // Get and/or lazily set the formatted message from/on the // MessageItem. Because the MessageItem instances come from a // cache (currently of size ~50), the hit rate on avoiding the // expensive formatMessage() call is very high. CharSequence formattedMessage = msgItem.getCachedFormattedMessage(); if (formattedMessage == null) { //肯定为null应为msgItem.formattedMessage从诞生来就没被注意过一次 formattedMessage = formatMessage(msgItem.mContact, msgItem.mBody, //重点到了 msgItem.mSubject, msgItem.mTimestamp, msgItem.mHighlight); msgItem.setCachedFormattedMessage(formattedMessage); } mBodyTextView.setText(formattedMessage); if (msgItem.isSms()) { hideMmsViewIfNeeded(); } else { Presenter presenter = PresenterFactory.getPresenter( "MmsThumbnailPresenter", mContext, this, msgItem.mSlideshow); presenter.present(); if (msgItem.mAttachmentType != WorkingMessage.TEXT) { inflateMmsView(); mMmsView.setVisibility(View.VISIBLE); setOnClickListener(msgItem); drawPlaybackButton(msgItem); } else { hideMmsViewIfNeeded(); } } drawLeftStatusIndicator(msgItem.mBoxId); drawRightStatusIndicator(msgItem); } //------------------------------------------------------------------------------ /*** * * 此方法描述的是: 开始转换了哦 * @author:[email protected],[email protected] * @version: 2010-5-13 下午03:32:52 */ private CharSequence formatMessage(String contact, String body, String subject, String timestamp, String highlight) { CharSequence template = mContext.getResources().getText(R.string.name_colon); //遇到鬼了 <主题:<xliff:g id="SUBJECT">%s</xliff:g>>" SpannableStringBuilder buf = //把他当作StringBuffer只是它可以放的不是 String 而已他能放跟多类型的东西 new SpannableStringBuilder(TextUtils.replace(template, new String[] { "%s" }, new CharSequence[] { contact })); //替换成联系人 boolean hasSubject = !TextUtils.isEmpty(subject); //主题 if (hasSubject) { buf.append(mContext.getResources().getString(R.string.inline_subject, subject)); //buff先在是 联系人 主题 XXXX eg wuyi <主题:dsadasdsa> 我爱我家 } if (!TextUtils.isEmpty(body)) { if (hasSubject) { buf.append(" - "); //如果内容有主题有就+ " - " eg wuyi <主题:sdsadsadsa> - } SmileyParser parser = SmileyParser.getInstance(); //获得表情类了哦 buf.append(parser.addSmileySpans(body)); //追查 急切关注中 } if (!TextUtils.isEmpty(timestamp)) { buf.append("\n"); int startOffset = buf.length(); // put a one pixel high spacer line between the message and the time stamp as requested // by the spec. //把之间的信息和时间戳的要求间隔一个像素的高线 //由规范 buf.append("\n"); buf.setSpan(new AbsoluteSizeSpan(3), startOffset, buf.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); startOffset = buf.length(); buf.append(timestamp); buf.setSpan(new AbsoluteSizeSpan(12), startOffset, buf.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // Make the timestamp text not as dark 改变某区域颜色 时间的地方为特殊颜色 int color = mContext.getResources().getColor(R.color.timestamp_color); buf.setSpan(new ForegroundColorSpan(color), startOffset, buf.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } if (highlight != null) { int highlightLen = highlight.length(); String s = buf.toString().toLowerCase(); int prev = 0; while (true) { int index = s.indexOf(highlight, prev); if (index == -1) { break; } buf.setSpan(new StyleSpan(Typeface.BOLD), index, index + highlightLen, 0); prev = index + highlightLen; } } return buf; } //------------------------------------------------------------ /** * Adds ImageSpans to a CharSequence that replace textual emoticons such * as :-) with a graphical version. * * @param text A CharSequence possibly containing emoticons * @return A CharSequence annotated with ImageSpans covering any * recognized emoticons. * 添加ImageSpans一个CharSequence的表情符号代替文字等 *如用图形版本:-)。 * 核心是把表情字符替换成ImageSpans的对象 */ public CharSequence addSmileySpans(CharSequence text) { SpannableStringBuilder builder = new SpannableStringBuilder(text); Matcher matcher = mPattern.matcher(text); while (matcher.find()) { int resId = mSmileyToRes.get(matcher.group()); //注意下面的一块有点不好理解哦但是是核心 builder.setSpan(new ImageSpan(mContext, resId), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } return builder; }
builder.setSpan(new ImageSpan(mContext, resId), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
原文出自:http://aichixihongshi.iteye.com/blog/1067222