Skip to content

Instantly share code, notes, and snippets.

@asamaru7
Created October 15, 2015 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asamaru7/0c3cd931e396cd0f8507 to your computer and use it in GitHub Desktop.
Save asamaru7/0c3cd931e396cd0f8507 to your computer and use it in GitHub Desktop.
package net.asamaru.test;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.style.ReplacementSpan;
public class VerticalCenterSpan extends ReplacementSpan {
public VerticalCenterSpan() {
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
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