Skip to content

Instantly share code, notes, and snippets.

@Jthomas54
Last active January 17, 2017 20:18
Show Gist options
  • Save Jthomas54/d304e541b49824bf179035197c481e6e to your computer and use it in GitHub Desktop.
Save Jthomas54/d304e541b49824bf179035197c481e6e to your computer and use it in GitHub Desktop.
TextView that allows the text to be rendered vertically, from bottom to top
import android.content.Context
import android.graphics.Canvas
import android.graphics.Path
import android.graphics.Rect
import android.util.AttributeSet
import android.widget.TextView
public class VerticalTextView extends TextView {
private Rect _textBounds = new Rect();
public VerticalTextView(Context context) {
super(context);
}
public VerticalTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerticalTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(21)
public VerticalTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
getPaint().getTextBounds(getText().toString(), 0, getText().length(), _textBounds);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
@Override
public void draw(Canvas canvas) {
Path path = new Path();
int startX = (getWidth() + _textBounds.height()) / 2,
startY = (getHeight() + _textBounds.width()) / 2,
stopX = (getWidth() + _textBounds.height()) / 2,
stopY = (getHeight() - _textBounds.width()) / 2;
path.moveTo(startX, startY);
path.lineTo(stopX, stopY);
canvas.save();
getPaint().setColor(getCurrentTextColor());
canvas.drawTextOnPath(getText().toString(), path, 0f, 0f, getPaint());
canvas.restore();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment