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

效果

  • 每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 dataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        dataList = new ArrayList<>();
        initDataList();
        voiceBarHintRunnable = new MyRunnable(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 implements Runnable{

        private final List list;

        public MyRunnable(List dataList) {
            list = dataList;
        }

        @Override
        public void run() {
            if (list == null || list.isEmpty()) {
                return;
            }
            T itemType = list.get(0);
            if (itemType instanceof String) {
                List 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 getRandomList(List list, int count) {
            if (list.size() <= count) {
                return list;
            } else {
                Random random = new Random();
                List tempList = new ArrayList();
                List randomList = new ArrayList();
                int temp = 0;
                for(int i = 0; i

布局文件




    

    


你可能感兴趣的:(周期循环定时取随机list+文本刷新+属性动画效果实现)