1

我想将我的文本视图设置为 -90 度,这意味着我必须像这样展示它!我必须在单个线性布局中显示 8 个文本视图。我可以旋转,但我只能看到一个文本视图。请任何人帮我举个例子。

在此处输入图像描述

提前致谢。

public class AngledTextView extends TextView
{ 

       public AngledTextView(Context context, AttributeSet attrs) 
       { 
          super(context, attrs); 
       } 

       @Override 
       protected void onDraw(Canvas canvas) 
       { // Save the current matrix 
         canvas.save(); 
         // Rotate this View at its center 
         canvas.rotate(270,this.getWidth()/2, this.getHeight() /2); 
         // Draw it 
         super.onDraw(canvas); 
         // Restore to the previous matrix 
         canvas.restore(); 
        } 
 } 
4

3 回答 3

9

对于垂直 TextView,我使用了以下代码,它运行良好。

public class VerticleTextView extends TextView {

final boolean topDown;
public VerticleTextView(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();
}
}
于 2012-10-11T07:32:41.557 回答
1

您应该使用 270 而不是 -90。这将完美地工作。

于 2013-12-09T10:59:49.220 回答
0

在 API 级别 12 中添加

    view.animate().rotationBy(90).start()
于 2015-02-22T17:56:07.943 回答