在-90度旋转总Textview

时间:2012-10-11 06:57:48

标签: android rotation textview

我想在-90度设置我的textview意味着我要像这样展示它!我想在单线性布局中显示8个textviews。我能够旋转,但我只能看到一个textview.Please任何人都可以帮我一些例子。

enter image description here

提前致谢。

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(); 
        } 
 } 

3 个答案:

答案 0 :(得分:8)

对于垂直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();
}
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

在API级别12中添加

    view.animate().rotationBy(90).start()