周期循環(huán)定時(shí)取隨機(jī)list+文本刷新+屬性動(dòng)畫(huà)效果實(shí)現(xiàn)

效果

  • 每5秒從源list,即下文dataList中隨機(jī)選取指定條目的不重復(fù)數(shù)據(jù);
  • Handler輪詢(xún)周期執(zhí)行上述操作;
  • 在UI線程更新UI操作;
  • 每次更新文本信息增加切換文本淡入淡出動(dòng)畫(huà)效果,多控件保證同進(jìn)同出。

核心代碼

public class MainActivity extends AppCompatActivity {

    private LinearLayout hintView;
    private TextView textViewFront;
    private TextView textViewBehind;

    /**
     * 周期循環(huán)
     */
    private Handler voiceBarHintHandler;
    private Runnable voiceBarHintRunnable;

    /**
     * 模擬數(shù)據(jù)源
     */
    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是指首次顯示延遲時(shí)間。
    }

    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中選取兩條隨機(jī)并且不重復(fù)的數(shù)據(jù)

                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時(shí)間。
        }

        /**
         * 從一個(gè)List中隨機(jī)選取兩條不重復(fù)的數(shù)據(jù)并將結(jié)果返回為一個(gè)新的List
         *
         * @param list 待選取的list集合
         * @param count  所選取的個(gè)數(shù)
         * @return randomList 篩選好的結(jié)果集
         */
        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);  //將產(chǎn)生的隨機(jī)數(shù)作為被抽list的索引
                    if (!tempList.contains(temp)){
                        tempList.add(temp);
                        randomList.add(list.get(temp));
                    }else {
                        i--;
                    }
                }
                return randomList;
            }
        }
    }

    /**
     * 開(kāi)啟屬性動(dòng)畫(huà)
     */
    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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 14,096評(píng)論 2 59
  • 對(duì)于android手機(jī)上的動(dòng)畫(huà)實(shí)現(xiàn)主要有三種,一種是幀動(dòng)畫(huà),一種是View動(dòng)畫(huà),以及3.0以上提供的屬性動(dòng)畫(huà),所有...
    查理吃西瓜閱讀 6,615評(píng)論 1 39
  • 前端工程師快速入門(mén) 概述 前端開(kāi)發(fā)工程師是Web前端開(kāi)發(fā)工程師的簡(jiǎn)稱(chēng),2007年才真正開(kāi)始受到重視的一個(gè)新興職業(yè)。...
    進(jìn)擊的大東閱讀 677評(píng)論 0 0
  • 一、簡(jiǎn)介 <<UITableView(或簡(jiǎn)單地說(shuō),表視圖)的一個(gè)實(shí)例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無(wú)邪8閱讀 10,963評(píng)論 3 3
  • 資料來(lái)源:http://www.itdecent.cn/p/916b2f8e1456 Activity Acti...
    百度不清閱讀 518評(píng)論 0 0

友情鏈接更多精彩內(nèi)容