当前位置:首页 » android文章 » Android文字自动轮播实现

Android文字自动轮播实现

作者:KNN   日期:2020-03-02   分类:android文章

项目中需要使用文字自动轮播的效果,先上效果图;

实现也很简单,就是一个进入和出去的动画。可以让文字自动轮播。

首先最简单的两个动画,写在了XML里面,当然也可以写到java中,

第一个in_animation.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <translate
  4. android:duration="1000"
  5. android:fromYDelta="100%"
  6. android:toYDelta="0%" />
  7. </set>

第二个out_animation.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <translate
  4. android:duration="1000"
  5. android:fromYDelta="0%"
  6. android:toYDelta="-100%" />
  7. </set>

下面就要开始自定义这个自动轮播的TextView了,、

第一步:上网查了下,Android里面已经有了TextSwitcher这个类,我们继承这个类,实现ViewSwitcher.ViewFactory提供的创建TextView的方法即可;

第二步:为了实现轮播,当然是每隔一个时间段就播放一次的效果,我们可以使用Timer定时器,每隔几秒发送一个Message给Handler,handler接受到消息开始更新文字。代码不多,就一下子贴出来吧,类似这样:

  1. public class TextSwitchView extends TextSwitcher implements ViewSwitcher.ViewFactory{
  2. private int index= -1;
  3. private Context context;
  4. private String[] resources = {"窗前明月光","疑是地上霜","举头望明月","低头思故乡"};
  5. private Timer timer;
  6. private Handler mHandler = new Handler(){
  7. @Override
  8. public void handleMessage(Message msg) {
  9. switch(msg.what) {
  10. case 1:
  11. index = next();
  12. updateText();
  13. break;
  14. }
  15. super.handleMessage(msg);
  16. }
  17. };
  18. //自定义View的构造方法
  19. public TextSwitchView(Context context) {
  20. super(context);
  21. this.context = context;
  22. init();
  23. }
  24. public TextSwitchView(Context context, AttributeSet attrs) {
  25. super(context, attrs);
  26. this.context = context;
  27. init();
  28. }
  29. private void init() {
  30. if (timer == null) {
  31. timer = new Timer();
  32. }
  33. //实现ViewSwitcher.ViewFactory接口方法,创建出TextView并启动动画
  34. setFactory(this);
  35. setInAnimation(AnimationUtils.loadAnimation(context, R.anim.in_animation));
  36. setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.out_animation));
  37. }
  38. public void setResources(String[] res) {
  39. resources = res;
  40. }
  41. //这个是自定义View的启动点,从外面传进来的间隔时间,并以此来开启这个定时任务器
  42. public void setTextStillTime(long time) {
  43. if (timer == null) {
  44. timer = new Timer();
  45. } else {
  46. timer.scheduleAtFixedRate(new MyTask(), 1, time);
  47. }
  48. }
  49. //启动任务,每间隔time时间发送一个消息给handler更新文字
  50. private class MyTask extends TimerTask {
  51. @Override
  52. public void run() {
  53. mHandler.sendEmptyMessage(1);
  54. }
  55. }
  56. private int next() {
  57. int flag = index + 1;
  58. if (flag > resources.length - 1) {
  59. flag = flag - resources.length;
  60. }
  61. return flag;
  62. }
  63. private void updateText() {
  64. setText(resources[index]);
  65. }
  66. @Override
  67. public View makeView() {
  68. TextView tv = new TextView(context);
  69. tv.setTextColor(Color.parseColor("#FF0000"));
  70. tv.setTextSize(22);
  71. return tv;
  72. }
  73. }

在项目中使用,要是需要自动更新的话就使用 textSwitchView.setTextStillTime(3000)方法;

  1. /**
  2. * 自动更新的TextSwitchView
  3. */
  4. TextSwitchView textSwitchView = (TextSwitchView) findViewById(R.id.switcher2);
  5. String[] autoRes = {"窗前明月光","疑是地上霜","举头望明月","低头思故乡"};
  6. textSwitchView.setResources(autoRes);
  7. textSwitchView.setTextStillTime(3000);

要是需要手动点击的话,给SwitchTextView设置点击响应事件就好了,整体Activity代码:

  1. public class MainActivity extends AppCompatActivity {
  2. private TextSwitcher textSwitcher1;
  3. private int curStr;
  4. String[] mRes = {"点击我翻转", "窗前明月光","疑是地上霜","举头望明月","低头思故乡"};
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. /**
  10. * 自动更新的TextSwitchView
  11. */
  12. TextSwitchView textSwitchView = (TextSwitchView) findViewById(R.id.switcher2);
  13. String[] autoRes = {"窗前明月光","疑是地上霜","举头望明月","低头思故乡"};
  14. textSwitchView.setResources(autoRes);
  15. textSwitchView.setTextStillTime(3000);
  16. /**
  17. * 点击更新的TextSwitchView
  18. */
  19. textSwitcher1 = (TextSwitcher) findViewById(R.id.switcher1);
  20. textSwitcher1.setFactory(new ViewSwitcher.ViewFactory() {
  21. @Override
  22. public View makeView() {
  23. TextView tv = new TextView(MainActivity.this);
  24. tv.setTextColor(Color.parseColor("#30c2b1"));
  25. tv.setText("点击我翻转");
  26. tv.setTextSize(22);
  27. return tv;
  28. }
  29. });
  30. next(null);
  31. }
  32. public void next(View source) {
  33. textSwitcher1.setText(mRes[curStr++ % mRes.length]);
  34. }
  35. }

上面的onClick()事件是在xml中定义的:这里要注意下,到进来的项目和你自己的包名不同,需要修改成自己的包名的TextSwitchView

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <TextSwitcher
  7. android:id="@+id/switcher1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_marginTop="100dp"
  11. android:inAnimation="@anim/in_animation"
  12. android:outAnimation="@anim/out_animation"
  13. android:layout_gravity="center"
  14. android:onClick="next"></TextSwitcher>
  15. <com.example.verticalscrolltext.TextSwitchView
  16. android:id="@+id/switcher2"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_marginTop="100dp"
  20. android:inAnimation="@anim/in_animation"
  21. android:outAnimation="@anim/out_animation"
  22. android:layout_gravity="center"
  23. android:onClick="next"></com.example.verticalscrolltext.TextSwitchView>
  24. </LinearLayout>

上面把整个项目代码全贴出来了,下载项目代码链接:

https://github.com/buder-cp/base_component_learn/tree/master/verticalscrolltext

 

 

文章来源网络: 觉得很赞 打赏支持