Skip to content

Instantly share code, notes, and snippets.

@extralam
Created May 21, 2015 16:46
Show Gist options
  • Save extralam/006732bc273fa00408e2 to your computer and use it in GitHub Desktop.
Save extralam/006732bc273fa00408e2 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Region.Op;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;
public class VerticalTextView extends TextView {
final boolean topDown;
public VerticalTextView(Context context, AttributeSet attrs) {
super(context, attrs);
int gravity = this.getGravity();
if(Gravity.isVertical(gravity) && (gravity & 112) == 80) {
this.setGravity(gravity & 7 | 48);
this.topDown = false;
} else {
this.topDown = true;
}
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
this.setMeasuredDimension(this.getMeasuredHeight(), this.getMeasuredWidth());
}
protected boolean setFrame(int l, int t, int r, int b) {
return super.setFrame(l, t, l + (b - t), t + (r - l));
}
public void draw(Canvas canvas) {
if(this.topDown) {
canvas.translate((float)this.getHeight(), 0.0F);
canvas.rotate(90.0F);
} else {
canvas.translate(0.0F, (float)this.getWidth());
canvas.rotate(-90.0F);
}
canvas.clipRect(0.0F, 0.0F, (float)this.getWidth(), (float)this.getHeight(), Op.REPLACE);
super.draw(canvas);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment