Skip to content

Commit

Permalink
Fix alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
hani-momanii committed Apr 5, 2017
1 parent 2828f6a commit 3849ee1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 35 deletions.
55 changes: 29 additions & 26 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,62 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:emojicon="http://schemas.android.com/apk/res-auto"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#ffffff"
tools:context="momanii.hani.supernova_emoji.MainActivity">
xmlns:emojicon="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="momanii.hani.supernova_emoji.MainActivity">

<ImageView
android:id="@+id/emoji_btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="4dp"
android:src="@mipmap/smiley"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:padding="4dp"
android:src="@mipmap/smiley"
/>

<ImageView
android:id="@+id/submit_btn"
android:src="@android:drawable/ic_menu_send"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="4dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:padding="4dp"
android:src="@android:drawable/ic_menu_send"
/>

<hani.momanii.supernova_emoji_library.Helper.EmojiconEditText
android:id="@+id/emojicon_edit_text"
android:layout_width="match_parent"
android:imeOptions="actionSend"
android:inputType="text"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/emoji_btn"
android:layout_toLeftOf="@id/submit_btn"
emojicon:emojiconSize="28sp" />

android:layout_toRightOf="@id/emoji_btn"
android:imeOptions="actionSend"
android:inputType="text"
emojicon:emojiconSize="28sp"/>


<CheckBox
android:id="@+id/use_system_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use System Default?"
android:id="@+id/use_system_default"
android:checked="false"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
android:layout_centerHorizontal="true"
android:checked="false"
android:text="Use System Default?"/>

<hani.momanii.supernova_emoji_library.Helper.EmojiconTextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="26dp"
android:lineSpacingExtra="17sp"
android:text="Hello Emojis !"
android:id="@+id/textView"
android:textColor="#000000"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp" />
android:textColor="#000000"
emojicon:emojiconAlignment="bottom"/>
</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,8 @@ public static void addEmojis(Context context, Spannable text, int emojiSize, int
* @param textSize
* @param useSystemDefault
*/
public static void addEmojis(Context context, Spannable text, int emojiSize, int emojiAlignment, int textSize, boolean useSystemDefault) {
public static void addEmojis(Context context, Spannable text, int emojiSize, int emojiAlignment,
int textSize, boolean useSystemDefault) {
addEmojis(context, text, emojiSize, emojiAlignment, textSize, 0, -1, useSystemDefault);
}

Expand Down Expand Up @@ -1222,7 +1223,7 @@ public static void addEmojis(Context context, Spannable text, int emojiSize, int
}

if (icon > 0) {
text.setSpan(new EmojiconSpan(context, icon, emojiSize), i, i + skip, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new EmojiconSpan(context, icon, emojiSize, emojiAlignment, textSize), i, i + skip, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,82 @@
package hani.momanii.supernova_emoji_library.Helper;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.style.DynamicDrawableSpan;

import java.lang.ref.WeakReference;

/**
* @author Hieu Rocker ([email protected])
* @author Hani Al Momani ([email protected])
*/

class EmojiconSpan extends DynamicDrawableSpan {
private final Context mContext;

private final int mResourceId;

private final int mSize;

private final int mTextSize;

private int mHeight;

private int mWidth;

private int mTop;

private Drawable mDrawable;

public EmojiconSpan(Context context, int resourceId, int size) {
super();
private WeakReference<Drawable> mDrawableRef;

public EmojiconSpan(Context context, int resourceId, int size, int alignment, int textSize) {
super(alignment);
mContext = context;
mResourceId = resourceId;
mSize = size;
mWidth = mHeight = mSize = size;
mTextSize = textSize;
}

public Drawable getDrawable() {
if (mDrawable == null) {
try {
mDrawable = mContext.getResources().getDrawable(mResourceId);
int size = mSize;
mDrawable.setBounds(0, 0, size, size);
mHeight = mSize;
mWidth = mHeight * mDrawable.getIntrinsicWidth() / mDrawable.getIntrinsicHeight();
mTop = (mTextSize - mHeight) / 2;
mDrawable.setBounds(0, mTop, mWidth, mTop + mHeight);
} catch (Exception e) {

// swallow
}
}
return mDrawable;
}

@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int
y, int bottom, Paint paint) {
//super.draw(canvas, text, start, end, x, top, y, bottom, paint);
Drawable b = getCachedDrawable();
canvas.save();

int transY = bottom - b.getBounds().bottom;
if (mVerticalAlignment == ALIGN_BASELINE) {
transY = top + ((bottom - top) / 2) - ((b.getBounds().bottom - b.getBounds().top) /
2) - mTop;
}

canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}

private Drawable getCachedDrawable() {
if (mDrawableRef == null || mDrawableRef.get() == null) {
mDrawableRef = new WeakReference<Drawable>(getDrawable());
}
return mDrawableRef.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void init(AttributeSet attrs) {
} else {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.Emojicon);
mEmojiconSize = (int) a.getDimension(R.styleable.Emojicon_emojiconSize, getTextSize());
mEmojiconAlignment = a.getInt(R.styleable.Emojicon_emojiconAlignment, DynamicDrawableSpan.ALIGN_BASELINE);
mEmojiconAlignment = a.getInt(R.styleable.Emojicon_emojiconAlignment, DynamicDrawableSpan.ALIGN_BOTTOM);
mTextStart = a.getInteger(R.styleable.Emojicon_emojiconTextStart, 0);
mTextLength = a.getInteger(R.styleable.Emojicon_emojiconTextLength, -1);
mUseSystemDefault = a.getBoolean(R.styleable.Emojicon_emojiconUseSystemDefault, mUseSystemDefault);
Expand Down

0 comments on commit 3849ee1

Please sign in to comment.