forked from vanniktech/Emoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmojiTextView.java
77 lines (62 loc) · 2.87 KB
/
EmojiTextView.java
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
package com.vanniktech.emoji;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import androidx.annotation.CallSuper;
import androidx.annotation.DimenRes;
import androidx.annotation.Px;
import androidx.appcompat.widget.AppCompatTextView;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
@SuppressWarnings("CPD-START") public class EmojiTextView extends AppCompatTextView {
private float emojiSize;
public EmojiTextView(final Context context) {
this(context, null);
}
public EmojiTextView(final Context context, final AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode()) {
EmojiManager.getInstance().verifyInstalled();
}
final Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
final float defaultEmojiSize = fontMetrics.descent - fontMetrics.ascent;
if (attrs == null) {
emojiSize = defaultEmojiSize;
} else {
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.EmojiTextView);
try {
emojiSize = a.getDimension(R.styleable.EmojiTextView_emojiSize, defaultEmojiSize);
} finally {
a.recycle();
}
}
setText(getText());
}
@Override @CallSuper public void setText(final CharSequence rawText, final BufferType type) {
final CharSequence text = rawText == null ? "" : rawText;
final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
final Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
final float defaultEmojiSize = fontMetrics.descent - fontMetrics.ascent;
EmojiManager.getInstance().replaceWithImages(getContext(), spannableStringBuilder, emojiSize, defaultEmojiSize);
super.setText(spannableStringBuilder, type);
}
/** sets the emoji size in pixels and automatically invalidates the text and renders it with the new size */
public final void setEmojiSize(@Px final int pixels) {
setEmojiSize(pixels, true);
}
/** sets the emoji size in pixels and automatically invalidates the text and renders it with the new size when {@code shouldInvalidate} is true */
public final void setEmojiSize(@Px final int pixels, final boolean shouldInvalidate) {
emojiSize = pixels;
if (shouldInvalidate) {
setText(getText());
}
}
/** sets the emoji size in pixels with the provided resource and automatically invalidates the text and renders it with the new size */
public final void setEmojiSizeRes(@DimenRes final int res) {
setEmojiSizeRes(res, true);
}
/** sets the emoji size in pixels with the provided resource and invalidates the text and renders it with the new size when {@code shouldInvalidate} is true */
public final void setEmojiSizeRes(@DimenRes final int res, final boolean shouldInvalidate) {
setEmojiSize(getResources().getDimensionPixelSize(res), shouldInvalidate);
}
}