Android在API级别> = 8中旋转TextView

Zba*_*ian 11 android rotation textview

我有简单的TextView

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:rotation="45"
   android:text="Simple text" />
Run Code Online (Sandbox Code Playgroud)

在Android 2.2.2上,文本不会旋转到45度.

我看到了不同的主题,但每个人都在做动画.我不想动画.我想要的只是旋转textview.

Sim*_*ges 17

在android中任何新视图都有一个名为setRotation(float)的方法可以使用它

textview.setRotation(float);
Run Code Online (Sandbox Code Playgroud)

但请注意,此方法已在API级别11中添加

所以如果你想支持它,你可以使用它

if (Build.VERSION.SDK_INT < 11) {

    RotateAnimation animation = new RotateAnimation(oldAngel, newAngel);
    animation.setDuration(100);
    animation.setFillAfter(true);
    textview.startAnimation(animation);
} else {

    textview.setRotation(progress);
}
Run Code Online (Sandbox Code Playgroud)


Jit*_*Dev 5

像这样创建一个自定义TextView

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 boolean setFrame(int l, int t, int r, int b){
      return super.setFrame(l, t, l+(b-t), t+(r-l));
   }

   @Override
   public void draw(Canvas canvas){
      if(topDown){
         canvas.translate(getHeight(), 0);
         canvas.rotate(90);
      }else {
         canvas.translate(0, getWidth());
         canvas.rotate(-90);
      }
      canvas.clipRect(0, 0, getWidth(), getHeight(), android.graphics.Region.Op.REPLACE);
      super.draw(canvas);
   }
}
Run Code Online (Sandbox Code Playgroud)

并使用 旋转角度canvas.rotate(90)在哪里90.