安卓控件TextSwitcher的使用(实现Textview的上下滚动)
2018-02-22 17:34
435 查看
经常可以在app上面看到许多的上下滚动textview,是可以直接使用TextSwitcher直接实现的。
开始还写了个自定义view来实现,然后发现官方原来有(:з」∠)。
控件还有ImageSwitcher和ViewSwitcher。TextSwitcher和ImageSwitcher都是继承于ViewSwitcher的,所以使用方法都一样只不过对象不一样罢了。
![](http://img.blog.csdn.net/20180222173310240?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZzc3NzUyMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
只是稍微截取了gif
直接放置一个TextSwitcher即可
最简单的设置属性,其中只要实现ViewFactory的重写即可,其他的ImageSwitcher和ViewSwitcher都是一样的,只不过设置的view不同!
当需要循环的时候只需要设置一个Handler来每次改变textSwitcher的setText中的值。
比如
在然后是通过调用
方法来控制textSwitcher的进出动画
实现很简单,就是做了一个进出动画和Handler来控制循环。这边就不复述了。
使用如下
开始还写了个自定义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() { @Override public 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() { @Override public 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() { @Override public 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() { @Override public View makeView() { TextView t = new TextView(mContext); return t; } }); new TextSwitcherAnimation(textSwitcher,texts).create();
相关文章推荐
- 安卓控件使用系列4:TextView实现跑马灯效果
- 安卓控件使用系列2:TextView实现图文(图片和文字)混排
- 安卓控件使用系列16:ImageView实现图片缩放和旋转
- 使用viewpager嵌套实现上下左右滑动切换图片(IOS双向滚动翻页效果相同)
- 安卓控件使用系列23:HorizontalScrollView 水平滚动控件的使用方法
- Android 头部设置上下滚动的消息列表 TextView (TextSwitcher运用)
- 安卓控件——AutoCompleteTextView和MultiAutoCompleteTextView实现自动匹配输入的内容
- Android:TextView的垂直滚动效果和上下滚动效果,原生动画实现
- 首页垂直滚动TextView广告效果,使用TextSwicher+animation实现
- 实现TextView文本的上下滚动
- 使用 ScrollerView 实现 可上下滚动的分类栏
- 使用AutoCompleteTextView控件实现好友记录提醒
- 安卓滚动字幕以及TextSwitcher、ImageSwitcher使用
- 使用VerticalRollingTextView实现单行文字垂直滚动
- 安卓控件使用系列3:TextView点击连接跳转页面
- 安卓开发之使用viewpager+fragment实现滚动tab页
- 安卓学习:(2)安卓控件TextView的使用
- 使用viewpager嵌套实现上下左右滑动切换图片(IOS双向滚动翻页效果相同)
- 使用TextView实现消息提醒(徽章控件)
- 使用RecyclerView实现滚动控件