我正在做一个简单的聊天应用程序,我想在编写消息时在编辑文本上显示笑脸。
我要通过ImageSpan来识别哪些字符将被图像所替代(只有在EditText上插入一个笑脸字符时才会调用该字符):
for (index = start; index < start+num_chars; index++) {
if (index + 1 > editable.length())
continue;
if(emoticons.containsKey(editable.subSequence(index, index + 1).toString())){
int length=1;
Drawable drawable = context.getResources().getDrawable(emoticons.get(editable.subSequence(index, index + 1).toString()));
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
int size=Utils.GetDipsFromPixel(context, (int)(textSize*1.3));
Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, size, size, true));
int dWidth = d.getIntrinsicWidth();
int dHeight = d.getIntrinsicHeight();
d.setBounds(0 , -dHeight, dWidth, 0);
ImageSpan span;
span = new ImageSpan(d,ImageSpan.ALIGN_BASELINE);
editable.setSpan(span, index, index + length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
}
}我使用SPAN_EXCLUSIVE_EXCLUSIVE标记来设置span,但是swiftkey键盘有问题,因为当我在编辑文本中插入一个笑脸时,我在imageSpan之后写的所有东西都保持在图像下面(比如SPAN_EXCLUSIVE_INCLUSIVE)。使用Android默认键盘,我就没有这个问题了。
我只希望whatsapp应用程序在EditText上具有相同的微笑行为。
有什么建议吗?我的代码有什么变化吗?
编辑:“可编辑”变量传递给方法。它是txtMessage.getText()值,其中txtMessage是EditText。
谢谢!
编辑:只跨越一部分代码!这在多行中很好!我认为问题是在使用可绘制->位图->ResizedBitmap->绘图。
public static final HashMap<String, Integer> emoticons = new HashMap();
static {
emoticons.put("\ue415", R.drawable.e415);
emoticons.put("\ue056", R.drawable.e056);
emoticons.put("\ue057", R.drawable.e057);
...
public static Spannable getSmiledText(Context context, Spannable editable,
int start, int num_chars, float textSize) {
int index;
for (index = start; index < start + num_chars; index++) {
if (index + 1 > editable.length())
continue;
if (EmojiLayout.emoticons.containsKey(editable.subSequence(index,
index + 1).toString())) {
int length = 1;
Bitmap smiley = BitmapFactory.decodeResource(context.getResources(), ((Integer) EmojiLayout.emoticons.get(editable.subSequence(index,
index + 1).toString())));
int size = Utils.GetDipsFromPixel(context,
(int) (textSize * 1.37));
Bitmap scaledbmp=Bitmap.createScaledBitmap(
smiley, size, size, false);
ImageSpan span;
span = new ImageSpan(scaledbmp);
Log.d("EmojiLayout", "Index: " + String.valueOf(index) + "To: "
+ String.valueOf(index + length));
editable.setSpan(span, index, index + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
}
}
return editable;
}发布于 2012-07-31 06:48:05
使用这个::
public static CharSequence addSmileySpans(Context ch, CharSequence your_recieved_message)
{
//smilyRegexMap = new HashMap<Integer, String>();
private static final HashMap<String, Integer> smilyRegexMap = new HashMap<String, Integer>();
smilyRegexMap.put( ">:-\\(" , R.drawable.fb_grumpy);
smilyRegexMap.put( ">:\\(" , R.drawable.fb_grumpy);
smilyRegexMap.put( ">:-O" , R.drawable.fb_upset);
smilyRegexMap.put( ":-\\)" , R.drawable.fb_smile);
smilyRegexMap.put( ":\\)",R.drawable.fb_smile);
smilyRegexMap.put( ":-\\]" , R.drawable.fb_smile);
smilyRegexMap.put( ":-\\(", R.drawable.fb_frown);
System.out.println("==In Spannable Function..........");
SpannableStringBuilder builder = new SpannableStringBuilder(your_recieved_message);
System.out.println("==================Size of Smily : "+ smilyRegexMap.size());
@SuppressWarnings("rawtypes")
Iterator it = smilyRegexMap.entrySet().iterator();
while (it.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry pairs = (Map.Entry) it.next();
Pattern mPattern = Pattern.compile((String) pairs.getKey(),Pattern.CASE_INSENSITIVE);
Matcher matcher = mPattern.matcher(your_recieved_message);
while (matcher.find()) {
Bitmap smiley = BitmapFactory.decodeResource(ch.getResources(), ((Integer) pairs.getValue()));
Object[] spans = builder.getSpans(matcher.start(), matcher.end(), ImageSpan.class);
if (spans == null || spans.length == 0) {
builder.setSpan(new ImageSpan(smiley), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return builder;
}发布于 2012-07-15 17:35:55
您只需使用ImageSpan构建一个可扩展文本,然后按照CommonsWare在这个post中的建议,构建一个可扩展到TextView或EditText的可扩展文本。您还可以尝试使用A-IV's解决方案:
private static final HashMap<String, Integer> emoticons = new HashMap();
static {
emoticons.put(":*", R.drawable.emo_im_kiss);
emoticons.put(":-D", R.drawable.emo_im_glad);
emoticons.put(":)", R.drawable.emo_im_happy);
emoticons.put(":-(", R.drawable.emo_im_sad);
...
}
public static Spannable getSmiledText(Context context, String text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
int index;
for (index = 0; index < builder.length(); index++) {
for (Entry<String, Integer> entry : emoticons.entrySet()) {
int length = entry.getKey().length();
if (index + length > builder.length())
continue;
if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) {
builder.setSpan(new ImageSpan(context, entry.getValue()), index, index + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
break;
}
}
}
return builder;
}发布于 2014-07-21 04:57:54
试着这样做,希望这能帮助您解决问题。
private static LinkedHashMap<String,Integer> emojisHashMap;
public static LinkedHashMap<String, Integer> getEmojisHashMap() {
if (emojisHashMap == null || emojisHashMap.size() == 0) {
emojisHashMap = new LinkedHashMap<String, Integer>();
emojisHashMap.put(":=q", R.drawable.smiley1);
emojisHashMap.put(":=w", R.drawable.smiley2);
emojisHashMap.put(":=e", R.drawable.smiley3);
emojisHashMap.put(":=r", R.drawable.smiley4);
return emojisHashMap;
} else {
return emojisHashMap;
}
}
public Spannable getSmiledText(CharSequence text) {
SpannableStringBuilder builder;
try {
builder = (SpannableStringBuilder) text;
}catch (Exception e){
builder = new SpannableStringBuilder(text);
}
if (getEmojisHashMap().size() > 0) {
int index;
for (index = 0; index < builder.length(); index++) {
if (Character.toString(builder.charAt(index)).equals(":")) {
for (Map.Entry<String, Integer> entry : getEmojisHashMap().entrySet()) {
int length = entry.getKey().length();
if (index + length > builder.length())
continue;
if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) {
builder.setSpan(new ImageSpan(getContext(), entry.getValue()), index, index + length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
break;
}
}
}
}
}
return builder;
}https://stackoverflow.com/questions/11494054
复制相似问题