AutoTextView实现文字自动翻转效果

举报
再见孙悟空_ 发表于 2022/01/13 00:44:59 2022/01/13
【摘要】 效果图如上 代码如下: AutoTextView package com.jky.mobilebzt.view; import android.content.Context;import android.content.res.TypedArray;import android.graphics.Camera;import ...

效果图如上

代码如下:

AutoTextView

  
  1. package com.jky.mobilebzt.view;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.Camera;
  5. import android.graphics.Matrix;
  6. import android.text.TextUtils;
  7. import android.util.AttributeSet;
  8. import android.view.View;
  9. import android.view.animation.AccelerateInterpolator;
  10. import android.view.animation.Animation;
  11. import android.view.animation.Transformation;
  12. import android.widget.TextSwitcher;
  13. import android.widget.TextView;
  14. import android.widget.ViewSwitcher;
  15. import com.jky.mobilebzt.R;
  16. public class AutoTextView extends TextSwitcher implements
  17. ViewSwitcher.ViewFactory {
  18. private float mHeight;
  19. private Context mContext;
  20. //mInUp,mOutUp分别构成向下翻页的进出动画
  21. private Rotate3dAnimation mInUp;
  22. private Rotate3dAnimation mOutUp;
  23. //mInDown,mOutDown分别构成向下翻页的进出动画
  24. private Rotate3dAnimation mInDown;
  25. private Rotate3dAnimation mOutDown;
  26. public AutoTextView(Context context) {
  27. this(context, null);
  28. }
  29. public AutoTextView(Context context, AttributeSet attrs) {
  30. super(context, attrs);
  31. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.auto3d);
  32. mHeight = a.getDimension(R.styleable.auto3d_textSize, 36);
  33. a.recycle();
  34. mContext = context;
  35. init();
  36. }
  37. private void init() {
  38. setFactory(this);
  39. mInUp = createAnim(-90, 0, true, true);
  40. mOutUp = createAnim(0, 90, false, true);
  41. mInDown = createAnim(90, 0, true, false);
  42. mOutDown = createAnim(0, -90, false, false);
  43. //TextSwitcher主要用于文件切换,比如 从文字A 切换到 文字 B,
  44. //setInAnimation()后,A将执行inAnimation,
  45. //setOutAnimation()后,B将执行OutAnimation
  46. //初始化翻转
  47. // setInAnimation(mInUp);
  48. // setOutAnimation(mOutUp);
  49. }
  50. private Rotate3dAnimation createAnim(float start, float end, boolean turnIn, boolean turnUp) {
  51. final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, turnIn, turnUp);
  52. rotation.setDuration(800);
  53. rotation.setFillAfter(false);
  54. rotation.setInterpolator(new AccelerateInterpolator());
  55. return rotation;
  56. }
  57. //这里返回的TextView,就是我们看到的View
  58. @Override
  59. public View makeView() {
  60. TextView t = new TextView(mContext);
  61. // t.setGravity(Gravity.CENTER);
  62. t.setTextSize(mHeight);
  63. // t.setTextColor(Resources.getSystem().getColor(android.R.color.black));
  64. t.setTextColor(mContext.getResources().getColor(R.color.white));
  65. t.setTextSize(13);
  66. // t.setGravity(Gravity.CENTER_VERTICAL);
  67. t.setPadding(0,15,0,15);
  68. t.setMaxLines(1);
  69. t.setEllipsize(TextUtils.TruncateAt.END);
  70. return t;
  71. }
  72. //定义动作,向下滚动翻页
  73. public void previous() {
  74. if (getInAnimation() != mInDown) {
  75. setInAnimation(mInDown);
  76. }
  77. if (getOutAnimation() != mOutDown) {
  78. setOutAnimation(mOutDown);
  79. }
  80. }
  81. //定义动作,向上滚动翻页
  82. public void next() {
  83. if (getInAnimation() != mInUp) {
  84. setInAnimation(mInUp);
  85. }
  86. if (getOutAnimation() != mOutUp) {
  87. setOutAnimation(mOutUp);
  88. }
  89. }
  90. class Rotate3dAnimation extends Animation {
  91. private final float mFromDegrees;
  92. private final float mToDegrees;
  93. private float mCenterX;
  94. private float mCenterY;
  95. private final boolean mTurnIn;
  96. private final boolean mTurnUp;
  97. private Camera mCamera;
  98. public Rotate3dAnimation(float fromDegrees, float toDegrees, boolean turnIn, boolean turnUp) {
  99. mFromDegrees = fromDegrees;
  100. mToDegrees = toDegrees;
  101. mTurnIn = turnIn;
  102. mTurnUp = turnUp;
  103. }
  104. @Override
  105. public void initialize(int width, int height, int parentWidth, int parentHeight) {
  106. super.initialize(width, height, parentWidth, parentHeight);
  107. mCamera = new Camera();
  108. mCenterY = getHeight() / 2;
  109. mCenterX = getWidth() / 2;
  110. }
  111. @Override
  112. protected void applyTransformation(float interpolatedTime, Transformation t) {
  113. final float fromDegrees = mFromDegrees;
  114. float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
  115. final float centerX = mCenterX;
  116. final float centerY = mCenterY;
  117. final Camera camera = mCamera;
  118. final int derection = mTurnUp ? 1 : -1;
  119. final Matrix matrix = t.getMatrix();
  120. camera.save();
  121. if (mTurnIn) {
  122. camera.translate(0.0f, derection * mCenterY * (interpolatedTime - 1.0f), 0.0f);
  123. } else {
  124. camera.translate(0.0f, derection * mCenterY * (interpolatedTime), 0.0f);
  125. }
  126. camera.rotateX(degrees);
  127. camera.getMatrix(matrix);
  128. camera.restore();
  129. matrix.preTranslate(-centerX, -centerY);
  130. matrix.postTranslate(centerX, centerY);
  131. }
  132. }
  133. }

