Skip to content

Commit

Permalink
handle emoji density scaling more correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
mcginty committed Feb 26, 2014
1 parent a0aaa7d commit 630dce0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="emoji_size">64dip</dimen>
<dimen name="emoji_drawer_size">40dip</dimen>
<dimen name="conversation_item_corner_radius">3dp</dimen>
<dimen name="conversation_item_drop_shadow_dist">2dp</dimen>
</resources>
8 changes: 5 additions & 3 deletions src/org/thoughtcrime/securesms/components/EmojiDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
Expand All @@ -35,7 +36,6 @@ public class EmojiDrawer extends FrameLayout {
private GridView emojiGrid;
private GridView recentEmojiGrid;
private ViewPager pager;
private PagerTabStrip pagerTabStrip;

public EmojiDrawer(Context context) {
super(context);
Expand Down Expand Up @@ -70,8 +70,7 @@ private void initialize() {

private void initializeResources() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.pager = (ViewPager ) findViewById(R.id.emoji_pager );
this.pagerTabStrip = (PagerTabStrip) findViewById(R.id.emoji_tab_strip);
this.pager = (ViewPager ) findViewById(R.id.emoji_pager);
this.emojiGridLayout = (FrameLayout ) inflater.inflate(R.layout.emoji_grid_layout, null);
this.recentEmojiGridLayout = (FrameLayout ) inflater.inflate(R.layout.emoji_grid_layout, null);
this.emojiGrid = (GridView ) emojiGridLayout.findViewById(R.id.emoji);
Expand Down Expand Up @@ -128,9 +127,11 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
private class EmojiGridAdapter extends BaseAdapter {

private final int type;
private final int emojiSize;

public EmojiGridAdapter(int type) {
this.type = type;
emojiSize = (int) getResources().getDimension(R.dimen.emoji_drawer_size);
}

@Override
Expand Down Expand Up @@ -162,6 +163,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
return convertView;
} else {
ImageView imageView = new ImageView(getContext());
imageView.setLayoutParams(new AbsListView.LayoutParams(emojiSize, emojiSize));
imageView.setImageDrawable(drawable);
return imageView;
}
Expand Down
21 changes: 15 additions & 6 deletions src/org/thoughtcrime/securesms/util/Emoji.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.DisplayMetrics;
import android.util.Log;

import com.google.thoughtcrimegson.Gson;
Expand Down Expand Up @@ -45,22 +46,30 @@ public synchronized static Emoji getInstance(Context context) {

private static final Pattern EMOJI_RANGE = Pattern.compile("[\ud83d\ude01-\ud83d\ude4f]");
public static final double EMOJI_LARGE = 1;
public static final double EMOJI_SMALL = 0.7;
public static final double EMOJI_SMALL = 0.75;
public static final int EMOJI_LARGE_SIZE = 22;

private final Context context;
private final String[] emojiAssets;
private final Set<String> emojiAssetsSet;
private final BitmapFactory.Options bitmapOptions;
private final int bigDrawSize;

private Emoji(Context context) {
this.context = context.getApplicationContext();
this.emojiAssets = initializeEmojiAssets();
this.emojiAssetsSet = new HashSet<String>();
this.bitmapOptions = initializeBitmapOptions();

this.bigDrawSize = scale(EMOJI_LARGE_SIZE);

Collections.addAll(this.emojiAssetsSet, emojiAssets);
}

private int scale(float value) {
return (int)(context.getResources().getDisplayMetrics().density * value);
}

public int getEmojiAssetCount() {
return emojiAssets.length;
}
Expand Down Expand Up @@ -91,8 +100,8 @@ public SpannableString emojify(SpannableString text, double size) {

if (emojiAssetsSet.contains(resource)) {
Drawable drawable = getEmojiDrawable(resource);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*size),
(int)(drawable.getIntrinsicHeight()*size));
drawable.setBounds(0, 0, (int)(bigDrawSize*size),
(int)(bigDrawSize*size));

ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
text.setSpan(imageSpan, matches.start(), matches.end(),
Expand Down Expand Up @@ -129,7 +138,7 @@ private Drawable getEmojiDrawable(String assetName) {
Bitmap bitmap = BitmapFactory.decodeStream(context.getAssets().open("emoji" + File.separator + assetName),
null, bitmapOptions);

bitmap = Bitmap.createScaledBitmap(bitmap, 40, 40, true);
bitmap = Bitmap.createScaledBitmap(bitmap, 64, 64, true);

return new BitmapDrawable(context.getResources(), bitmap);
} catch (IOException e) {
Expand All @@ -150,8 +159,8 @@ 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.inDensity = DisplayMetrics.DENSITY_MEDIUM;
options.inTargetDensity = context.getResources().getDisplayMetrics().densityDpi;
options.inSampleSize = 1;
options.inJustDecodeBounds = false;

Expand Down

1 comment on commit 630dce0

@WhisperBTC
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! BitHub has sent payment of $96.01USD for this commit.

Please sign in to comment.