admin
2024-01-26 c2d382d99ca506932985d1843d4371d6ed0203ff
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
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());
    }
}