admin
2020-08-06 7af5470c3badafb043d417cc37996e4992881ce7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;
 
import com.lcjian.lcjianlibrary.R;
 
public class EmotionEditText extends EditText {
    
    private int mEmotionSize;
    
    private int mEmotionWidth;
    
    private int mEmotionHeight;
    
    public EmotionEditText(Context context) {
        super(context);
        init(null);
        mEmotionSize = (int) getTextSize();
    }
 
    public EmotionEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }
 
    public EmotionEditText(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
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        updateText();
    }
 
    /**
     * Set the size of emotion in pixels.
     */
    public void setEmotionSize(int pixels) {
        mEmotionSize = pixels;
        mEmotionWidth = mEmotionSize;
        mEmotionHeight = mEmotionSize;
        updateText();
    }
    
    public void setEmotionWidth(int pixels) {
        mEmotionWidth = pixels;
        updateText();
    }
    
    public void setEmotionHeight(int pixels) {
        mEmotionHeight = pixels;
        updateText();
    }
 
    private void updateText() {
        Spannable text = getText();
        EmotionSpan[] oldSpans = text.getSpans(0, text.length(), EmotionSpan.class);
        for (int i = 0; i < oldSpans.length; i++) {
            text.removeSpan(oldSpans[i]);
        }
        
        Pattern p = Pattern.compile("\\[[^\\[\\]]+\\]");
        Matcher m = p.matcher(getText());
        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 imageSpan = new EmotionSpan(getContext(), bitmap, mEmotionWidth, mEmotionHeight);
                    text.setSpan(imageSpan, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            } catch (IOException e) {
                Log.e("sms", "Failed to loaded content " + m.group(), e);
            }
        }
    }
}