Skip to content

Instantly share code, notes, and snippets.

@vvakame
Created December 16, 2012 10:22
Show Gist options
  • Save vvakame/4306120 to your computer and use it in GitHub Desktop.
Save vvakame/4306120 to your computer and use it in GitHub Desktop.

Revisions

  1. vvakame revised this gist Dec 16, 2012. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions EmojiView.java
    Original file line number Diff line number Diff line change
    @@ -19,23 +19,23 @@

    public class EmojiView extends View {

    static final String TAG = RandomEmojiView.class.getSimpleName();
    static final String TAG = RandomEmojiView.class.getSimpleName();

    List<String> mEmojiList;
    List<String> mEmojiList;

    Paint mPaint;

    public RandomEmojiView(Context context) {
    public EmojiView(Context context) {
    super(context);
    init();
    }

    public RandomEmojiView(Context context, AttributeSet attrs) {
    public EmojiView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    }

    public RandomEmojiView(Context context, AttributeSet attrs, int defStyle) {
    public EmojiView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
    }
    @@ -47,7 +47,6 @@ void init() {
    mPaint.setTextSize(20);
    mPaint.setAntiAlias(true);

    int emojiCount = 0;
    mEmojiList = new ArrayList<String>();
    try {
    // EmojiSources.txt by
    @@ -75,7 +74,6 @@ void init() {
    String s = new String(new int[] { c1, c2 }, 0, 2);
    mEmojiList.add(s);
    }
    emojiCount++;
    }
    } catch (IOException e) {
    throw new RuntimeException(e);
  2. vvakame created this gist Dec 16, 2012.
    103 changes: 103 additions & 0 deletions EmojiView.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    package net.vvakame.sample;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.View;

    public class EmojiView extends View {

    static final String TAG = RandomEmojiView.class.getSimpleName();

    List<String> mEmojiList;

    Paint mPaint;

    public RandomEmojiView(Context context) {
    super(context);
    init();
    }

    public RandomEmojiView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    }

    public RandomEmojiView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
    }

    void init() {
    mPaint = new Paint();
    mPaint.setColor(Color.argb(0xff, 0xff, 0x00, 0x00));
    mPaint.setStrokeWidth(1);
    mPaint.setTextSize(20);
    mPaint.setAntiAlias(true);

    int emojiCount = 0;
    mEmojiList = new ArrayList<String>();
    try {
    // EmojiSources.txt by
    // http://www.unicode.org/Public/6.0.0/ucd/EmojiSources.txt
    InputStream is = getContext().getAssets().open("EmojiSources.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line;
    Pattern p = Pattern.compile("^([0-9a-fA-F]+\\s{0,1}[0-9a-fA-F]*);");
    while ((line = br.readLine()) != null) {
    Matcher m = p.matcher(line);
    if (!m.find()) {
    continue;
    }
    Log.d(TAG, m.group(1));
    String codePoint = m.group(1);
    String[] splited = codePoint.split(" ");
    if (splited.length == 1) {
    int c = Integer.parseInt(splited[0], 16);
    String s = new String(new int[] { c }, 0, 1);
    mEmojiList.add(s);
    } else {
    int c1 = Integer.parseInt(splited[0], 16);
    int c2 = Integer.parseInt(splited[1], 16);
    String s = new String(new int[] { c1, c2 }, 0, 2);
    mEmojiList.add(s);
    }
    emojiCount++;
    }
    } catch (IOException e) {
    throw new RuntimeException(e);
    }
    }

    @Override
    protected void onDraw(Canvas canvas) {
    final int delta = 35;
    int x = 0;
    int y = delta;

    for (String emoji : mEmojiList) {
    canvas.drawRect(x, y, x + 1, y + 1, mPaint);
    canvas.drawText(emoji, x, y, mPaint);

    if (delta * 22 < x) {
    x = 0;
    y += delta;
    } else {
    x += delta;
    }
    }
    }
    }