gpt4 book ai didi

android - EditText 上的 ImageSpan(笑脸)。使用 SwiftKey 键盘不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:11 24 4
gpt4 key购买 nike

我正在做一个简单的聊天应用程序,我想在编写消息时在 edittext 上显示笑脸。

我用它来识别哪些字符将通过 ImageSpan 由 Image 替换(仅当在 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 标签来设置跨度,但我在使用 swiftkey 键盘时遇到问题,因为当我在 edittext 中插入笑脸时,我在 imageSpan 之后写的所有内容都保持在图像下方(如 SPAN_EXCLUSIVE_INCLUSIVE)。使用 Android 默认键盘我没有这个问题。

我只希望 whatsapp 应用程序在 EditText 上有相同的行为和笑脸。

有什么建议吗?我必须对我的代码做任何更改吗?

编辑:“可编辑”变量被传递给方法。它是 txtMessage.getText() 值,其中 txtMessage 是一个 EditText。

谢谢!

编辑:只跨越一部分代码!这在多行中效果很好!我认为问题在于使用 Drawable->Bitmap->ResizedBitmap->Drawable。

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;
}

最佳答案

使用这个::

    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;
}

关于android - EditText 上的 ImageSpan(笑脸)。使用 SwiftKey 键盘不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11494054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com