attrs.xml


  
  1. <declare-styleable name="auto3d">
  2. <attr name="textSize" format="dimension" />
  3. </declare-styleable>

xml


  
  1. <com.xx.xx.view.AutoTextView
  2. android:id="@+id/atv_auto_qy"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_marginTop="@dimen/margin_5" />

activity


  
  1. private void showQyInfo() {
  2. // qyPowers.clear();
  3. isShowQyView = true;
  4. mQyInfoView.setVisibility(View.VISIBLE);
  5. if(timer == null){
  6. String[] qyinfo = context.getResources().getStringArray(R.array.qy_power);
  7. qyPowers = Arrays.asList(qyinfo);
  8. timer = new Timer();
  9. timer.schedule(timerTask, 100, 3000);
  10. powerPosition = 0;
  11. }
  12. }
  13. //计时
  14. TimerTask timerTask = new TimerTask() {
  15. @Override
  16. public void run() {
  17. if(isShowQyView) {
  18. Message message = new Message();
  19. message.what = 0x001;
  20. handler.sendMessage(message);
  21. }
  22. }
  23. };
  24. final Handler handler = new Handler() {
  25. @Override
  26. public void handleMessage(Message msg) {
  27. switch (msg.what) {
  28. case 0x001:
  29. atvQy.next();
  30. atvQy.setText(qyPowers.get(powerPosition));
  31. powerPosition ++;
  32. powerPosition = powerPosition % 3;
  33. }
  34. }
  35. };

strings.xml


  
  1. <array name="qy_power">
  2. <item>8000+本建筑行业标准在线查看</item>
  3. <item>随时创建反馈,企业专家、云服务专家组、标准规范起草人为您解答</item>
  4. <item>移动课堂专享课程免费学习</item>
  5. </array>

文章来源: wukong.blog.csdn.net,作者:再见孙悟空_,版权归原作者所有,如需转载,请联系作者。

原文链接:wukong.blog.csdn.net/article/details/115464473

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。