安卓控件TextSwitcher的使用(实现Textview的上下滚动)
经常可以在app上面看到许多的上下滚动textview,是可以直接使用TextSwitcher直接实现的。
开始还写了个自定义view来实现,然后发现官方原来有(:з」∠)。
控件还有ImageSwitcher和ViewSwitcher。TextSwitcher和ImageSwitcher都是继承于ViewSwitcher的,所以使用方法都一样只不过对象不一样罢了。
效果
只是稍微截取了gif
使用
布局
<TextSwitcher
android: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();
安卓控件TextSwitcher的使用(实现Textview的上下滚动)相关推荐
- 安卓控件使用系列2:TextView实现图文(图片和文字)混排
使用TextView实现图文混排是有些开发者并没有实现过,下面来介绍一下如何实现. 整体思路:首先定义根据资源文件的变量名返回资源文件ID的方法,定义指向图片资源文件变量名html格式的字符串,写一个 ...
- 安卓控件大小动态文字排版_动态Web排版:爆炸文字
安卓控件大小动态文字排版 Lately I've been interested in creating motion typography for the web. Making shattered ...
- android 柱状图_安卓控件 仪表盘控件 柱状图控件 曲线控件 xamarin.android 分类器 瓶子控件 报警控件 水箱控件 进度条控件等...
本篇博客主要介绍一个控件库,HslControls.dll 的界面,这个控件库支持winform,winform的参考另一篇文章:https://www.cnblogs.com/dathlin/p/1 ...
- 安卓控件 - 列表视图
文档目录 一.案例操作 1.创建安卓应用[ReadAncientPoetry] 2.将背景图片拷贝到drawable目录 3.布局资源文件activity_main.xml 4.诗歌列表项模板poem ...
- 转 安卓控件属性大全
"Android控件属性大全": 关键词:android 控件 属性 大全 控件属性: android属性 android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面 ...
- android view flipper,安卓控件——ViewFlipper
今天在逛博客的时候又学习到一个新控件,就是ViewFlipper,老规矩,还是通过一个demo来学习,下面先附上效果图: ViewFlipper效果图 根据效果图,说下利用该控件实现的几个简单功能点: ...
- Android 第五课 常用控件的使用方法(TextView、Button、EditView、 ImageView、 ProgressBar、 ProgressDialog等)
总结:见名知意 TextView: Button: EditView: ImageView: ProgressBar: ProgressDialog和AlertDialog有些类似,都可以再界面弹出对 ...
- 安卓控件使用系列17:ImageView获取网络图片
安卓使用ImageView控件获取网络图片是我们在开发应用程序中常常用到的一个功能,我们来分享一下是怎么实现这一功能的. 这个例子是点击Button按钮,就会按照制定url地址找到图片的位置,并下载绑 ...
- 安卓控件 listView 的学习及优化 (ConvetView、viewHolder)
一. listView 的学习 众所周知ListView 是一个控件,一个在垂直滚动的列表中显示条目的一个控件. 使用方法为: 1. 布局添加Listview 2. 在对应的activity找到lis ...
最新文章
- node_modules中包不完整的解决方法
- 微生物组文献1采用FimH拮抗物选择性抑制尿路致病性大肠杆菌
- MySQL的优化(大纲)
- linux命令行ps1变量_利用Shell中变量PS1定制Linux Shell命令主提示符
- 计算机bq,BQ24721部分翻译
- python爬虫知乎图片_Python爬虫入门教程 25-100 知乎文章图片爬取器之一
- Redis学习一Redis的介绍与安装部署
- 数据库访问技术(二)---ADO.NET
- Sqlserver2005迁移至Oracle系列之二:生成存储过程
- Oracle 11g安装步骤(超详细)
- excel冻结窗格线的设置问题
- 送人玫瑰,手有余香!
- win7 U盘安装和激活
- Installation failed due to: ‘‘cmd package install-create -r -t --user current --full --dont-kill -t
- 使用梯子导致的浏览器不能正常使用
- 芯片优缺点_“碳基芯片”的材料,将采用碳纳米管制成,或比传统芯片提升10倍...
- 苹果的黑科技:如何让按不动的触控板产生点按的感觉
- node+express 搭建商城项目(1-项目搭建)
- DBUS介绍与Linux C实例
- dual,rowid,rownum