Implement adjustments to conversation list items to compensate for badge placement.

This commit is contained in:
Alex Hart
2021-10-13 15:14:09 -03:00
committed by Greyson Parrelli
parent 343aadcd9a
commit 731683ae09
8 changed files with 180 additions and 87 deletions

View File

@@ -0,0 +1,35 @@
package org.thoughtcrime.securesms.util;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.style.ReplacementSpan;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* Centers the given drawable in the bounds of a single line of text, regardless of the size of the given drawable.
*/
public class CenteredImageSpan extends ReplacementSpan {
private final Drawable drawable;
public CenteredImageSpan(@NonNull Drawable drawable) {
this.drawable = drawable;
}
@Override
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) {
return drawable.getBounds().right;
}
@Override
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
canvas.save();
int transY = top + (bottom - top) / 2 - drawable.getBounds().height() / 2;
canvas.translate(x, transY);
drawable.draw(canvas);
canvas.restore();
}
}

View File

@@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
@@ -121,6 +122,12 @@ public final class SpanUtil {
return imageSpan;
}
public static CharSequence buildCenteredImageSpan(@NonNull Drawable drawable) {
SpannableString imageSpan = new SpannableString(" ");
imageSpan.setSpan(new CenteredImageSpan(drawable), 0, imageSpan.length(), 0);
return imageSpan;
}
public static CharSequence learnMore(@NonNull Context context,
@ColorInt int color,
@NonNull View.OnClickListener onLearnMoreClicked)