Created
October 15, 2015 23:05
-
-
Save asamaru7/b58f54dcae87d5901fd2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.asamaru.test; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.RectF; | |
import android.text.style.ReplacementSpan; | |
import android.util.Log; | |
public class ColorVerticalCenterSpan extends ReplacementSpan { | |
private int backgroundColor = 0; | |
private int foregroundColor = 0; | |
public ColorVerticalCenterSpan(int foregroundColor) { | |
this.foregroundColor = foregroundColor; | |
} | |
public ColorVerticalCenterSpan(int foregroundColor, int backgroundColor) { | |
this.backgroundColor = backgroundColor; | |
this.foregroundColor = foregroundColor; | |
} | |
@Override | |
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { | |
// Background | |
if (backgroundColor != 0) { | |
paint.setColor(backgroundColor); | |
canvas.drawRect(new RectF(x, top, x + paint.measureText(text, start, end), bottom), paint); | |
} | |
// Text | |
if (foregroundColor != 0) { | |
paint.setColor(foregroundColor); | |
} | |
int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)); | |
canvas.drawText(text, start, end, x, yPos, paint); | |
} | |
@Override | |
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { | |
return Math.round(paint.measureText(text, start, end)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment