Android文字广告(Textview上下滚动),使用TextSwitcher控件实现

逃离我推掉我的手 2022-12-10 15:59 558阅读 0赞

经常可以在app上面看到许多的上下滚动textview,是可以直接使用TextSwitcher直接实现的。
开始还写了个自定义view来实现,然后发现官方原来有(:з」∠)

控件还有ImageSwitcher和ViewSwitcher。TextSwitcher和ImageSwitcher都是继承于ViewSwitcher的,所以使用方法都一样只不过对象不一样罢了。


效果

滚动效果

只是稍微截取了gif

使用

布局

  1. <TextSwitcher
  2. android:id="@+id/textSwitcher"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. </TextSwitcher>

直接放置一个TextSwitcher即可

activity中调用

  1. TextSwitcher textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
  2. textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
  3. @Override
  4. public View makeView() {
  5. return new TextView(context);
  6. }
  7. });
  8. List<String> texts = new ArrayList<>();
  9. for (int i = 0; i < 10; i++) {
  10. texts.add("循环....."+i);
  11. }
  12. textSwitcher.setText(texts.get(0));

最简单的设置属性,其中只要实现ViewFactory的重写即可,其他的ImageSwitcher和ViewSwitcher都是一样的,只不过设置的view不同!

当需要循环的时候只需要设置一个Handler来每次改变textSwitcher的setText中的值。
比如

  1. private Handler handler = new Handler();
  2. private Runnable task = new Runnable() {
  3. @Override
  4. public void run() {
  5. marker = ++marker % texts.size();
  6. textSwitcher.setText(texts.get(marker));
  7. handler.postDelayed(task, 4000);
  8. }
  9. };

在然后是通过调用

  1. textSwitcher.setInAnimation();
  2. textSwitcher.setOutAnimation();

方法来控制textSwitcher的进出动画

实现动画切换并且自动循环

创建一个类来统一管理textSwitcher的动画和循环

  1. import android.os.Handler;
  2. import android.util.Log;
  3. import android.view.animation.AlphaAnimation;
  4. import android.view.animation.Animation;
  5. import android.view.animation.AnimationSet;
  6. import android.view.animation.TranslateAnimation;
  7. import android.widget.TextSwitcher;
  8. import java.util.List;
  9. /**
  10. * TextSwitcherAnimation
  11. * Author: gjn.
  12. * Time: 2018/2/22.
  13. */
  14. public class TextSwitcherAnimation {
  15. private static final int DURATION = 1000;
  16. private TextSwitcher textSwitcher;
  17. private List<String> texts;
  18. private int marker;
  19. private AnimationSet InAnimationSet;
  20. private AnimationSet OutAnimationSet;
  21. private int delayTime = 2000;
  22. private Handler handler = new Handler();
  23. private Runnable task = new Runnable() {
  24. @Override
  25. public void run() {
  26. nextView();
  27. handler.postDelayed(task, delayTime * 2);
  28. }
  29. };
  30. public TextSwitcherAnimation(TextSwitcher textSwitcher, List<String> texts) {
  31. this.textSwitcher = textSwitcher;
  32. this.texts = texts;
  33. }
  34. public void start() {
  35. stop();
  36. handler.postDelayed(task, delayTime);
  37. }
  38. public void stop(){
  39. handler.removeCallbacks(task);
  40. }
  41. public int getMarker() {
  42. return marker;
  43. }
  44. public TextSwitcherAnimation setTexts(List<String> texts) {
  45. this.texts = texts;
  46. return this;
  47. }
  48. public void setDelayTime(int delayTime) {
  49. this.delayTime = delayTime;
  50. }
  51. public void create() {
  52. marker = 0;
  53. if (texts == null){
  54. Log.w("TextSwitcherAnimation", "texts is null");
  55. return;
  56. }
  57. if (textSwitcher == null) {
  58. Log.w("TextSwitcherAnimation", "textSwitcher is null");
  59. return;
  60. }
  61. textSwitcher.setText(texts.get(0));
  62. createAnimation();
  63. textSwitcher.setInAnimation(InAnimationSet);
  64. textSwitcher.setOutAnimation(OutAnimationSet);
  65. start();
  66. }
  67. private void createAnimation() {
  68. AlphaAnimation alphaAnimation;
  69. TranslateAnimation translateAnimation;
  70. int h = textSwitcher.getHeight();
  71. if (h <= 0) {
  72. textSwitcher.measure(0,0);
  73. h = textSwitcher.getMeasuredHeight();
  74. }
  75. InAnimationSet = new AnimationSet(true);
  76. OutAnimationSet = new AnimationSet(true);
  77. alphaAnimation = new AlphaAnimation(0,1);
  78. translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
  79. Animation.ABSOLUTE, h, Animation.ABSOLUTE, 0);
  80. InAnimationSet.addAnimation(alphaAnimation);
  81. InAnimationSet.addAnimation(translateAnimation);
  82. InAnimationSet.setDuration(DURATION);
  83. alphaAnimation = new AlphaAnimation(1,0);
  84. translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
  85. Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -h);
  86. OutAnimationSet.addAnimation(alphaAnimation);
  87. OutAnimationSet.addAnimation(translateAnimation);
  88. OutAnimationSet.setDuration(DURATION);
  89. }
  90. private void nextView() {
  91. marker = ++marker % texts.size();
  92. textSwitcher.setText(texts.get(marker));
  93. }
  94. }

实现很简单,就是做了一个进出动画和Handler来控制循环。这边就不复述了。

使用如下

  1. List<String> texts = new ArrayList<>();
  2. for (int i = 0; i < 10; i++) {
  3. texts.add("循环....."+i);
  4. }
  5. textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
  6. @Override
  7. public View makeView() {
  8. TextView t = new TextView(mContext);
  9. return t;
  10. }
  11. });
  12. new TextSwitcherAnimation(textSwitcher,texts).create();

发表评论

表情:
评论列表 (有 0 条评论,558人围观)

还没有评论,来说两句吧...

相关阅读