2015-05-21 17:29:23 -07:00
|
|
|
package org.thoughtcrime.securesms.components.emoji;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
|
|
|
|
2015-06-08 17:54:16 +02:00
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.util.ResUtil;
|
2015-05-21 17:29:23 -07:00
|
|
|
|
|
|
|
public class EmojiView extends View implements Drawable.Callback {
|
|
|
|
private String emoji;
|
|
|
|
private Drawable drawable;
|
|
|
|
|
2015-07-06 14:05:18 -07:00
|
|
|
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
|
2015-05-21 17:29:23 -07:00
|
|
|
private final Rect textBounds = new Rect();
|
|
|
|
|
|
|
|
public EmojiView(Context context) {
|
2015-07-07 14:25:41 -07:00
|
|
|
this(context, null);
|
2015-05-21 17:29:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public EmojiView(Context context, AttributeSet attrs) {
|
2015-07-07 14:25:41 -07:00
|
|
|
this(context, attrs, 0);
|
2015-05-21 17:29:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public EmojiView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEmoji(String emoji) {
|
|
|
|
this.emoji = emoji;
|
|
|
|
this.drawable = EmojiProvider.getInstance(getContext())
|
2015-07-10 14:58:05 -07:00
|
|
|
.getEmojiDrawable(Character.codePointAt(emoji, 0));
|
2015-05-21 17:29:23 -07:00
|
|
|
postInvalidate();
|
|
|
|
}
|
|
|
|
|
2015-07-07 14:25:41 -07:00
|
|
|
public String getEmoji() {
|
|
|
|
return emoji;
|
|
|
|
}
|
|
|
|
|
2015-05-21 17:29:23 -07:00
|
|
|
@Override protected void onDraw(Canvas canvas) {
|
|
|
|
if (drawable != null) {
|
|
|
|
drawable.setBounds(getPaddingLeft(),
|
|
|
|
getPaddingTop(),
|
|
|
|
getWidth() - getPaddingRight(),
|
|
|
|
getHeight() - getPaddingBottom());
|
|
|
|
drawable.setCallback(this);
|
|
|
|
drawable.draw(canvas);
|
|
|
|
} else {
|
|
|
|
float targetFontSize = 0.75f * getHeight() - getPaddingTop() - getPaddingBottom();
|
|
|
|
paint.setTextSize(targetFontSize);
|
2015-06-08 17:54:16 +02:00
|
|
|
paint.setColor(ResUtil.getColor(getContext(), R.attr.emoji_text_color));
|
2015-05-21 17:29:23 -07:00
|
|
|
paint.getTextBounds(emoji, 0, emoji.length(), textBounds);
|
|
|
|
float overflow = textBounds.width() / (getWidth() - getPaddingLeft() - getPaddingRight());
|
|
|
|
if (overflow > 1f) {
|
|
|
|
paint.setTextSize(targetFontSize / overflow);
|
|
|
|
}
|
|
|
|
canvas.drawText(emoji, 0.5f * (getWidth() - textBounds.width()), 0.5f * (getHeight() + textBounds.height()), paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override public void invalidateDrawable(@NonNull Drawable drawable) {
|
|
|
|
super.invalidateDrawable(drawable);
|
|
|
|
postInvalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
|
|
}
|
|
|
|
}
|