display emoji correctly on devices of all densities

This commit is contained in:
Moxie Marlinspike 2013-07-15 17:31:24 -07:00
parent c827f0a2a7
commit 4281df7a28
2 changed files with 32 additions and 4 deletions

4
res/values/dimens.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="emoji_size">64dip</dimen>
</resources>

View File

@ -2,6 +2,9 @@ package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.text.Spannable;
@ -12,6 +15,8 @@ import android.util.Log;
import com.google.thoughtcrimegson.Gson;
import com.google.thoughtcrimegson.reflect.TypeToken;
import org.thoughtcrime.securesms.R;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
@ -42,14 +47,16 @@ public class Emoji {
public static final double EMOJI_LARGE = 1;
public static final double EMOJI_SMALL = 0.7;
private final Context context;
private final String[] emojiAssets;
private final Set<String> emojiAssetsSet;
private final Context context;
private final String[] emojiAssets;
private final Set<String> emojiAssetsSet;
private final BitmapFactory.Options bitmapOptions;
private Emoji(Context context) {
this.context = context.getApplicationContext();
this.emojiAssets = initializeEmojiAssets();
this.emojiAssetsSet = new HashSet<String>();
this.bitmapOptions = initializeBitmapOptions();
Collections.addAll(this.emojiAssetsSet, emojiAssets);
}
@ -119,7 +126,12 @@ public class Emoji {
private Drawable getEmojiDrawable(String assetName) {
try {
return Drawable.createFromStream(context.getAssets().open("emoji" + File.separator + assetName), null);
Bitmap bitmap = BitmapFactory.decodeStream(context.getAssets().open("emoji" + File.separator + assetName),
null, bitmapOptions);
bitmap = Bitmap.createScaledBitmap(bitmap, 40, 40, true);
return new BitmapDrawable(context.getResources(), bitmap);
} catch (IOException e) {
throw new AssertionError(e);
}
@ -134,6 +146,18 @@ public class Emoji {
}
}
private BitmapFactory.Options initializeBitmapOptions() {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true;
// options.inDensity = 64;
options.inTargetDensity = context.getResources().getDimensionPixelSize(R.dimen.emoji_size);
options.inSampleSize = 1;
options.inJustDecodeBounds = false;
return options;
}
private static class EmojiLRU {
private static final String EMOJI_LRU_PREFERENCE = "pref_popular_emoji";