anotherim/src/main/java/eu/siacs/conversations/ui/text/QuoteSpan.java

58 lines
1.8 KiB
Java
Raw Normal View History

2016-11-25 14:06:43 +00:00
package eu.siacs.conversations.ui.text;
import android.graphics.Canvas;
import android.graphics.Paint;
2017-11-07 12:23:49 +00:00
import android.support.annotation.ColorInt;
2016-11-25 14:06:43 +00:00
import android.text.Layout;
import android.text.TextPaint;
import android.text.style.CharacterStyle;
import android.text.style.LeadingMarginSpan;
import android.util.DisplayMetrics;
import android.util.TypedValue;
public class QuoteSpan extends CharacterStyle implements LeadingMarginSpan {
private final int color;
private final int width;
private final int paddingLeft;
private final int paddingRight;
private static final float WIDTH_SP = 2f;
private static final float PADDING_LEFT_SP = 1.5f;
private static final float PADDING_RIGHT_SP = 8f;
public QuoteSpan(int color, DisplayMetrics metrics) {
this.color = color;
this.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, WIDTH_SP, metrics);
this.paddingLeft = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, PADDING_LEFT_SP, metrics);
this.paddingRight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, PADDING_RIGHT_SP, metrics);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setColor(this.color);
}
@Override
public int getLeadingMargin(boolean first) {
return paddingLeft + width + paddingRight;
}
@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom,
CharSequence text, int start, int end, boolean first, Layout layout) {
Paint.Style style = p.getStyle();
int color = p.getColor();
p.setStyle(Paint.Style.FILL);
p.setColor(this.color);
c.drawRect(x + dir * paddingLeft, top, x + dir * (paddingLeft + width), bottom, p);
p.setStyle(style);
p.setColor(color);
}
2017-11-07 12:23:49 +00:00
@ColorInt
public int getColor() {
return this.color;
}
2016-11-25 14:06:43 +00:00
}