emojicon icon indicating copy to clipboard operation
emojicon copied to clipboard

why the image looook seems lower than the text ?

Open Sanjay-F opened this issue 9 years ago • 12 comments

word

Sanjay-F avatar Dec 11 '14 13:12 Sanjay-F

emojicon:emojiconSize +4sp >= android:textSize will have a better performace...

Sanjay-F avatar Dec 11 '14 14:12 Sanjay-F

it seems ..the view don't handle

    android:lineSpacingExtra="10sp"

    android:lineSpacingMultiplier="1.1"

so .the emoji will lower than the char.

Sanjay-F avatar Dec 12 '14 05:12 Sanjay-F

Hey @Sanjay-F Am getting Cannot Resolve method error in setUseSystemDefault and newInstance. Any idea where I am going wrong?

harishannam avatar Jan 01 '15 20:01 harishannam

@Sanjay-F 你处理好这个问题了吗?

KennethYo avatar Mar 03 '15 03:03 KennethYo

Fixed in 2b58b54. Please try out 1.2-SNAPSHOT.

rockerhieu avatar Mar 03 '15 07:03 rockerhieu

@rockerhieu Still no handle android:lineSpacingExtra="10sp" image

KennethYo avatar Mar 04 '15 02:03 KennethYo

@KennethYo try it if (mVerticalAlignment == ALIGN_BASELINE) { transY = top + ((paint.getFontMetricsInt().descent - paint.getFontMetricsInt().ascent) / 2) - ((b.getBounds().bottom - b.getBounds().top) / 2) - mTop; }

laiforever3 avatar Nov 17 '15 09:11 laiforever3

@Sanjay-F I guess this emoji's layout problem comes from the "MultiplelineSpace" property. When this property is set, both the bottom line and base line of the text have changed. Thus, no matter the alignment is the ALIGN_BASELINE or not, it would be lower a little bit.

I believe the only method to fix this problem is using customized ImageSpan. Here is the answer

Wish it could help :smile:

android-feng avatar Jan 04 '16 03:01 android-feng

anyone got any working solution.. please share

eRaised2X avatar Jun 01 '17 12:06 eRaised2X

public class EmojiconSpan extends DynamicDrawableSpan {

private final Context mContext;

private final int mResourceId;

private Drawable mDrawable;

private WeakReference<Drawable> mDrawableRef;

public EmojiconSpan(Context context, int resourceId, int emojiSize, int alignment, int textSize) {
    super(alignment);
    mContext = context;
    mResourceId = resourceId;
}

public Drawable getDrawable() {
    if (mDrawable == null) {
        try {
            mDrawable = mContext.getResources().getDrawable(mResourceId);
            if (mVerticalAlignment == ALIGN_BASELINE) {
                mDrawable.setBounds(0, 0, mDrawable.getIntrinsicHeight() + 20, mDrawable.getIntrinsicWidth() + 20);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return mDrawable;
}

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

    int transY = bottom - b.getBounds().bottom;
    if (mVerticalAlignment == ALIGN_BASELINE) {
        Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
        int fontHeight = fmPaint.descent - fmPaint.ascent;
        int centerY = y + fmPaint.descent - fontHeight / 2;
        transY = centerY - (mDrawable.getBounds().bottom - mDrawable.getBounds().top) / 2;
    }
    canvas.translate(x, transY);
    b.draw(canvas);
    canvas.restore();
}

public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fontMetricsInt) {
    Drawable drawable = this.getDrawable();
    Rect rect = drawable.getBounds();
    if (fontMetricsInt != null) {
        Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
        int fontHeight = fmPaint.bottom - fmPaint.top;
        int drHeight = rect.bottom - rect.top;
        int top = drHeight / 2 - fontHeight / 4;
        int bottom = drHeight / 2 + fontHeight / 4;
        fontMetricsInt.ascent = -bottom;
        fontMetricsInt.top = -bottom;
        fontMetricsInt.bottom = top;
        fontMetricsInt.descent = top;
    }
    return rect.right;
}

private Drawable getCachedDrawable() {
    if (mDrawableRef == null || mDrawableRef.get() == null) {
        mDrawableRef = new WeakReference<Drawable>(getDrawable());
    }
    return mDrawableRef.get();
}

} wechatimg10

zjc avatar Jul 12 '17 07:07 zjc

@zjc can you send a PR for this?

rockerhieu avatar Jul 12 '17 07:07 rockerhieu

sorry for my pool english ,i modify the EmojiconSpan.java, you can add a emojiconAlignment wrap_drawable for Solve this problem

zjc avatar Jul 12 '17 07:07 zjc