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

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


效果

只是稍微截取了gif

使用

布局

<TextSwitcherandroid:id="@+id/textSwitcher"android:layout_width="match_parent"android:layout_height="wrap_content"></TextSwitcher>

直接放置一个TextSwitcher即可

activity中调用

TextSwitcher textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {@Overridepublic View makeView() {return new TextView(context);}
});
List<String> texts = new ArrayList<>();
for (int i = 0; i < 10; i++) {texts.add("循环....."+i);
}
textSwitcher.setText(texts.get(0));

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

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

    private Handler handler = new Handler();private Runnable task = new Runnable() {@Overridepublic void run() {marker = ++marker % texts.size();textSwitcher.setText(texts.get(marker));handler.postDelayed(task, 4000);}};

在然后是通过调用

textSwitcher.setInAnimation();
textSwitcher.setOutAnimation();

方法来控制textSwitcher的进出动画

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

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

import android.os.Handler;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.TextSwitcher;import java.util.List;/*** TextSwitcherAnimation* Author: gjn.* Time: 2018/2/22.*/public class TextSwitcherAnimation {private static final int DURATION = 1000;private TextSwitcher textSwitcher;private List<String> texts;private int marker;private AnimationSet InAnimationSet;private AnimationSet OutAnimationSet;private int delayTime = 2000;private Handler handler = new Handler();private Runnable task = new Runnable() {@Overridepublic void run() {nextView();handler.postDelayed(task, delayTime * 2);}};public TextSwitcherAnimation(TextSwitcher textSwitcher, List<String> texts) {this.textSwitcher = textSwitcher;this.texts = texts;}public void start() {stop();handler.postDelayed(task, delayTime);}public void stop(){handler.removeCallbacks(task);}public int getMarker() {return marker;}public TextSwitcherAnimation setTexts(List<String> texts) {this.texts = texts;return this;}public void setDelayTime(int delayTime) {this.delayTime = delayTime;}public void create() {marker = 0;if (texts == null){Log.w("TextSwitcherAnimation", "texts is null");return;}if (textSwitcher == null) {Log.w("TextSwitcherAnimation", "textSwitcher is null");return;}textSwitcher.setText(texts.get(0));createAnimation();textSwitcher.setInAnimation(InAnimationSet);textSwitcher.setOutAnimation(OutAnimationSet);start();}private void createAnimation() {AlphaAnimation alphaAnimation;TranslateAnimation translateAnimation;int h = textSwitcher.getHeight();if (h <= 0) {textSwitcher.measure(0,0);h = textSwitcher.getMeasuredHeight();}InAnimationSet = new AnimationSet(true);OutAnimationSet = new AnimationSet(true);alphaAnimation = new AlphaAnimation(0,1);translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,Animation.ABSOLUTE, h, Animation.ABSOLUTE, 0);InAnimationSet.addAnimation(alphaAnimation);InAnimationSet.addAnimation(translateAnimation);InAnimationSet.setDuration(DURATION);alphaAnimation = new AlphaAnimation(1,0);translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -h);OutAnimationSet.addAnimation(alphaAnimation);OutAnimationSet.addAnimation(translateAnimation);OutAnimationSet.setDuration(DURATION);}private void nextView() {marker = ++marker % texts.size();textSwitcher.setText(texts.get(marker));}
}

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

