Created
January 17, 2014 01:39
-
-
Save jinsnet/8467025 to your computer and use it in GitHub Desktop.
String to emoticon image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public SpannableString changeToEmoticon(Context context, String text) | |
{ | |
SpannableString result = new SpannableString(text); | |
Pattern pattern = Pattern.compile("\\((.*?)\\)"); // 괄호안의 문자를 가져오는 정규식 | |
Matcher match = pattern.matcher(text); | |
while(match.find()) // 문장에 포함된 모든 괄호 문자를 이미지로 변경 | |
{ | |
int start = match.start(); | |
int end = match.end(); | |
int path = DBPool.getInstance(context).getImagePath(match.group(1)); | |
String temp = match.group(1); // temp가 괄호안 문자를 참조 | |
while(temp.contains("(")) // 괄호안에 괄호가 있는 경우에 앞의 괄호들 제거 | |
{ | |
start = start + temp.indexOf("(")+1; | |
temp = temp.substring(temp.indexOf("(")+1); | |
} | |
path = DBPool.getInstance(context).getImagePath(temp); //해당 문자가 DB에 존재하는 문자인지 확인 | |
if(path != 0) | |
{ | |
Drawable drawable = context.getResources().getDrawable(context.getResources().getIdentifier(String.format("sticker_img_%d", path), "drawable", context.getPackageName())); | |
drawable.setBounds(0, 0, drawable.getIntrinsicWidth()/2, drawable.getIntrinsicHeight()/2); | |
result.setSpan(new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); // ImageSpan을 이용한 이미지 변경 | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment