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.
Android 4.1 JellyBeanで絵文字が追加されたけど、うまく表示されなかったん。 ちゃんと動くようになったのでここにメモっておく。
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 EmojiView(Context context) {
super(context);
init();
}
public EmojiView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public EmojiView(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);
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);
}
}
} 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;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment