package com.lcjian.library.emotion;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
import android.content.Context;
|
import android.content.res.TypedArray;
|
import android.graphics.Bitmap;
|
import android.graphics.BitmapFactory;
|
import android.text.Spannable;
|
import android.text.SpannableStringBuilder;
|
import android.text.TextUtils;
|
import android.util.AttributeSet;
|
import android.util.Log;
|
import android.widget.TextView;
|
|
import com.lcjian.lcjianlibrary.R;
|
|
public class EmotionTextView extends TextView {
|
|
private int mEmotionSize;
|
|
private int mEmotionWidth;
|
|
private int mEmotionHeight;
|
|
public EmotionTextView(Context context) {
|
super(context);
|
init(null);
|
}
|
|
public EmotionTextView(Context context, AttributeSet attrs) {
|
super(context, attrs);
|
init(attrs);
|
}
|
|
public EmotionTextView(Context context, AttributeSet attrs, int defStyle) {
|
super(context, attrs, defStyle);
|
init(attrs);
|
}
|
|
private void init(AttributeSet attrs) {
|
if (attrs == null) {
|
mEmotionSize = (int) getTextSize();
|
mEmotionWidth = mEmotionSize;
|
mEmotionHeight = mEmotionSize;
|
} else {
|
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.Emotion);
|
mEmotionSize = (int) a.getDimension(R.styleable.Emotion_emotionSize, getTextSize());
|
mEmotionWidth = (int) a.getDimension(R.styleable.Emotion_emotionWidth, 0);
|
mEmotionHeight = (int) a.getDimension(R.styleable.Emotion_emotionWidth, 0);
|
if (mEmotionWidth == 0 || mEmotionHeight == 0) {
|
mEmotionWidth = mEmotionSize;
|
mEmotionHeight = mEmotionSize;
|
}
|
a.recycle();
|
}
|
setText(getText());
|
}
|
|
@Override
|
public void setText(CharSequence text, BufferType type) {
|
if (!TextUtils.isEmpty(text)) {
|
SpannableStringBuilder builder = new SpannableStringBuilder(text);
|
Pattern p = Pattern.compile("\\[[^\\[\\]]+\\]");
|
Matcher m = p.matcher(text);
|
while(m.find()){
|
try {
|
InputStream input = getContext().getAssets().open("emotions" + File.separator + EmotionHandler.faceMap.get(m.group()));
|
if (input != null) {
|
Bitmap bitmap = BitmapFactory.decodeStream(input);
|
EmotionSpan emotionSpan = new EmotionSpan(getContext(), bitmap, mEmotionWidth, mEmotionHeight);
|
builder.setSpan(emotionSpan, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
}
|
} catch (IOException e) {
|
Log.e("sms", "Failed to loaded content " + m.group(), e);
|
}
|
}
|
text = builder;
|
}
|
super.setText(text, type);
|
}
|
|
/**
|
* Set the size of emojicon in pixels.
|
*/
|
public void setEmotionSize(int pixels) {
|
mEmotionSize = pixels;
|
mEmotionWidth = mEmotionSize;
|
mEmotionHeight = mEmotionSize;
|
super.setText(getText());
|
}
|
|
public void setEmotionWidth(int pixels) {
|
mEmotionWidth = pixels;
|
super.setText(getText());
|
}
|
|
public void setEmotionHeight(int pixels) {
|
mEmotionHeight = pixels;
|
super.setText(getText());
|
}
|
}
|