使用如下

        List<String> texts = new ArrayList<>();for (int i = 0; i < 10; i++) {texts.add("循环....."+i);}textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {@Overridepublic View makeView() {TextView t = new TextView(mContext);return t;}});new TextSwitcherAnimation(textSwitcher,texts).create();

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

  1. android文字广告的循环滚动,android怎样写一个循环文字滚动的TextView

    效果图: 在layout中这样来声明: android:layout_width="wrap_content" android:layout_height="wrap_c ...

  2. android中的TextView组件,Android-TextView文本视图控件详解

    TextView是Android开发当中运用到最多的控件之一,显示一行或者多行文本,也可以给上下左右设置图片,并且结合SpannableString和其子类可以做到图文混排. TextView文本视图 ...

  3. android禁止下拉刷新,Android开发之无痕过渡下拉刷新控件的实现思路详解

    相信大家已经对下拉刷新熟悉得不能再熟悉了,市面上的下拉刷新琳琅满目,然而有很多在我看来略有缺陷,接下来我将说明一下存在的缺陷问题,然后提供一种思路来解决这一缺陷,废话不多说!往下看嘞! 1.市面一些下 ...

  4. Android 打造完美的侧滑菜单/侧滑View控件

    概述 Android 打造完美的侧滑菜单/侧滑View控件,完全自定义实现,支持左右两个方向弹出,代码高度简洁流畅,兼容性高,控件实用方便. 详细 代码下载:http://www.demodashi. ...

  5. 如何在Android实现桌面清理内存简单Widget小控件

    如何在Android实现桌面清理内存简单Widget小控件 我们经常会看到类似于360.金山手机卫士一类的软件会带一个widget小控件,显示在桌面上,上面会显示现有内存大小,然后会带一个按键功能来一 ...

  6. Android入门(二)——常见布局与控件

    文章目录 一.常见界面布局 1.线性布局 LinearLayout 2.相对布局 RelativeLayout 3.表格布局 TableLayout 4.帧布局 FrameLayout 二.常见界面控 ...

  7. android 桌面图标的点击放大效果,Android仿英语流利说取词放大控件的实现方法(附demo源码下载)...

    本文实例讲述了Android仿英语流利说取词放大控件的实现方法.分享给大家供大家参考,具体如下: 1 取词放大控件 英语流利说是一款非常帮的口语学习app,在app的修炼页面长按屏幕,会弹出一个放大镜 ...

  8. android点击隐藏控件,Android编程实现点击EditText之外的控件隐藏软键盘功能

    本文实例讲述了Android编程实现点击EditText之外的控件隐藏软键盘功能.分享给大家供大家参考,具体如下: 工具类 ... public static void hideKeyboard(Co ...

  9. Android Native APP开发笔记:使用WebView控件加载网页

    文章目录 目的 基础使用 处理网页导航 加载本地网页 Web和Native之间交互 调试Web应用 处理页面重绘 总结 目的 WebView是一个比较常用的控件,功能上也比较单一,就是用来加载网页的, ...

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

    实现步骤: a.xml布局 <TextSwitcherandroid:id="@+id/textSwitcher"android:layout_width="mat ...

最新文章

  1. java代码例子_Java与C++两大语言比较
  2. 互联网元年:如何提升自己?
  3. 数据结构实验之链表四:有序链表的归并
  4. Android使用ViewPager+PhotoView实现图片查看器
  5. Python牛刀小试(五)--logging模块
  6. PHP iconv 解决utf-8和gb2312编码转换问题
  7. [Node.js] mySQL数据库 -- 英雄管理系统(完善)
  8. python基础知识——模块
  9. JAVA JDK API(中文) 1.6、1.8
  10. 解决html中图片自适应屏幕大小的问题
  11. 计算机毕业设计ssm汽车租赁管理系统mfobv系统+程序+源码+lw+远程部署
  12. 女神周迅离婚,Python分析国内离婚情况,结果触目惊心!
  13. MD5的使用(计算指定目录下文件的md5值)
  14. 计算机文化基础0008 17秋在线作业1,【在线】《计算机文化基础0008》17秋在线作业2.doc...
  15. c语言单链表的创建(头插法和尾插法)
  16. 计算机名打印机无法共享,win10系统打印机无法共享的解决方案
  17. 丢手帕问题 java_丢手帕问题java 实现
  18. RK3399 HDCP 2.2key
  19. 华工软院IBM LinuxONE Community Cloud云计算实验文档
  20. 马博士谈营养(23):又到腊八熬粥时

热门文章

  1. 我终于明白了!!!类、成员变量、方法、以及如何定义他们,还有static到底干啥用?
  2. ADB读取和备份安卓应用数据(无Root)
  3. Hystix、Feign、Zuul网关
  4. yuv420格式nv12,nv21,I420,YV12互转
  5. 数字货币 2018年市值排行榜前100币种简介
  6. Excel 2010 VBA 入门 110 获取最小值的自定义函数
  7. 美版有锁iphone4怎么突然连不上网了
  8. 解密刷子是如何进行APP刷量的?
  9. 带头像的用户显示功能
  10. 基于多任务学习和GCN的交通路网出租车需求预测