如何在Canvas上设置文本方向(旋转)。例如垂直。
答案 0 :(得分:1)
您可以使用rotate()
方法指定下一步要绘制的元素的旋转。
附加说明:请务必阅读Canvas
documentation的其余部分,因为它有很多方法可以操作它(假设您需要的不仅仅是在某个阶段旋转文本)
答案 1 :(得分:1)
这是一个简单的垂直文本实现和循环。
public class VerticalTextView extends TextView{
final boolean topDown;
public VerticalTextView(Context context, AttributeSet attrs){
super(context, attrs);
final int gravity = getGravity();
if(Gravity.isVertical(gravity) && (gravity&Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
setGravity((gravity&Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
topDown = false;
}else
topDown = true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
@Override
protected void onDraw(Canvas canvas){
TextPaint textPaint = getPaint();
textPaint.setColor(getCurrentTextColor());
textPaint.drawableState = getDrawableState();
canvas.save();
if(topDown){
canvas.translate(getWidth(), 0);
canvas.rotate(90);
}else {
canvas.translate(0, getHeight());
canvas.rotate(-90);
}
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
getLayout().draw(canvas);
canvas.restore();
}
}
默认情况下,旋转的文字是从上到下。如果你设置
android:gravity="bottom"
然后它从下到上绘制。
答案 2 :(得分:0)
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
canvas.rotate(90, x, y);
canvas.drawText("text", x, y, textPaint);
}