美文网首页
周期循环定时取随机list+文本刷新+属性动画效果实现

周期循环定时取随机list+文本刷新+属性动画效果实现

作者: 千夜零一 | 来源:发表于2021-08-19 13:10 被阅读0次

    效果

    • 每5秒从源list,即下文dataList中随机选取指定条目的不重复数据;
    • Handler轮询周期执行上述操作;
    • 在UI线程更新UI操作;
    • 每次更新文本信息增加切换文本淡入淡出动画效果,多控件保证同进同出。

    核心代码

    public class MainActivity extends AppCompatActivity {
    
        private LinearLayout hintView;
        private TextView textViewFront;
        private TextView textViewBehind;
    
        /**
         * 周期循环
         */
        private Handler voiceBarHintHandler;
        private Runnable voiceBarHintRunnable;
    
        /**
         * 模拟数据源
         */
        private List<String> dataList;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
            dataList = new ArrayList<>();
            initDataList();
            voiceBarHintRunnable = new MyRunnable<String>(dataList);
            voiceBarHintHandler.removeCallbacksAndMessages(voiceBarHintRunnable); //null是清除词handler之前所有的消息
            voiceBarHintHandler.postDelayed(voiceBarHintRunnable,0);  //这里的delay是指首次显示延迟时间。
        }
    
        private void initView(){
           hintView = findViewById(R.id.hintView);
           textViewFront = findViewById(R.id.textViewFront);
           textViewBehind = findViewById(R.id.textViewBehind);
           voiceBarHintHandler = new Handler(this.getMainLooper());
        }
    
        private void initDataList(){
            dataList.add("李华");
            dataList.add("撒 凡尔赛 贝宁");
            dataList.add("李思思");
            dataList.add("易烊千玺");
        }
    
        class MyRunnable<T> implements Runnable{
    
            private final List<T> list;
    
            public MyRunnable(List<T> dataList) {
                list = dataList;
            }
    
            @Override
            public void run() {
                if (list == null || list.isEmpty()) {
                    return;
                }
                T itemType = list.get(0);
                if (itemType instanceof String) {
                    List<T> randomList = getRandomList(list, 2);  //从源list中选取两条随机并且不重复的数据
    
                    startValueAnimator(hintView);
                    for (int i = 0; i < randomList.size(); i++) {
                        String item = randomList.get(i).toString();
                        if (i == 0) {
                            textViewFront.setVisibility(View.VISIBLE);
                            textViewFront.setText("你好,"+ item);
                        } else if (i == 1) {
                            textViewBehind.setVisibility(View.VISIBLE);
                            textViewBehind.setText("你好," + item);
                        } else {
                        }
                    }
                }
                voiceBarHintHandler.postDelayed(this, 5000); //这里的delay才是每次切换的真正delay时间。
            }
    
            /**
             * 从一个List中随机选取两条不重复的数据并将结果返回为一个新的List
             *
             * @param list 待选取的list集合
             * @param count  所选取的个数
             * @return randomList 筛选好的结果集
             */
            public List<T> getRandomList(List<T> list, int count) {
                if (list.size() <= count) {
                    return list;
                } else {
                    Random random = new Random();
                    List<Integer> tempList = new ArrayList<Integer>();
                    List<T> randomList = new ArrayList<T>();
                    int temp = 0;
                    for(int i = 0; i<count ; i++){
                        temp = random.nextInt(list.size() -1);  //将产生的随机数作为被抽list的索引
                        if (!tempList.contains(temp)){
                            tempList.add(temp);
                            randomList.add(list.get(temp));
                        }else {
                            i--;
                        }
                    }
                    return randomList;
                }
            }
        }
    
        /**
         * 开启属性动画
         */
        private void startValueAnimator(View view){
            ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",1.0F,0.0F,1.0F);
            animator.setDuration(300);
            animator.setStartDelay(4850);
            animator.start();
        }
    }
    

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/hintView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/textViewFront"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/design_default_color_primary"
            android:padding="4dp"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:visibility="gone" />
    
        <TextView
            android:id="@+id/textViewBehind"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="@color/design_default_color_primary"
            android:padding="4dp"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:visibility="gone" />
    
    </LinearLayout>
    

    相关文章

      网友评论

          本文标题:周期循环定时取随机list+文本刷新+属性动画效果实现

          本文链接:https://www.haomeiwen.com/subject/xonobltx.html