歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 怎麼旋轉TextView文字顯示方向

Android 怎麼旋轉TextView文字顯示方向

日期:2017/3/1 11:12:34   编辑:Linux編程

在一個項目中,需要旋轉TextView的文字顯示方向,怎麼實現呢?這裡提供一種變通的方法來實現該功能:Animation動畫,保存動畫結束狀態來實現該功能。

主要代碼如下:

1、定義一個anim xml資源文件rotate_right.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set>
  3. <rotate xmlns:Android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator"
  4. android:fromDegrees="0" android:toDegrees="-90" android:duration="0"
  5. android:pivotX="50%" android:pivotY="50%" android:repeatCount="0" />
  6. </set>

2、設置textview播放動畫

  1. private Animation mAnimationRight;
  2. private TextView mlblRightPhotoNum;
  3. mAnimationRight = AnimationUtils.loadAnimation(mContext, R.anim.rotate_right);
  4. mAnimationRight.setFillAfter(true);
  5. mlblRightPhotoNum = (TextView) findViewById(R.id.lblRightPhotoNum);
  6. mlblRightPhotoNum.setAnimation(mAnimationRight);

總結:主要用到了Animation 的 setFillAfter(boolean b)方法,該方法表示是否保持動畫結束時狀態;

拓展:

1、Animation 方法:setFillBefore(boolean b)當動畫結束後,是否返回動畫開始狀態。

2、當activity必須指定launchMode時【例如:Camera程序必須制定橫屏,才能取景正常】,可以通過OrientationEventListener及動畫旋轉來模擬橫豎屏效果。

Copyright © Linux教程網 All Rights Reserved