TextView控件上添加表情图片
2014-08-19 21:42
357 查看
package org.face;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spanned;
import android.util.AttributeSet;
import android.widget.TextView;
public
class FaceTextView
extends TextView {
private CharSequence text;
public FaceTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public FaceTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public FaceTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public CharSequence getText() {
return text ==
null ?
"" : text;
}
@Override
public
void setText(CharSequence text, BufferType type) {
this.text = text;
String cs = text.toString();
// 对表情符以img标记进行修饰,改用drawable显示出来
if (cs.contains(":-{1}quot;)) {
cs = cs.replace(":-{1}quot;, "<img src=\"confused\"/>");
}
Spanned span = Html.fromHtml(cs, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = null;
String sourceName = getContext().getPackageName() +
":drawable/"
+ source;
int id = getResources().getIdentifier(sourceName,
null,
null);
if (id !=
0) {
drawable = getResources().getDrawable(id);
if (drawable !=
null) {
drawable.setBounds(0,
0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
}
}
return drawable;
}
}, null);
super.setText(span, type);
}
}
布局文件main.xml
[html]
view plaincopyprint?
<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<org.face.FaceTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第六个:-$笑脸"/>
</LinearLayout>
=>有人也采用SpannableString和ImageSpan类实现了添加图片的效果
这里我将它也贴上
你可以将上面FaceTextView类里面的setText(CharSequence text, BufferType type)方法内容替换下
[java]
view plaincopyprint?
Drawable drawable = getResources().getDrawable(R.drawable.confused);
drawable.setBounds(0,
0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
// 需要处理的文本,[smile]是需要被替代的文本
SpannableString spannable = new SpannableString(getText().toString()+"[smile]");
// 要让图片替代指定的文字就要用ImageSpan
ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
// 开始替换,注意第2和第3个参数表示从哪里开始替换到哪里替换结束(start和end)
// 最后一个参数类似数学中的集合,[5,12)表示从5到12,包括5但不包括12
spannable.setSpan(span, getText().length(),getText().length()+"[smile]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
super.setText(spannable, type);
上面两种方式的缺陷是:gif图片显示出来的是个静态的,待研究.
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spanned;
import android.util.AttributeSet;
import android.widget.TextView;
public
class FaceTextView
extends TextView {
private CharSequence text;
public FaceTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public FaceTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public FaceTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public CharSequence getText() {
return text ==
null ?
"" : text;
}
@Override
public
void setText(CharSequence text, BufferType type) {
this.text = text;
String cs = text.toString();
// 对表情符以img标记进行修饰,改用drawable显示出来
if (cs.contains(":-{1}quot;)) {
cs = cs.replace(":-{1}quot;, "<img src=\"confused\"/>");
}
Spanned span = Html.fromHtml(cs, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = null;
String sourceName = getContext().getPackageName() +
":drawable/"
+ source;
int id = getResources().getIdentifier(sourceName,
null,
null);
if (id !=
0) {
drawable = getResources().getDrawable(id);
if (drawable !=
null) {
drawable.setBounds(0,
0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
}
}
return drawable;
}
}, null);
super.setText(span, type);
}
}
package org.face; import android.content.Context; import android.graphics.drawable.Drawable; import android.text.Html; import android.text.Spanned; import android.util.AttributeSet; import android.widget.TextView; public class FaceTextView extends TextView { private CharSequence text; public FaceTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public FaceTextView(Context context) { super(context); // TODO Auto-generated constructor stub } public FaceTextView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public CharSequence getText() { return text == null ? "" : text; } @Override public void setText(CharSequence text, BufferType type) { this.text = text; String cs = text.toString(); // 对表情符以img标记进行修饰,改用drawable显示出来 if (cs.contains(":-{1}quot;)) { cs = cs.replace(":-{1}quot;, "<img src=\"confused\"/>"); } Spanned span = Html.fromHtml(cs, new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { Drawable drawable = null; String sourceName = getContext().getPackageName() + ":drawable/" + source; int id = getResources().getIdentifier(sourceName, null, null); if (id != 0) { drawable = getResources().getDrawable(id); if (drawable != null) { drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); } } return drawable; } }, null); super.setText(span, type); } }这种方式是将text中字符替换成HTML标签,采用Html.fromHtml去解析.
布局文件main.xml
[html]
view plaincopyprint?
<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<org.face.FaceTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第六个:-$笑脸"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFF"> <org.face.FaceTextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第六个:-$笑脸"/> </LinearLayout>上面代码需要看到效果需要有张confused的图片。
=>有人也采用SpannableString和ImageSpan类实现了添加图片的效果
这里我将它也贴上
你可以将上面FaceTextView类里面的setText(CharSequence text, BufferType type)方法内容替换下
[java]
view plaincopyprint?
Drawable drawable = getResources().getDrawable(R.drawable.confused);
drawable.setBounds(0,
0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
// 需要处理的文本,[smile]是需要被替代的文本
SpannableString spannable = new SpannableString(getText().toString()+"[smile]");
// 要让图片替代指定的文字就要用ImageSpan
ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
// 开始替换,注意第2和第3个参数表示从哪里开始替换到哪里替换结束(start和end)
// 最后一个参数类似数学中的集合,[5,12)表示从5到12,包括5但不包括12
spannable.setSpan(span, getText().length(),getText().length()+"[smile]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
super.setText(spannable, type);
Drawable drawable = getResources().getDrawable(R.drawable.confused); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); // 需要处理的文本,[smile]是需要被替代的文本 SpannableString spannable = new SpannableString(getText().toString()+"[smile]"); // 要让图片替代指定的文字就要用ImageSpan ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE); // 开始替换,注意第2和第3个参数表示从哪里开始替换到哪里替换结束(start和end) // 最后一个参数类似数学中的集合,[5,12)表示从5到12,包括5但不包括12 spannable.setSpan(span, getText().length(),getText().length()+"[smile]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); super.setText(spannable, type);
上面两种方式的缺陷是:gif图片显示出来的是个静态的,待研究.
![](http://hi.csdn.net/attachment/201107/18/893577_13109835470F6p.png)
相关文章推荐
- TextView控件上添加表情图片
- android---textview控件学习笔记之显示表情图片和文本(二)
- Android之TextView、EditText控件显示表情图片
- android之在EditText、TextView中添加表情图片
- Android控件TextView显示html文件中的图片
- 在EditText/TextView中插入表情图片、样式、下划线等
- Android控件TextView显示html文件中的图片
- android如何在textview或edittext上添加表情
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- android_常用UI控件_02_EditText_01添加图片到edittext中
- android Button,textView 等控件设置控件内嵌周围图片使用方法
- android如果重写onDraw实现一个类似TextView可以显示表情和链接的控件(二)
- 在EditText/TextView中插入表情图片、样式、下划线等(转)
- TextView和EditText中添加图片(ImageSpan)
- 在TextView中添加图片
- 动态添加的控件(如TextView)如何居中?如何相对对齐?
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- 在EditText和TextView显示表情图片(类似QQ表情)
- Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标